题目描述
A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一。每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征。
一些旅行者希望游览 A 国。旅行者计划乘飞机降落在 x 号城市,沿着 x 号城市到 y 号城市之间那条唯一的路径游览,最终从 y 城市起飞离开 A 国。在经过每一座城市时,游览者就会有机会与这座城市的幸运数字拍照,从而将这份幸运保存到自己身上。然而,幸运是不能简单叠加的,这一点游览者也十分清楚。他们迷信着幸运数字是以异或的方式保留在自己身上的。
例如,游览者拍了 3 张照片,幸运值分别是 5,7,11,那么最终保留在自己身上的幸运值就是 9(5 xor 7 xor 11)。
有些聪明的游览者发现,只要选择性地进行拍照,便能获得更大的幸运值。例如在上述三个幸运值中,只选择 5 和 11 ,可以保留的幸运值为 14 。现在,一些游览者找到了聪明的你,希望你帮他们计算出在他们的行程安排中可以保留的最大幸运值是多少。
输入输出格式
输入格式:
第一行包含 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
输出格式:
输出需要包含 q 行,每行包含 1 个非负整数,表示这名旅行者可以保留的最大幸运值。
题意:
一棵树,节点有权值,求出一个节点到另一个节点的路径上的异或最大值。
题解:
①要求出多个数异或max还是用线性基好一点,用倍增加速线性基;
②先倍增预处理lca,同时预处理出lca移动路径上的节点线性基;
③查询先求出lca,然后像这样,类似于rmq的思想,暴力合并四个线性基:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARoAAAFDCAYAAADlMsGFAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABvmSURBVHhe7d19jB3VecfxsV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yZo2uPO2iNLjk44Ulm5OzT1hg/iRJ9h+afta/LiNo0HluhaExxAl5TRkS+TNGYlR4SPWyfc+b5l6SLDlxsbnVfQQNeqfOsPFfq+iwyR0S/e4x882t8d7av8PcSpI7z77a3Oo+gga94AeARmVTNGSEOyRy+yxlqpS+9GcEQYPeqhI28ty6wqpolbLp1cfMrf4haNAbWRVHSFhkPadMNTNKXpUizeO7XnzA3OtXf0awYA+9Mypc8gIj5DlFSJXizjTlvda5j39lWuUjM1B9GjoRNOitqkOfOqoYN0CkSlm38Lr0ts/9t8p2g65uNcjD0Am9VSUo6ggZUaQ/I8MmV99CRlDRYDDGVTh1hYvL/TuLDps0/h1NI2gARW7QnDTvg8mq0y+ZdqoHqWb+csst5l4/h02CoAEUnfno0lJbCfpYzQh6NICiVQtmXpguj1QzfUVFA0Sw4aWHp62T8fV1yGQRNADUMXQCoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoI6gAaCOoAGgjqABoCxJ/h9mNrbr5U6V/AAAAABJRU5ErkJggg==" alt="" />用一些位运算技巧可以算出每个数的最高二进制有效位数和线性基的sz(是否满)可以优化掉很多常数,但这样好像已经可以过了。
#include<cstdio>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
const int N = ;
int n,m,f[N][],hd[N],o,lg[N],p[],lwt[N],dep[N];
ll w[N];
struct Edge{int v,nt;}E[*N];
int cal(ll x){
int ret=;
for(int i=;i;i>>=)if(x>>i)x>>=i,ret|=i;
return ret;
}
struct basis{
ll d[];
void ins(ll x){
for(int i = cal(x);i >= ;i--)
if((x>>i)&){
if(!d[i]) {d[i] = x;break;}
else x^=d[i];
}
}
ll mx(){
ll ret = ;
for(int i = ;i >= ;i--) if(d[i]) ret = max(ret,ret^d[i]);
return ret;
}
void merge(basis a){
for(int i = ;i >= ;i--) if(a.d[i]) ins(a.d[i]);
}
}Bs[N][],tmp;
char gc(){
static char *p1,*p2,s[];
if(p1==p2) p2=(p1=s)+fread(s,,,stdin);
return(p1==p2)?EOF:*p1++;
}
ll rd(){
ll x = ; char c = gc();
while(c<''||c>'') c = gc();
while(c>=''&&c<='') x=x*+c-'',c = gc();
return x;
}
void adde(int u,int v){
E[o] = (Edge){v,hd[u]}; hd[u] = o++;
E[o] = (Edge){u,hd[v]}; hd[v] = o++;
}
void dfs(int u,int fa){
dep[u]=dep[fa]+; f[u][] = fa; Bs[u][].ins(w[fa]);
for(int i = ;i <= && p[i]<dep[u];i++){
f[u][i] = f[f[u][i-]][i-];
Bs[u][i] = Bs[u][i-];
Bs[u][i].merge(Bs[f[u][i-]][i-]);
}
for(int i = hd[u],v;i!=-;i=E[i].nt)if((v=E[i].v)!=fa) dfs(v,u);
}
int get(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
for(int i = dep[u]-dep[v];i;i-=lwt[i]) u = f[u][lg[lwt[i]]];
if(u==v) return u;
for(int i = ;i >= ;i--)if(f[u][i]!=f[v][i]){u = f[u][i]; v = f[v][i];}
return f[u][];
}
void query(int u,int v){
int lca = get(u,v);
memset(tmp.d,,sizeof(tmp.d));
tmp.ins(w[u]),tmp.ins(w[v]);
if(u!=lca){
int lu = dep[u] - dep[lca];
tmp.merge(Bs[u][lg[lu]]);
for(int i = lu-p[lg[lu]];i;i-=lwt[i]) u = f[u][lg[lwt[i]]];
tmp.merge(Bs[u][lg[lu]]);
}
if(v!=lca){
int lv = dep[v] - dep[lca];
tmp.merge(Bs[v][lg[lv]]);
for(int i = lv-p[lg[lv]];i;i-=lwt[i]) v = f[v][lg[lwt[i]]];
tmp.merge(Bs[v][lg[lv]]);
}
ll ans = tmp.mx();
printf("%lld\n",ans);
}
int main()
{ freopen("bzoj4568.in","r",stdin);
freopen("bzoj4568.out","w",stdout);
n = rd(); m = rd();
lg[] = -;
for(int i = ;i <= n;i++) w[i]=rd(),hd[i] = -,lg[i]=lg[i>>]+,lwt[i]=i&(-i);
for(int i = p[] = ;i <= ;i++) p[i] = p[i-]*;
for(int i = ;i < n;i++) adde(rd(),rd());
dfs(,);
for(int i = ;i <= m;i++) {
int u = rd(),v = rd();
query(u,v);
//printf("%d\n",i);
}
//printf("haha\n");
return ;
}//by tkys_Austin;