[SCOI2016]幸运数字

题目

传送门

题解

考虑使用 \(LCA\) 加上暴力合并线性基。

那么这道题就变成了一道板题,没什么可说的了。

其实我本来也只想挂一个板子的

唯一需要注意的就是此题似乎只能用读入优化与输出优化才可以过,有点卡常\(=\space =\)

#include<bits/stdc++.h>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T qread(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=20000;
const int logMAXN=15;

template<class T>struct liner_basic{
    #define MAXSIZE 62
    T f[MAXSIZE+5];
    int siz,failed;
    liner_basic(){memset(f,siz=failed=0,sizeof f);}
    inline void insert(T num){
        if(!num)return;
        fep(i,MAXSIZE,0){
            if(!(num>>i))continue;
            if(!f[i]){f[i]=num;++siz;return;}
            num^=f[i];
        }++failed;
    }
    inline T query(T ret){
        fep(i,MAXSIZE,0)if((ret^f[i])>ret)ret^=f[i];
        return ret;
    }
    friend liner_basic operator + (const liner_basic<T> lhs,const liner_basic<T> rhs){
        liner_basic<T>ret=lhs;
        rep(i,0,MAXSIZE)ret.insert(rhs.f[i]);
        return ret;
    }
    #undef MAXSIZE
};
liner_basic<LL>f[MAXN+5][logMAXN+5];
int to[MAXN+5][logMAXN+5],dep[MAXN+5];

struct edge{int to,nxt;}e[MAXN<<2|2];
int tail[MAXN+5],ecnt=-1;
inline void add_edge(const int u,const int v){
    e[++ecnt]=edge{v,tail[u]};tail[u]=ecnt;
    e[++ecnt]=edge{u,tail[v]};tail[v]=ecnt;
}

LL w[MAXN+5];

int n,q;

inline void Init(){
    memset(tail,-1,sizeof tail);
    // cin>>n>>q;
    n=qread(1),q=qread(1);
    rep(i,1,n)w[i]=qread(1ll);
    rep(i,1,n-1)add_edge(qread(1),qread(1));
}

void dfs(const int u,const int fa){
    f[u][0].insert(w[fa]);
    to[u][0]=fa,dep[u]=dep[fa]+1;
    // printf("Now u == %d, fa == %d\n",u,fa);
    for(int i=tail[u],v;~i;i=e[i].nxt)if((v=e[i].to)^fa)
        dfs(v,u);
}

inline void buildtre(){
    dfs(1,0);
    rep(j,1,logMAXN)rep(i,1,n){
        to[i][j]=to[to[i][j-1]][j-1];
        f[i][j]=f[i][j-1]+f[to[i][j-1]][j-1];
    }
}

inline liner_basic<LL>getlca(int u,int v){
    liner_basic<LL>lG,rG;
    lG.insert(w[u]),rG.insert(w[v]);
    // if(u==v)return lG+rG;
    if(dep[u]<dep[v])swap(u,v);
    fep(i,logMAXN,0)if(dep[to[u][i]]>=dep[v]){
        lG=lG+f[u][i];
        u=to[u][i];
    }
    if(u==v)return lG+rG;
    fep(i,logMAXN,0)if(to[u][i]^to[v][i]){
        lG=lG+f[u][i],rG=rG+f[v][i];
        u=to[u][i],v=to[v][i];
    }
    lG=lG+f[u][0];
    return lG+rG;
}

inline void getquery(){
    int u,v;
    while(q--){
        u=qread(1),v=qread(1);
        liner_basic<LL>G=getlca(u,v);
        writc(G.query(0),'\n');
    }
}

signed main(){
    Init();
    // puts("finished Init");
    buildtre();
    getquery();
    return 0;
}
posted @ 2020-05-15 20:38  Arextre  阅读(110)  评论(0编辑  收藏  举报