bzoj 4568 [SCOI 2016] 幸运数字

题目大意

给定一棵\(n\)个点的树,每个点有权值
\(q\)次询问树上路径中
每个点权值可选可不选的最大异或和
\(n\le 2*10^4,q\le 2*10^5,val[i]\le 2^{60}\)

分析

线性基
但数据范围不太对啊woc
两线性基合并\(O(60^2)\)
如果 树剖+线性基 则\(O(q\log^2n ~60^2)\)
如果 树上倍增+线性基 则是一个常数较大的\(O(q\log n~60^2)\)
不是很妙啊

做法1

注意到,这不是常规的树上询问,因为是异或(线性基)
线性基同一个值重复插入时没有问题的
即树上某一段算重是可以的
那么我们可以这样算
1086046-20170710110919728-505044825.jpg
其中橙色部分时一个长度\(2^k\)的段
这样就只用统计\(4\)次了
复杂度\(O(4*60^2~q)\)

做法2

点分治
求出重心到每个点的线性基\(O(60n)\)
然后解决询问
如果询问与该重心有关,单次\(O(60^2)\)
否则将该询问传到儿子
点分每层扫过的询问数\(O(q)\)
总复杂度
\(O(60n\log n~+~q \log n~+~60^2 q)\)

solution 1

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2e4+7;
const int B=60;
typedef long long LL;
 
inline int rd(){
    int x=0;bool f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(;isdigit(c);c=getchar()) x=x*10+c-48;
    return f?x:-x;
}
 
inline LL lrd(){
    LL x=0;bool f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(;isdigit(c);c=getchar()) x=x*10+c-48;
    return f?x:-x;
}
 
struct vec{
    int g[M],te;
    struct edge{
        int y,nxt;
        edge(int _y=0,int _nxt=0){
            y=_y,nxt=_nxt;
        }
    }e[M<<1];
    vec(){memset(g,0,sizeof g);te=0;}
    inline void push(int x,int y){e[++te]=edge(y,g[x]);g[x]=te;}
    inline void push2(int x,int y){push(x,y);push(y,x);}
    inline int& operator () (int x){return g[x];}
    inline edge& operator [] (int x){return e[x];}
}e;
 
struct Base{
    LL a[B+3];
    Base(){memset(a,0,sizeof a);}
    void ins(LL x){
        for(int i=B;i>=0;i--) if(x>>i&1){
            if(a[i]) x^=a[i];
            else {a[i]=x;return;}
        }
    }
 
    LL getmx(){
        LL res=0; int i;
        for(i=B;i>=0;i--) if((res^a[i])>res) res^=a[i];
        return res;
    }
 
    friend Base merge(const Base &x,const Base &y){
        Base res; int i;
        for(i=B;i>=0;i--) if(x.a[i]) res.ins(x.a[i]);
        for(i=B;i>=0;i--) if(y.a[i]) res.ins(y.a[i]);
        return res;
    }
 
    friend Base merge(const Base &x,const LL &y){
        Base res=x;
        res.ins(y);
        return res;
    }
};
 
int n,m,D;
LL val[M];
int dep[M];
int pre[M][15];
Base f[M][15];
 
void dfs(int x){
    int p,y;
    for(p=e(x);p;p=e[p].nxt)
        if((y=e[p].y)!=pre[x][0]){
            dep[y]=dep[x]+1;
            pre[y][0]=x;
            f[y][0].ins(val[y]);
            dfs(y);
        }
}
 
void init(){
    D=(int)log2(n);
    int i,j;
    for(j=1;j<=D;j++)
        for(i=1;i<=n;i++){
            pre[i][j]=pre[pre[i][j-1]][j-1];
            f[i][j]=merge(f[i][j-1],f[pre[i][j-1]][j-1]);
        }
}
 
int LCA(int x,int y){
    int i;
    if(dep[x]<dep[y]) swap(x,y);
    for(i=D;i>=0;i--)
        if(dep[pre[x][i]]>=dep[y]) x=pre[x][i];
    if(x==y) return x;
    for(i=D;i>=0;i--)
        if(pre[x][i]!=pre[y][i]) x=pre[x][i],y=pre[y][i];
    return pre[x][0];
}
 
int jp(int x,int kth){
    for(int i=D;i>=0;i--) if(kth>>i&1) x=pre[x][i];
    return x;
}
 
Base calc(int x,int to){
    for(int i=D;i>=0;i--) if(dep[pre[x][i]]>=dep[to]){
        int y=jp(x,dep[pre[x][i]]-dep[to]);
        return merge(f[x][i],f[y][i]);
    }
    return Base();
}
 
LL get(int x,int y){
    Base res;
    int lca=LCA(x,y),i;
    res=merge(merge(calc(x,lca),calc(y,lca)),val[lca]);
    return res.getmx();
}
 
int main(){
 
    int i,x,y;
 
    n=rd(),m=rd();
    for(i=1;i<=n;i++) val[i]=lrd();
 
    for(i=1;i<n;i++) e.push2(rd(),rd());
 
    pre[1][0]=0; dep[1]=1;
    dfs(1);
    init();
 
    for(i=1;i<=m;i++){
        x=rd(),y=rd();
        printf("%lld\n",get(x,y));
    }
 
    return 0;
}

