题意

题目链接

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
*/

BZOJ4568: [Scoi2016]幸运数字(线性基 倍增)的更多相关文章

  1. BZOJ 4568: [Scoi2016]幸运数字 [线性基 倍增]

    4568: [Scoi2016]幸运数字 题意:一颗带点权的树,求树上两点间异或值最大子集的异或值 显然要用线性基 可以用倍增的思想,维护每个点向上\(2^j\)个祖先这些点的线性基,求lca的时候合 ...

  2. 洛谷P3292 [SCOI2016]幸运数字 线性基+倍增

    P3292 [SCOI2016]幸运数字 传送门 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在 ...

  3. P3292 [SCOI2016]幸运数字 [线性基+倍增]

    线性基+倍增 // by Isaunoya #include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for ( ...

  4. bzoj4568 [Scoi2016]幸运数字 线性基+树链剖分

    A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游览 A ...

  5. BZOJ 4568 [Scoi2016]幸运数字 ——线性基 倍增

    [题目分析] 考虑异或的最大值,维护线性基就可以了. 但是有多次的询问,树剖或者倍增都可以. 想了想树剖动辄数百行的代码. 算了,我还是写倍增吧. 注:被位运算和大于号的优先级坑了一次,QaQ [代码 ...

  6. P3292 [SCOI2016]幸运数字 线性基

    正解:线性基+倍增 解题报告: 先放下传送门QAQ 然后这题,其实没什么太大的技术含量,,,?就几个知识点套在一起,除了代码长以外没任何意义,主要因为想复习下线性基的题目所以还是写下,,, 随便写下思 ...

  7. 洛谷P3292 [SCOI2016] 幸运数字 [线性基,倍增]

    题目传送门 幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的 ...

  8. [SCOI2016]幸运数字 线性基

    题面 题面 题解 题面意思非常明确:求树上一条链的最大异或和. 我们用倍增的思想. 将这条链分成2部分:x ---> lca , lca ---> y 分别求出这2个部分的线性基,然后合并 ...

  9. BZOJ.4516.[SCOI2016]幸运数字(线性基 点分治)

    题目链接 线性基可以\(O(log^2)\)暴力合并.又是树上路径问题,考虑点分治. 对于每个点i求解 LCA(u,v)==i 时的询问(u,v),只需求出这个点到其它点的线性基后,暴力合并. LCA ...

随机推荐

  1. Tomcat启动和请求处理解析

    tomcat是我们经常使用的组件,但是内部是如何运行的呢,我们去一探究竟. 1.tomcat架构 tomcat的整体架构图如下: Tomcat中只有一个Server,一个Server可以有多个Serv ...

  2. P1642 规划

    题目链接 题意分析 一看就知道是一道\(01\)分数规划的题 我们二分值之后 跑树形背包就可以了 CODE: #include<iostream> #include<cstdio&g ...

  3. Hibernate 连接数据库,数据库返回数据超过限制报错

    1.packet for query is too large 1024 >. you can change this value on the server mysql max_allowed ...

  4. SpringBoot整合JdbcTemplate连接Mysql

    import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProper ...

  5. phpStudy mysql升级至5.7

    1.先停止mysql服务,卸载原来的mysql (其他菜单-->服务器管理--->MySQL--->卸载服务) 2.下载MySQL你需要的版本 ZIP Archive 版本 3.解压 ...

  6. Polycarp Restores Permutation

    http://codeforces.com/contest/1141/problem/C一开始没想法暴力的,next_permutation(),TLE 后来看了这篇https://blog.csdn ...

  7. IP地址转换为Int

    1.转换类 import com.google.common.base.Strings; import java.security.InvalidParameterException; import ...

  8. LINUX 查找替换命令 总结

    find /var/ -name "*.php" > /home/tmp 在/var/目录下查找 所有以.php后缀结尾的文件  结果很多,就 > 输出结果到/home ...

  9. SPSS学习系列之SPSS Statistics(简称SPSS)是什么?

    不多说,直接上干货! IBM SPSS Statistics 为业务经理和分析人员提供解决基本业务和研究问题所需的核心统计过程.该软件提供的工具使用户能够快速查看数据.为其他测试拟定假设情况.执行澄清 ...

  10. 如何调用npm已经安装在全局位置的模块

    参考链接  https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders npm install xxx -g ...