BZOJ 4568: [Scoi2016]幸运数字(倍增+线性基)
解题思路
异或最大值肯定线性基了,树上两点那么就倍增搞一搞,就维护每个点到各级祖先的线性基,时间复杂度$O(nlog^3n)$,并不知道咋过去的。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=20005;
template<class T> void rd(T &x){
x=0; char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
}
int n,q,head[N],cnt,to[N<<1],nxt[N<<1],f[N][20],dep[N];
LL w[N];
struct Base{
LL a[62];
inline void insert(LL x){
if(!x) return ;
for(int i=60;~i;i--)
if((1ll<<i)&x){
if(!a[i]) {a[i]=x; break;}
x^=a[i]; if(!x) break;
}
}
inline void clear() {memset(a,0,sizeof(a));}
inline LL query(){
LL ret=0;
for(int i=60;~i;i--)
if(a[i] && (ret^a[i])>ret) ret^=a[i];
return ret;
}
}b[N][17],ans;
inline void add(int bg,int ed){
to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
}
inline void build(Base &zz,Base B){
for(int i=60;~i;i--)
if(B.a[i]) zz.insert(B.a[i]);
}
void dfs(int x,int F){
f[x][0]=F; b[x][0].insert(w[x]);
for(int i=1;i<=15;i++) {
f[x][i]=f[f[x][i-1]][i-1];
if(f[x][i]) b[x][i]=b[x][i-1],build(b[x][i],b[f[x][i-1]][i-1]);
}
for(int i=head[x];i;i=nxt[i]){
int u=to[i]; if(u==F) continue;
dep[u]=dep[x]+1; dfs(u,x);
}
}
inline int LCA(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
for(int i=15;~i;i--)
if(dep[f[x][i]]>=dep[y]) x=f[x][i];
if(x==y) return x;
for(int i=15;~i;i--)
if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
inline void solve(int x,int y){
int lca=LCA(x,y); ans.clear();
for(int i=15;~i;i--)
if(dep[f[x][i]]>=dep[lca]) {
build(ans,b[x][i]);
x=f[x][i];
}
for(int i=15;~i;i--)
if(dep[f[y][i]]>=dep[lca]){
build(ans,b[y][i]);
y=f[y][i];
}
ans.insert(w[lca]);
printf("%lld\n",ans.query());
}
int main(){
rd(n); rd(q); int x,y;
for(int i=1;i<=n;i++) rd(w[i]);
for(int i=1;i<n;i++){
rd(x); rd(y);
add(x,y); add(y,x);
}
dep[1]=1; dfs(1,0);
while(q--){
rd(x),rd(y);
solve(x,y);
}
return 0;
}
BZOJ 4568: [Scoi2016]幸运数字(倍增+线性基)的更多相关文章
- 【BZOJ 4568】 4568: [Scoi2016]幸运数字 (线性基+树链剖分+线段树)
4568: [Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形 ...
- [BZOJ4568][Scoi2016]幸运数字 倍增+线性基
4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1791 Solved: 685[Submit][Statu ...
- 【BZOJ4568】[Scoi2016]幸运数字 倍增+线性基
[BZOJ4568][Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念 ...
- BZOJ 4568: [Scoi2016]幸运数字 [线性基 倍增]
4568: [Scoi2016]幸运数字 题意:一颗带点权的树,求树上两点间异或值最大子集的异或值 显然要用线性基 可以用倍增的思想,维护每个点向上\(2^j\)个祖先这些点的线性基,求lca的时候合 ...
- bzoj4568: [Scoi2016]幸运数字(LCA+线性基)
4568: [Scoi2016]幸运数字 题目:传送门 题解: 好题!!! 之前就看过,当时说是要用线性基...就没学 填坑填坑: %%%线性基 && 神犇 主要还是对于线性基的运用和 ...
- bzoj 4568: [Scoi2016]幸运数字
4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 848 Solved: 336[Submit][Status ...
- [SCOI2016]幸运数字(线性基,倍增)
[SCOI2016]幸运数字 题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作 ...
- BZOJ 4568 [Scoi2016]幸运数字 ——线性基 倍增
[题目分析] 考虑异或的最大值,维护线性基就可以了. 但是有多次的询问,树剖或者倍增都可以. 想了想树剖动辄数百行的代码. 算了,我还是写倍增吧. 注:被位运算和大于号的优先级坑了一次,QaQ [代码 ...
- BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)
题目链接 BZOJ 4568 考虑树链剖分+线段树维护每一段区域的异或线性基 对于每个询问,求出该点集的异或线性基.然后求一下这个线性基里面能异或出的最大值即可. #include <bits ...
随机推荐
- get the deadlock information from sql server
https://stackoverflow.com/questions/12422986/sql-query-to-get-the-deadlocks-in-sql-server-2008 You c ...
- java构造器内部多态方法
public class TestC { public static void main(String []args) { new Graph(5); }}class Grp{ void draw() ...
- How to show out three rows from the same databand On A4?
How to show out three rows from the same databand On A4? Quote Post by DoraHuang » Tue Mar 13, 2018 ...
- 获取header中content-type的值
后台传过来的值需要根据content-Type的值来判定成功与否 获取header中content-Tyep的值 用res.header['Content-Type']
- [Codeforces 729F] Financiers Game
题意: 两个人分别从长度为n的数列的两端开始取数,如果前一 个人取了k个数,后一个人必须取k或k+1个. 第一个人最 开始可以取1个或2个,不能操作时结束. 两个人都希望自 己取到的数字之和尽量大,并 ...
- 面试题30:包含min函数的栈
思路: 1.首先将栈的基本结构写出 #初始化栈的写法 def __init__(self): self.stack = [] #栈的压入 (加self是实例化,如果前面加入静态装饰器啥的,就不需要 ...
- Ascii Chart
Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex -------------------------- ...
- Pikachu漏洞练习平台实验——暴力破解(一)
概述 一个有效的字典可以大大提高暴力破解的效率 比如常用的用户名/密码TOP500 脱裤后的账号密码(社工库) 根据特定的对象(比如手机.生日和银行卡号等)按照指定的规则来生成密码 暴力破解流程 确认 ...
- luoguP1314 聪明的质监员 题解(NOIP2011)
P1314 聪明的质监员 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include< ...
- 七、hibernate的事务使用
hibernate中事务隔离级别 1:读未提交 2:读已提交 4:可重复读 8:可串行化 hibernate事务使用 在核心配置文件中配置事务隔离级别 <property name=" ...