题意

题目链接

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. Spring Boot 入门系列

    本系列博文版权 简书 面皮大师 所有,转载请标明原文及出处: http://www.jianshu.com/u/062bd8f1299c 项目地址: https://github.com/daleiw ...

  2. leetcode-292-Nim Game(搬石子)

    题目描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

  3. Python3 操作系统与路径 模块(os / os.path / pathlib)

    #!/usr/bin/env python # coding=utf-8 __author__ = 'Luzhuo' __date__ = '2017/5/7' import os def os_de ...

  4. python学习,day4:生成器,通过yield实现单线程情况下的并发运算

    首先了解一个斐波那契函数的实现,了解下生成器的工作流程 # coding=utf-8 # Author: RyAn Bi def fib(max): n,a,b=0,0,1 while n < ...

  5. UVA_11922 Permutation Transformer 【splay树】

    一.题目 UVA11922 二.分析 为什么会有伸展树? 伸展树与AVL的区别除了保持平衡的方式不同外,最重要的是在每次查找点时,让该点旋转到根结点,这里可以结合计算机里的局部性原理思考. 伸展树有什 ...

  6. [LibreOJ #2341]【WC2018】即时战略【交互】【LCT】

    Description 有一棵n个点的结构未知的树,初始时只有1号点是已被访问的. 你可以调用交互库的询问函数explore(x,y),其中x是已访问的点,y是任意点. 它会返回x向y方向走第一步的点 ...

  7. Mac 10.12安装Homebrew图形化界面管理工具Cakebrew

    下载: (链接: https://pan.baidu.com/s/1mivJ9H2 密码: f8dr)

  8. 我3年前开发的IM即时通讯一直没勇气推出,现在智能时代了,有什么可以结合的地方吗?

    我3年前开发的IM即时通讯一直没勇气推出,现在智能时代了,有什么可以结合的地方吗? 服务端采用基于XMPP协议的Openfire(当然改造了很多,也添加了很多握手协议) 客户端有做了四个版本:一个Ja ...

  9. 连接Git@OSC操作步骤

    一.准备工作 软件下载 Git:地址 TortoiseGit:地址 二.安装与配置 1.Git安装 Git配置 设置客户端的用户名和email 然后,到Git@OSC 上面注册一个帐号. 这个帐号就是 ...

  10. MySQL 备份数据库

    一.数据备份 1.备份一个数据库 mysqldump基本语法: mysqldump -u username -p dbname table1 table2 ...-> BackupName.sq ...