4568: [Scoi2016]幸运数字
Description
A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一。每座城市都有一个
幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征。一些旅行者希望游览 A 国。旅行者计划
乘飞机降落在 x 号城市,沿着 x 号城市到 y 号城市之间那条唯一的路径游览,最终从 y 城市起飞离开 A 国。
在经过每一座城市时,游览者就会有机会与这座城市的幸运数字拍照,从而将这份幸运保存到自己身上。然而,幸
运是不能简单叠加的,这一点游览者也十分清楚。他们迷信着幸运数字是以异或的方式保留在自己身上的。例如,
游览者拍了 3 张照片,幸运值分别是 5,7,11,那么最终保留在自己身上的幸运值就是 9(5 xor 7 xor 11)。
有些聪明的游览者发现,只要选择性地进行拍照,便能获得更大的幸运值。例如在上述三个幸运值中,只选择 5
和 11 ,可以保留的幸运值为 14 。现在,一些游览者找到了聪明的你,希望你帮他们计算出在他们的行程安排中
可以保留的最大幸运值是多少。
Input
第一行包含 2 个正整数 n ,q,分别表示城市的数量和旅行者数量。第二行包含 n 个非负整数,其中第 i 个整
数 Gi 表示 i 号城市的幸运值。随后 n-1 行,每行包含两个正整数 x ,y,表示 x 号城市和 y 号城市之间有一
条道路相连。随后 q 行,每行包含两个正整数 x ,y,表示这名旅行者的旅行计划是从 x 号城市到 y 号城市。N
<=20000,Q<=200000,Gi<=2^60
Output
输出需要包含 q 行,每行包含 1 个非负整数,表示这名旅行者可以保留的最大幸运值。
Sample Input
4 2
11 5 7 9
1 2
1 3
1 4
2 3
1 4
11 5 7 9
1 2
1 3
1 4
2 3
1 4
Sample Output
14
11
11
解题思路:17西安现场赛有个简化版…求区间最大异或和。这题是树上的,所以套个树链剖分就好了。难点还是怎么维护区间最大异或和,实际上只要知道什么是线性基。就知道怎么用线段树去维护了!……很简单……这份代码跑了27s。
#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long int ll;
const int MAXN=20005;
inline void scan_d(ll &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0'), c = getchar();
}
}
inline void scan_d(int &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0'), c = getchar();
}
}
struct Edge{
int to;
int next;
}e[MAXN<<1];
int head[MAXN],edge_num;
void insert_edge(int u,int v){
e[edge_num].to=v;
e[edge_num].next=head[u];
head[u]=edge_num++;
}
struct XXJS{
ll c[60];
};
void insert_vector(XXJS &a,ll x){
for(int i=59;i>=0;i--){
if(x&(1ll<<i)){
if(!a.c[i]){
a.c[i]=x;
break;
}
else
x^=a.c[i];
}
}
}
void merge_XXJ(XXJS &a,XXJS &b,XXJS& c){
XXJS d;
for(int i=59;i>=0;i--)d.c[i]=b.c[i];
for(int i=59;i>=0;i--)if(c.c[i])insert_vector(d,c.c[i]);
for(int i=59;i>=0;i--)a.c[i]=d.c[i];
}
int top[MAXN];
int fa[MAXN];
int deep[MAXN];
int size[MAXN];
int pos[MAXN];
int son[MAXN];
int SEG;
ll a[MAXN];
ll A[MAXN];
XXJS XXJ[MAXN<<2];
int n,q;
void init(){
edge_num=0;
memset(head,-1,sizeof(head));
SEG=1;
memset(son,-1,sizeof(son));
}
void dfs1(int u,int pre,int d){
deep[u]=d;
fa[u]=pre;
size[u]=1;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(v!=pre){
dfs1(v,u,d+1);
size[u]+=size[v];
if(son[u]==-1||size[v]>size[son[u]])
son[u]=v;
}
}
}
void dfs2(int u,int sp){
top[u]=sp;
pos[u]=SEG++;
A[pos[u]]=a[u];
if(son[u]!=-1)
dfs2(son[u],sp);
else
return;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(v!=son[u]&&v!=fa[u])
dfs2(v,v);
}
}
void push_up(int rt){
merge_XXJ(XXJ[rt],XXJ[rt<<1],XXJ[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l==r){
insert_vector(XXJ[rt],A[l]);
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
push_up(rt);
}
XXJS ANS;
void query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
merge_XXJ(ANS,ANS,XXJ[rt]);
return;
}
int m=(l+r)>>1;
if(L<=m)
query(L,R,l,m,rt<<1);
if(R>m)
query(L,R,m+1,r,rt<<1|1);
}
void query(int u,int v){
int f1=top[u],f2=top[v];
int temp=0;
while(f1!=f2){
if(deep[f1]<deep[f2]){
swap(f1,f2);
swap(u,v);
}
query(pos[f1],pos[u],1,n,1);
u=fa[f1];
f1=top[u];
}
if(deep[u]>deep[v])
swap(u,v);
query(pos[u],pos[v],1,n,1);
}
ll ans;
int main(){
scan_d(n);
scan_d(q);
for(int i=1;i<=n;i++)
scan_d(a[i]);
init();
int qq,ww;
for(int i=0;i<n-1;i++){
scan_d(qq);
scan_d(ww);
insert_edge(qq,ww);
insert_edge(ww,qq);
}
dfs1(1,0,0);
dfs2(1,1);
build(1,n,1);
for(int i=0;i<q;i++){
scan_d(qq);
scan_d(ww);
memset(ANS.c,0,sizeof(ANS.c));
query(qq,ww);
ans=0;
for(int i=59;i>=0;i--)
if((ans^ANS.c[i])>ans)
ans=ans^ANS.c[i];
printf("%lld\n",ans);
}
return 0;
}