Solution to a problem P3292 [[SCOI2016] lucky number]

Topic Link

Solution [SCOI2016] lucky number

Title effect: Given a tree, each query exclusive right or maximum point on the path

analysis:

See a set of requirements that a subset of the exclusive-OR value substantially maximum we can think of the conventional linear group

The combined first group is linear complexity \ (O (logm ^ 2) \) a, \ (m \) is the range

So sbzcy wrote a sectional TLE a tree

We need to consider optimization, since without modification, is static challenge, we consider a similar request \ (the LCA \) method, multiplying linear group on a path to merge

Note processing, and the border issue slight cards often use to optimize read

#include <cstdio>
#include <cctype>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 32769,limit = 62,maxdep = 23;
inline ll read(){
    ll x = 0;char c = getchar();
    while(!isdigit(c))c = getchar();
    while(isdigit(c))x = x * 10 + c - '0',c = getchar();
    return x;
}
struct LB{//线性基
    ll p[limit + 1];
    inline ll query()const{
        ll res = 0;
        for(int i = limit;i >= 0;i--)
            if((res ^ p[i]) > res)res ^= p[i];
        return res;
    }
    inline void insert(ll x){
        for(int i = limit;i >= 0;i--){
            if(!(x >> i))continue;
            if(!p[i]){
                p[i] = x;
                break;
            }
            x ^= p[i];
        }
    }
    inline void merge(const LB &x){
        for(int i = limit;i >= 0;i--)
            if(x.p[i])insert(x.p[i]);
    }
}f[maxn][maxdep + 1],zero;
vector<int> G[maxn];
inline void addedge(int from,int to){
    G[from].push_back(to);
}
int n,q,faz[maxn][maxdep + 1],dep[maxn];
ll val[maxn];
void dfs(int u){//预处理倍增
    dep[1] = 1;
    f[u][0].insert(val[u]);
    for(int i = 1;i <= maxdep;i++)
        f[u][i] = f[u][i - 1],f[u][i].merge(f[faz[u][i - 1]][i - 1]),faz[u][i] = faz[faz[u][i - 1]][i - 1];
    for(int v : G[u]){
        if(v == faz[u][0])continue;
        faz[v][0] = u;
        dep[v] = dep[u] + 1;
        dfs(v);
    }
}
inline ll path_query(int x,int y){//查询
    LB res = zero;
    if(dep[x] < dep[y])swap(x,y);
    for(int i = maxdep;i >= 0;i--)
        if(dep[faz[x][i]] >= dep[y])
            res.merge(f[x][i]),x = faz[x][i];
    if(x == y)return res.merge(f[x][0]),res.query();
    for(int i = maxdep;i >= 0;i--)
        if(faz[x][i] != faz[y][i]){
            res.merge(f[x][i]),x = faz[x][i];
            res.merge(f[y][i]),y = faz[y][i];
        }
    res.merge(f[x][0]);
    res.merge(f[y][0]);
    res.merge(f[faz[x][0]][0]);
    return res.query();
}
int main(){
    n = read(),q = read();
    for(int i = 1;i <= n;i++)val[i] = read();
    for(int x,y,i = 1;i < n;i++)
        x = read(),y = read(),addedge(x,y),addedge(y,x);
    dfs(1);
    for(int x,y,i = 1;i <= q;i++)
        x = read(),y = read(),printf("%lld\n",path_query(x,y));
    return 0;
}

Guess you like

Origin www.cnblogs.com/colazcy/p/11515173.html