题意

题目链接

Sol

线性基是可以合并的

倍增维护一下

然后就做完了??

喵喵喵?

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 2e4 + 10, B = 60;
inline LL read() {
char c = getchar(); LL x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, Q, g[MAXN][16], dep[MAXN];
LL a[MAXN];
vector<int> v[MAXN];
struct Node {
LL P[62];
Node() {
memset(P, 0, sizeof(P));
}
void insert(LL x) {
for(int i = B; i >= 0; i--) {
if((!(x >> i & 1))) continue;
if(P[i]) {x ^= P[i]; continue;}
P[i] = x; break;
}
}
LL QueryMax() {
LL ans = 0;
for(int i = B; i >= 0; i--) ans = max(ans, ans ^ P[i]);
return ans;
}
Node operator + (const Node &rhs) const {
Node ans;
for(int i = 0; i <= B; i++) ans.P[i] = this -> P[i];
for(int i = 0; i <= B; i++) if(rhs.P[i]) ans.insert(rhs.P[i]);
return ans;
}
}f[MAXN][16];
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
g[x][0] = fa;
f[x][0].insert(a[x]); f[x][0].insert(a[fa]);
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa) continue;
dfs(to, x);
}
}
void Pre() {
for(int j = 1; j <= 15; j++)
for(int i = 1; i <= N; i++)
g[i][j] = g[g[i][j - 1]][j - 1],
f[i][j] = f[i][j - 1] + f[g[i][j - 1]][j - 1];
}
LL Query(int x, int y) {
Node base; base.insert(a[x]); base.insert(a[y]);
if(dep[x] < dep[y]) swap(x, y);
for(int i = 15; i >= 0; i--)
if(dep[g[x][i]] >= dep[y])
base = base + f[x][i], x = g[x][i];
if(x == y) return base.QueryMax();
for(int i = 15; i >= 0; i--)
if(g[x][i] != g[y][i]) {
base = base + f[x][i];
base = base + f[y][i];
x = g[x][i], y = g[y][i];
}
base = base + f[x][0]; base = base + f[y][0];
return base.QueryMax();
}
int main() {
#ifndef ONLINE_JUDGE
//freopen("a.in", "r", stdin); freopen("b.out", "w", stdout);
#endif
N = read(); Q = read();
for(int i = 1; i <= N; i++) a[i] = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dfs(1, 0);
Pre();
while(Q--) {
int x = read(), y = read();
printf("%lld\n", Query(x, y));
}
return 0;
}
/*
8 4
45654 251 321 54687 321321 654 6321432 5646
1 2
2 3
2 4
1 5
4 6
6 7
5 8 7 8
7 5
6 8
4 1
*/

最新文章

  1. Android系统的五种数据存储形式(二)
  2. javascript对json对象的序列化与反序列化
  3. linux svn客户端 常用命令
  4. android 点击重新加载界面设计
  5. ViewPager + HorizontalScrollView 实现可滚动的标签栏
  6. JavaSE思维导图(六)
  7. ReactJs入门思路
  8. 1.Java
  9. css3新属性box-orient
  10. Delphi调用SQL分页存储过程实例
  11. 树莓派中编译Opencv3.4.1和OpenCVSharp库
  12. html5随机背景颜色
  13. 针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1)
  14. 《F4+2》β冲刺第二天
  15. c# list修改某一个属性的值
  16. Docker vs Warden
  17. LINUX 环境变量总结
  18. Python中级 —— 06SMTP发送电子邮件
  19. Putty连接虚拟机Centos出现:Network error:Connection refused的解决方法
  20. [Clr via C#读书笔记]Cp9参数

热门文章

  1. C#-WebForm-css box-shadow 给边框添加阴影效果
  2. 我也学习JAVA多线程-join
  3. DB2 体系结构 (进程模型)
  4. vSphere通过Client创建Centos7主机
  5. Linux网络编程服务器模型选择之并发服务器(下)
  6. spring boot快速入门 10: 日志使用
  7. ifram的使用 左边是&lt;a&gt;链接 右边是对应网页嵌套的显示网页链接内容 和toggle的收放用法
  8. ubuntu手动安装PhantomJS
  9. sql在insert时判断有无唯一性冲突
  10. Linux下Tomcat8.0.44配置使用Apr