451. Dis
题意 | 简单
给出 n 个点的一棵树,每个点有各自的点权,m 次询问两个点简单路径所构成点集的异或和。
1
≤
\leq
≤
n
,
m
n,m
n,m
≤
\leq
≤
2
e
5
2e^5
2e5 , 1
≤
\leq
≤ ai
≤
\leq
≤
1
0
9
10^9
109
思路 | 倍增
已知给出的是一颗树,在树上直接求
l
c
a
lca
lca ,把每个点权放进去通过倍增预处理一下,最后注意一下板子的边界就好了
数据量有点大,记得开快读~
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pi acos(-1)
#define e exp(1)
#define For(i, a, b) for(int (i) = (a); (i) <= (b); (i) ++)
#define Bor(i, a, b) for(int (i) = (b); (i) >= (a); (i) --)
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define eps 1e-7
#define INF 0x3f3f3f3f
#define inf -2100000000
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 2e5 + 10;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;
inline int read() {
int ret = 0, f = 0; char ch = getchar();
while (ch > '9' || ch < '0') f ^= ch == '-', ch = getchar();
while (ch <= '9' && ch >= '0') ret = ret * 10 + ch - '0', ch = getchar();
return f ? -ret : ret;
}
int a[maxn];
vector<int>g[maxn];
int fa[maxn][20];
int xo[maxn][20];
int n, m;
int depth[maxn];
void dfs(int u, int f,int d) {
fa[u][0] = f;
xo[u][0] = a[f];
depth[u] = d;
int len = g[u].size();
for (int i = 0; i < len; i++) {
int v = g[u][i];
if (v == f) continue;
dfs(v, u, d + 1);
}
}
void init() {
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
fa[j][i] = fa[fa[j][i - 1]][i - 1];
xo[j][i] = xo[j][i - 1] ^ xo[fa[j][i - 1]][i - 1];
}
}
}
int lca(int x,int y) {
if (depth[x] < depth[y]) swap(x, y);
int ans = 0;
for (int i = log2(depth[x] - depth[y]); i >= 0; i--) {
if ((1 << i) <= depth[x] - depth[y]) ans ^= xo[x][i] ,x = fa[x][i];
}
if (x == y) return ans ^ a[y];
for (int i = log2(depth[x]); i >= 0; i--) {
if (fa[x][i] != fa[y][i]) {
ans ^= xo[x][i];
ans ^= xo[y][i];
x = fa[x][i];
y = fa[y][i];
}
}
ans ^= xo[x][0];
return ans;
}
void solve() {
n = read(); m = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i < n; i++) {
int u, v; u = read(); v = read();
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1, 0);
init();
while (m--) {
int u, v;
u = read(); v = read();
int ans = lca(u, v) ^ a[u] ^ a[v];
if (u == v) printf("%d\n", a[u]);
else printf("%d\n", ans);
}
}
int main() {
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}