题意

线性基套上树上倍增即可,注意边界。

code:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn=20010;
  5. int n,m,cnt,t;
  6. int head[maxn],dep[maxn];
  7. int f[maxn][20];
  8. ll a[maxn];
  9. struct edge{int to,nxt;}e[maxn<<1];
  10. struct Xord
  11. {
  12. ll d[65];
  13. Xord(){memset(d,0,sizeof(d));}
  14. inline void insert(ll x)
  15. {
  16. for(int i=60;~i;i--)
  17. {
  18. if(!(x&(1ll<<i)))continue;
  19. if(!d[i]){d[i]=x;return;}
  20. else x^=d[i];
  21. }
  22. }
  23. inline ll query()
  24. {
  25. ll res=0;
  26. for(int i=60;~i;i--)res=max(res,res^d[i]);
  27. return res;
  28. }
  29. }xord[maxn][20];
  30. inline void add(int u,int v)
  31. {
  32. e[++cnt].nxt=head[u];
  33. head[u]=cnt;
  34. e[cnt].to=v;
  35. }
  36. void dfs(int x,int fa)
  37. {
  38. dep[x]=dep[fa]+1;
  39. for(int i=1;i<=t;i++)
  40. {
  41. f[x][i]=f[f[x][i-1]][i-1];
  42. for(int j=0;j<=60;j++)xord[x][i].d[j]=xord[x][i-1].d[j];
  43. for(int j=0;j<=60;j++)if(xord[f[x][i-1]][i-1].d[j])xord[x][i].insert(xord[f[x][i-1]][i-1].d[j]);
  44. }
  45. for(int i=head[x];i;i=e[i].nxt)
  46. {
  47. int y=e[i].to;
  48. if(y==fa)continue;
  49. f[y][0]=x;dfs(y,x);
  50. }
  51. }
  52. inline ll query(int x,int y)
  53. {
  54. Xord res;
  55. if(dep[x]>dep[y])swap(x,y);
  56. for(int i=t;~i;i--)
  57. if(dep[f[y][i]]>=dep[x])
  58. {
  59. for(int j=0;j<=60;j++)if(xord[y][i].d[j])res.insert(xord[y][i].d[j]);
  60. y=f[y][i];
  61. }
  62. if(x==y)
  63. {
  64. res.insert(a[x]);
  65. return res.query();
  66. }
  67. for(int i=t;~i;i--)
  68. if(f[x][i]!=f[y][i])
  69. {
  70. for(int j=0;j<=60;j++)if(xord[x][i].d[j])res.insert(xord[x][i].d[j]);
  71. for(int j=0;j<=60;j++)if(xord[y][i].d[j])res.insert(xord[y][i].d[j]);
  72. x=f[x][i],y=f[y][i];
  73. }
  74. res.insert(a[x]),res.insert(a[y]),res.insert(a[f[x][0]]);
  75. return res.query();
  76. }
  77. int main()
  78. {
  79. //freopen("test.in","r",stdin);
  80. //freopen("test.out","w",stdout);
  81. scanf("%d%d",&n,&m);t=(int)log2(n)+1;
  82. for(int i=1;i<=n;i++)scanf("%lld",&a[i]),xord[i][0].insert(a[i]);
  83. for(int i=1;i<n;i++)
  84. {
  85. int u,v;scanf("%d%d",&u,&v);
  86. add(u,v),add(v,u);
  87. }
  88. dfs(1,0);
  89. for(int i=1;i<=m;i++)
  90. {
  91. int x,y;scanf("%d%d",&x,&y);
  92. printf("%lld\n",query(x,y));
  93. }
  94. return 0;
  95. }

luoguP3292 [SCOI2016]幸运数字(倍增做法)的更多相关文章

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

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

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

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

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

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

  4. luoguP3292 [SCOI2016]幸运数字(点分治做法)

    题意 考虑点分治,每次处理过重心的询问(即两点在重心的不同子树中). 求出每个点到重心的线性基,之后对过重心的询问合并两点线性基求解. code: #include<bits/stdc++.h& ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 面向对象程序设计(JAVA) 第12周学习指导及要求

    2019面向对象程序设计(Java)第12周学习指导及要求 (2019.11.15-2019.11.18)   学习目标 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API ...

  2. 小程序-API请求

    Page({ onLoad:function(){ // 在onLoad中调用发送请求的函数 this.getProList(); } getProList:function(){ var self= ...

  3. webpack打包优化实践

    事情缘由 近段时间在做基于scratch3.0的改造项目.基于scratch-gui改造,项目本身已经很大了,然后里面还要用到scratch-blocks,scratch-vm,scratch-ren ...

  4. 新书上线:《Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统》,欢迎大家买回去垫椅子垫桌脚

    新书上线 大家好,笔者的新书<Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统>已上线,此书内容充实.材质优良,乃家中必备垫桌脚 ...

  5. 洛谷 P5686 [CSP-SJX2019]和积和

    传送门 思路 应用多个前缀和推出式子即可 \(30pts\): 首先如果暴力算的话很简单,直接套三层循环就好了(真的是三层!!最后两个\(sigma\)一起算就好了) \[\sum_{l = 1}^{ ...

  6. JavaScript中如何判断数组类型

    前言 JavaScript中关于数组的判定问题,一直都是一个必须要掌握的点,那么,运用知识,如何判断一个类型是数组,就需要有对JavaScript使用有着深入的了解. 判断方法 一.Array.isA ...

  7. ASP.NET Core 获取主机名时的 "Decoded string is not a valid IDN name" 错误

    在 ASP.NET Core 中通过 Request.Host.Host 获取主机名(hostname)时,如果主机名中包含非 ASCII 字符(比如 puny code),就会引发下面的异常: Sy ...

  8. 暑期班--JAVA无敌课程---第一天-Day01-----Java基础

    1.Java发展历史 1.1Games Golsing Java创始人 2.What is JDK 3.记本本开发第一个Java程序 巴拉巴拉 巴拉巴拉 巴拉巴拉 巴拉巴拉 巴拉巴拉 巴拉巴拉 巴拉巴 ...

  9. Protractor - 怎样运行

    前一篇设置好了Protractor基本运行环境,那怎样运行Protractor呢? 要运行我们的测试脚本,至少需要配置好两个文件: ---Package.json ---conf.js Package ...

  10. laravel使用Dingo\Api通过response()->json()返回空对象

    laravel使用Dingo\Api写接口跟android对接时,android一直反应解析错误,无法解析数据. { "status_code":200, "messag ...