题目链接

loj

luogu

题意

求树上路径最大点权异或和

自然想到(维护树上路径)+ (维护最大异或和)

那么有三种方法可以选择

1.树剖+线性基

2.倍增+线性基

3.点分治+线性基

至于线性基的合并

一共就是long级的

暴力合并就好啦

这是一份在loj T掉在洛谷AC的可怜代码

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int N = 2e4 + 2;
const int LIM = 60; inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
} struct Edge{
int v, next;
}edge[N << 1];
int esize, head[N];
inline void addedge(int x, int y){
edge[++esize] = (Edge){y, head[x]};
head[x] = esize;
} struct XXJ{
long long w[LIM + 2];
XXJ(){
memset(w, 0, sizeof(w));
}
void ins(long long x){
for(int i = LIM; ~i; --i){
if(!(x >> i)) continue;
else if(!w[i]){w[i] = x; break;}
x ^= w[i];
}
}
void print(){
long long res = 0;
for(int i = LIM; ~i; --i)
if((res ^ w[i]) > res) res ^= w[i];
printf("%lld\n", res);
}
}px[N][18];
long long w[N];
int p[N][18], dep[N];
int n, m; XXJ merge(XXJ x, XXJ y){
for(int i = LIM; i >= 0; --i){
if(y.w[i]) x.ins(y.w[i]);
}
return x;
} XXJ LCA(int x, int y){
XXJ res;
res.ins(w[x]); res.ins(w[y]);
if(dep[x] < dep[y]) swap(x, y);
for(int i = 15; i >= 0; --i)
if(dep[x] - (1 << i) >= dep[y]){
res = merge(res, px[x][i]);
x = p[x][i];
}
if(x == y) return res;
for(int i = 15; i >= 0; --i){
if(p[x][i] != p[y][i]){
res = merge(res, px[x][i]);
res = merge(res, px[y][i]);
x = p[x][i], y = p[y][i];
}
}
res = merge(res, px[x][0]);
return res;
} void dfs(int x, int ff){
dep[x] = dep[ff] + 1;
p[x][0] = ff;
px[x][0].ins(w[ff]);
for(int i = head[x], vv; ~i; i = edge[i].next){
vv = edge[i].v; if(vv == ff) continue;
dfs(vv, x);
}
} void calc(){
for(int i = 1; i <= 15; ++i){
for(int j = 1; j <= n; ++j){
p[j][i] = p[p[j][i - 1]][i - 1];
px[j][i] = merge(px[j][i - 1], px[p[j][i - 1]][i - 1]);
}
}
} int main() {
memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i){
scanf("%lld", &w[i]);
}
for(int i = 1, x, y; i < n; ++i){
scanf("%d%d", &x, &y);
addedge(x, y); addedge(y, x);
}
dfs(1, 0);
calc();
for(int i = 1, x, y; i <= m; ++i){
x = read(), y = read();
XXJ res = LCA(x, y);
res.print();
}
return 0;
}

SCOI2016幸运数字(树剖/倍增/点分治+线性基)的更多相关文章

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

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

  2. [BZOJ4568][SCOI2016]幸运数字(倍增LCA,点分治+线性基)

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2131  Solved: 865[Submit][Statu ...

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

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

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

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

  5. 【线性基合并 树链剖分】bzoj4568: [Scoi2016]幸运数字

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

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

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

  7. [BZOJ4568][Scoi2016]幸运数字 倍增+线性基

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1791  Solved: 685[Submit][Statu ...

  8. 【BZOJ4568】[Scoi2016]幸运数字 倍增+线性基

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

  9. [SCOI2016]幸运数字(线性基,倍增)

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

随机推荐

  1. Linux—vim常用命令

    vim常用命令: 1. 键入i进入编辑模式2. esc进入命令模式3. a,进入编辑模式3. b,光标移动到单词前,end,光标移动到行尾4. home光标移动到行首5. cc,删除当前行,并进入编辑 ...

  2. c++入门之命名空间存在的意义

    看过鸡啄米的C++编程入门系列教程的朋友,应该能注意到,在其中的很多实例中,都有这么一条语句:using namespace std;,即使用命名空间std,其作用就是规定该文件中使用的标准库函数都是 ...

  3. 牛客练习赛 A题 筱玛的快乐

    链接:https://ac.nowcoder.com/acm/contest/342/A来源:牛客网 筱玛的快乐 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语 ...

  4. 提取PPT文件中的Vba ProjectStg Compressed Atom。Extract PPT VBA Compress Stream

    http://msdn.microsoft.com/en-us/library/cc313106(v=office.12).aspx  微软文档 PartI ********************* ...

  5. virtualization - Ubuntu Budgie screen distortion in Hyper-V - Ask Ubuntu

    virtualization - Ubuntu Budgie screen distortion in Hyper-V - Ask Ubuntuhttps://askubuntu.com/questi ...

  6. bootstrap 弹窗或者提示框插件 bootstrap-growl 和bootstrap-notify

    Bootstrap简单好用的页面右上角咆哮提示框 - daidaineteasy的专栏 - CSDN博客https://blog.csdn.net/daidaineteasy/article/deta ...

  7. if判断条件注意!!!

    if(condition){ console.log(condition为true才执行): } 实际上会对condition执行Boolean()转型函数,将其转换成布尔值

  8. CLOUD常用表

    采购采购订单(t_PUR_POOrder, t_PUR_POOrderEntry)-收料通知单(T_PUR_Receive,T_PUR_ReceiveEntry)-采购入库单(T_STK_INSTOC ...

  9. leetcode资料整理

    注:借鉴了 http://m.blog.csdn.net/blog/lsg32/18712353 在Github上提供leetcode有: 1.https://github.com/soulmachi ...

  10. Java8 flatMap的sample

    外国人写得, 很不错 http://www.java67.com/2016/03/how-to-use-flatmap-in-java-8-stream.html package test; impo ...