题目链接  BZOJ 4568

考虑树链剖分+线段树维护每一段区域的异或线性基

对于每个询问,求出该点集的异或线性基。然后求一下这个线性基里面能异或出的最大值即可。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define ls i << 1
#define rs i << 1 | 1
#define lson i << 1, L, mid
#define rson i << 1 | 1, mid + 1, R typedef long long LL; const int N = 2e4 + 10; struct lb{
LL d[70];
int cnt;
void clear(){
memset(d, 0, sizeof d);
cnt = 0;
}
bool ins(LL val){
dec(i, 62, 0) if (val & (1LL << i)){
if (!d[i]){ d[i] = val; break; }
val ^= d[i];
}
return val > 0;
}
LL qmax(){
LL ret = 0;
dec(i, 62, 0) if ((ret ^ d[i]) > ret) ret ^= d[i];
return ret;
}
LL qmin(){
rep(i, 0, 62) if (d[i]) return d[i];
return 0;
}
}; lb t[N << 3]; int f[N], fp[N], son[N], deep[N], father[N], sz[N], top[N];
int q, tot, n;
LL a[N];
vector <int> v[N]; lb merge(const lb &n1, const lb &n2){
lb ret = n1;
dec(i, 60, 0) if (n2.d[i]) ret.ins(n2.d[i]);
return ret;
} void dfs1(int x, int fa, int dep){
deep[x] = dep;
father[x] = fa;
son[x] = 0;
sz[x] = 1;
int ct = (int)v[x].size();
rep(i, 0, ct - 1){
int u = v[x][i];
if (u == fa) continue;
dfs1(u, x, dep + 1);
sz[x] += sz[u];
if (sz[son[x]] < sz[u]) son[x] = u;
}
} void dfs2(int x, int tp){
top[x] = tp;
f[x] = ++tot;
fp[f[x]] = x;
if (son[x]) dfs2(son[x], tp);
int ct = (int)v[x].size();
rep(i, 0, ct - 1){
int u = v[x][i];
if (u == father[x] || u == son[x]) continue;
dfs2(u, u);
}
} void build(int i, int L, int R){
if (L == R){
t[i].ins(a[fp[L]]);
return;
} int mid = (L + R) >> 1;
build(lson);
build(rson);
t[i] = merge(t[ls], t[rs]);
} lb query(int i, int L, int R, int l, int r){
if (L == l && R == r) return t[i];
int mid = (L + R) >> 1;
if (r <= mid) return query(lson, l, r);
else if (l > mid) return query(rson, l, r);
else return merge(query(lson, l, mid), query(rson, mid + 1, r));
} lb work(int x, int y){
lb ret;
ret.clear();
int f1 = top[x], f2 = top[y];
for (; f1 != f2; ){
if (deep[f1] < deep[f2]) swap(f1, f2), swap(x, y);
ret = merge(ret, query(1, 1, n, f[f1], f[x]));
x = father[f1], f1 = top[x];
} if (x == y) return merge(ret, query(1, 1, n, f[x], f[y]));
if (deep[x] > deep[y]) swap(x, y);
return merge(ret, query(1, 1, n, f[x], f[y]));
} int main(){ scanf("%d%d", &n, &q);
rep(i, 1, n) scanf("%lld", a + i); rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} dfs1(1, 0, 0);
dfs2(1, 1);
build(1, 1, n); while (q--){
int x, y;
scanf("%d%d", &x, &y);
lb cnt = work(x, y);
printf("%lld\n", cnt.qmax());
} return 0;
}

  

BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)的更多相关文章

  1. [SCOI2016]幸运数字 树链剖分,线性基

    [SCOI2016]幸运数字 LG传送门 为了快乐,我们用树剖写这题. 强行树剖,线段树上每个结点维护一个线性基,每次查询暴力合并. 瞎分析一波复杂度:树剖两点之间\(\log n\)条重链,每条重链 ...

  2. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

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

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

  4. bzoj 4568: [Scoi2016]幸运数字

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 848  Solved: 336[Submit][Status ...

  5. bzoj 4568: [Scoi2016]幸运数字【树链剖分+线段树+线性基】

    一眼做法,好处是好想好写坏处是常数大,容易被卡(bzoj loj 洛谷开O2 能AC,不开有90分-- 大概就是树剖之后维护线段树,在线段树的每个节点上上维护一个线性基,暴力\( 60^2 \)的合并 ...

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

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

  7. BZOJ 4568: [Scoi2016]幸运数字(倍增+线性基)

    传送门 解题思路 异或最大值肯定线性基了,树上两点那么就倍增搞一搞,就维护每个点到各级祖先的线性基,时间复杂度\(O(nlog^3n)\),并不知道咋过去的. 代码 #include<iostr ...

  8. 【BZOJ 4568】 4568: [Scoi2016]幸运数字 (线性基+树链剖分+线段树)

    4568: [Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形 ...

  9. bzoj 3531 [Sdoi2014]旅行(树链剖分,线段树)

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 876  Solved: 446[Submit][Status][ ...

随机推荐

  1. 如何用纯 CSS 创作一个文本淡入淡出的 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ERwpeG 可交互视频 ...

  2. 【全面】Linux基础知识和基本操作语句大全(一)

    接触Linux已经有一段时间了,由于实际需要,三三两两地掌握了一些基本语法和实用语句,主要都是在日常开发中用得比较多的,条理不是特别清晰,请见谅!下面开始上硬货!! 基本操作: 关闭Linux系统的命 ...

  3. python-matplotlib-lec0

    直奔主题吧..以下是对matplotlib画图的简单讲解,代码已测试. win7 + pycharm + python 2.7 参考文档: http://old.sebug.net/paper/boo ...

  4. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  5. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...

  6. hbase问题总结

    一.客户端访问hbase时出现no further information 使用java api访问hbase时,一直连不上,查看日志发现以下错误: java.net.ConnectException ...

  7. 虚拟机上的Linux学习

    title: 虚拟机上的Linux学习 date: 2018-08-08 15:48:28 updated: tags: [Linux,学习笔记] description: keywords: com ...

  8. springboot-vue-自定义注解限制接口调用

    新建注解: /** * 想要权限拦截的接口就加上这个注解 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Doc ...

  9. 关于MySQL建表对DML的影响【转】

    本文来自这里 今天一位同学问到线上曾经碰到过连续建表,导致阻塞普通的insert.update等.不过也没有保留现场.因此有疑问为什么建表会影响DML? 分析          首先这个现象不是在所有 ...

  10. STL学习笔记2--list

    List --- 双向列表 List是线性列表结构,数据查找需要一个接一个,不能直接得到元素地址,检索时间与目标元素的位置成正比.但是插入数据比较快,可以在任何位置插入数据或者删除数据.list特点是 ...