solution 2

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=2e5+7;
const int M=2e4+7;
const int B=60;
typedef long long LL;
 
inline int ri(){
    int x=0;bool f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(;isdigit(c);c=getchar()) x=x*10+c-48;
    return f?x:-x;
}
 
inline LL rl(){
    LL x=0;bool f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(;isdigit(c);c=getchar()) x=x*10+c-48;
    return f?x:-x;
}
 
struct vec{
    int g[M],te;
    struct edge{
        int y,nxt;
        edge(int _y=0,int _nxt=0){y=_y,nxt=_nxt;}
    }e[M<<1];
    vec(){memset(g,0,sizeof g);te=0;}
    inline void push(int x,int y){e[++te]=edge(y,g[x]);g[x]=te;}
    inline void push2(int x,int y){push(x,y);push(y,x);}
    inline int& operator () (int x){return g[x];}
    inline edge& operator [] (int x){return e[x];}
}e;
 
struct vec2{
    int g[M],te;
    struct ques{
        int x,y,id,nxt;
        ques(int _x=0,int _y=0,int _id=0,int _nxt=0){x=_x,y=_y,id=_id,nxt=_nxt;}
    }e[N*17];
    vec2(){memset(g,0,sizeof g);te=0;}
    inline void push(int u,int x,int y,int id){e[++te]=ques(x,y,id,g[u]);g[u]=te;}
    inline int& operator () (int x){return g[x];}
    inline ques& operator [] (int x){return e[x];}
}ask;
 
struct Base{
    LL a[B+3];
 
    Base(){memset(a,0,sizeof a);}
    void clear(){memset(a,0,sizeof a);}
 
    void ins(LL x){
        for(int i=B;i>=0;i--) if(x>>i&1){
            if(a[i]) x^=a[i];
            else {a[i]=x;break;}
        }
    }
 
    LL getmx(){
        LL res=0;
        for(int i=B;i>=0;i--) if((res^a[i])>res) res^=a[i];
        return res;
    }
 
    friend Base merge(const Base &x,const Base &y){
        Base res;
        for(int i=B;i>=0;i--) if(x.a[i]) res.ins(x.a[i]);
        for(int i=B;i>=0;i--) if(y.a[i]) res.ins(y.a[i]);
        return res;
    }
 
    friend Base merge(const Base &x,const LL &y){
        Base res=x;
        res.ins(y);
        return res;
    }
};
 
int n,m;
LL val[M];
int sz[M];
int mi,size,rt;
bool vis[M];
int bl[M];
LL ans[N];
Base f[M];
 
LL calc(int x,int y){
    Base res=merge(f[x],f[y]);
    return res.getmx();
}
 
void getsz(int x,int fa){
    sz[x]=1;
    int p,y;
    for(p=e(x);p;p=e[p].nxt)
    if((y=e[p].y)!=fa&&!vis[y]){
        getsz(y,x);
        sz[x]+=sz[y];
    }
}
 
void getrt(int x,int fa){
    int f=size-sz[x],p,y;
    for(p=e(x);p;p=e[p].nxt)
    if((y=e[p].y)!=fa&&!vis[y]){
        getrt(y,x);
        f=max(f,sz[y]);
    }
    if(f<mi) mi=f,rt=x;
}
 
void dfs(int x,int fa){
    f[x]=merge(f[fa],val[x]);
    int p,y;
    for(p=e(x);p;p=e[p].nxt)
    if((y=e[p].y)!=fa&&!vis[y]) bl[y]=bl[x],dfs(y,x);
}
 
void work(int fr){
    getsz(fr,0);
    mi=size=sz[fr];
    getrt(fr,0);
    int x=rt,i,p,y;
    vis[x]=1;
 
    bl[x]=x;
    f[x].clear(); f[x].ins(val[x]);
    if(x!=fr) {ask(x)=ask(fr); ask(fr)=0;}//
 
    for(p=e(x);p;p=e[p].nxt)
    if(!vis[y=e[p].y]){
        bl[y]=y;
        dfs(y,x);
    }
 
    for(p=ask(x);p;p=ask[p].nxt){
        if(bl[ask[p].x]==bl[ask[p].y]) ask.push(bl[ask[p].x],ask[p].x,ask[p].y,ask[p].id);
        else ans[ask[p].id]=calc(ask[p].x,ask[p].y);
    }
     
    for(p=e(x);p;p=e[p].nxt)
    if(!vis[y=e[p].y]) work(y);
}
 
int main(){
 
    int i,x,y;
    n=ri(),m=ri();
    for(i=1;i<=n;i++) val[i]=rl();
 
    for(i=1;i<n;i++) e.push2(ri(),ri());
 
    for(i=1;i<=m;i++){
        x=ri(), y=ri();
        if(x==y) ans[i]=val[x];
        else ask.push(1,x,y,i);
    }
 
    work(1);
 
    for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
 
    return 0;
}

转载于:https://www.cnblogs.com/acha/p/7145254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值