P3292 [SCOI2016]幸运数字
思路
如果这题是求x,yx, yx,y之间的距离显然我们可以通过树剖加线段树来写, 但是这里变成了求任意个数的异或最大值。如果给定区间我们显然可以通过线性基来求解,但是这里是动态的区间,每次区间需要我们去找,于是我们想到可以通过线段树加树剖来维护一段区间内的线性基,然后再合并不同区间的线性基,最后对合并好了的线性基进行求解最大值。线性基区间的寻找就有点像是前面说的求这两点间的点权和了。
然后整体复杂度分析一下log(n)log(n)log(n)的树剖,nlog(n)nlog(n)nlog(n)的线段树,再还有log(n)∗log(n)log(n) * log(n)log(n)∗log(n)的线性基合并,乘起来就是nlog4(n)n log ^ 4(n)nlog4(n),由于nnn比较小,勉强能冲一冲。
代码
/*Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x;
}const int N = 2e4 + 10;int head[N], to[N << 1], nex[N << 1], cnt = 1;int son[N], fa[N], sz[N], dep[N], top[N], id[N], rk[N], tot, n, m;ll value[N], tree[N << 2][64], ans[64];void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}void dfs1(int rt, int f) {dep[rt] = dep[f] + 1;sz[rt] = 1, fa[rt] = f;for(int i = head[rt]; i; i = nex[i]) {if(to[i] == f) continue;dfs1(to[i], rt);if(!son[rt] || sz[son[rt]] < sz[to[i]]) son[rt] = to[i];sz[rt] += sz[to[i]];}
}void dfs2(int rt, int tp) {top[rt] = tp; tot++;rk[tot] = rt, id[rt] = tot;if(!son[rt]) return ;dfs2(son[rt], tp);for(int i = head[rt]; i; i = nex[i]) {if(to[i] == fa[rt] || to[i] == son[rt]) continue;dfs2(to[i], to[i]);}
}void insert(ll * base, ll x) {for(int i = 60; i >= 0; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return ;}x ^= base[i];}}
}void merge(ll * a, ll * b) {for(int i = 60; i >= 0; i--) {if(b[i]) {insert(a, b[i]);}}
}void push_up(int rt) {merge(tree[rt], tree[ls]);merge(tree[rt], tree[rs]);
}void build(int rt, int l, int r) {if(l == r) {insert(tree[rt], value[rk[l]]);return ;}build(lson);build(rson);push_up(rt);
}void query(int rt, int l, int r, int L, int R) {if(l >= L && r <= R) {merge(ans, tree[rt]);return ;}if(L <= mid) query(lson, L, R);if(R > mid) query(rson, L, R);
}ll query_max() {ll res = 0;for(int i = 60; i >= 0; i--) {if((res ^ ans[i]) > res) {res ^= ans[i];}ans[i] = 0;//用完了就重置0方便下一次查找答案的线性基合并。}return res;
}ll solve(int x, int y) {while(top[x] != top[y]) {if(dep[top[x]] < dep[top[y]]) swap(x, y);query(1, 1, n, id[top[x]], id[x]);x = fa[top[x]];}if(dep[x] > dep[y]) swap(x, y);query(1, 1, n, id[x], id[y]);return query_max();
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);n = read(), m = read();for(int i = 1; i <= n; i++) {value[i] = read();}for(int i = 1; i < n; i++) {int x = read(), y = read();add(x, y);add(y, x);}dfs1(1, 0);dfs2(1, 1);build(1, 1, n);for(int i = 1; i <= m; i++) {int x = read(), y = read();printf("%lld\n", solve(x, y));}return 0;
}