经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
BZOJ4568: [Scoi2016]幸运数字(线性基 倍增)
来源:cnblogs  作者:自为风月马前卒  时间:2018/12/3 17:49:11  对本文有异议

题意

题目链接

Sol

线性基是可以合并的

倍增维护一下

然后就做完了??

喵喵喵?

  1. // luogu-judger-enable-o2
  2. #include<bits/stdc++.h>
  3. #define LL long long
  4. using namespace std;
  5. const int MAXN = 2e4 + 10, B = 60;
  6. inline LL read() {
  7. char c = getchar(); LL x = 0, f = 1;
  8. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  9. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  10. return x * f;
  11. }
  12. int N, Q, g[MAXN][16], dep[MAXN];
  13. LL a[MAXN];
  14. vector<int> v[MAXN];
  15. struct Node {
  16. LL P[62];
  17. Node() {
  18. memset(P, 0, sizeof(P));
  19. }
  20. void insert(LL x) {
  21. for(int i = B; i >= 0; i--) {
  22. if((!(x >> i & 1))) continue;
  23. if(P[i]) {x ^= P[i]; continue;}
  24. P[i] = x; break;
  25. }
  26. }
  27. LL QueryMax() {
  28. LL ans = 0;
  29. for(int i = B; i >= 0; i--) ans = max(ans, ans ^ P[i]);
  30. return ans;
  31. }
  32. Node operator + (const Node &rhs) const {
  33. Node ans;
  34. for(int i = 0; i <= B; i++) ans.P[i] = this -> P[i];
  35. for(int i = 0; i <= B; i++) if(rhs.P[i]) ans.insert(rhs.P[i]);
  36. return ans;
  37. }
  38. }f[MAXN][16];
  39. void dfs(int x, int fa) {
  40. dep[x] = dep[fa] + 1;
  41. g[x][0] = fa;
  42. f[x][0].insert(a[x]); f[x][0].insert(a[fa]);
  43. for(int i = 0; i < v[x].size(); i++) {
  44. int to = v[x][i];
  45. if(to == fa) continue;
  46. dfs(to, x);
  47. }
  48. }
  49. void Pre() {
  50. for(int j = 1; j <= 15; j++)
  51. for(int i = 1; i <= N; i++)
  52. g[i][j] = g[g[i][j - 1]][j - 1],
  53. f[i][j] = f[i][j - 1] + f[g[i][j - 1]][j - 1];
  54. }
  55. LL Query(int x, int y) {
  56. Node base; base.insert(a[x]); base.insert(a[y]);
  57. if(dep[x] < dep[y]) swap(x, y);
  58. for(int i = 15; i >= 0; i--)
  59. if(dep[g[x][i]] >= dep[y])
  60. base = base + f[x][i], x = g[x][i];
  61. if(x == y) return base.QueryMax();
  62. for(int i = 15; i >= 0; i--)
  63. if(g[x][i] != g[y][i]) {
  64. base = base + f[x][i];
  65. base = base + f[y][i];
  66. x = g[x][i], y = g[y][i];
  67. }
  68. base = base + f[x][0]; base = base + f[y][0];
  69. return base.QueryMax();
  70. }
  71. int main() {
  72. #ifndef ONLINE_JUDGE
  73. //freopen("a.in", "r", stdin); freopen("b.out", "w", stdout);
  74. #endif
  75. N = read(); Q = read();
  76. for(int i = 1; i <= N; i++) a[i] = read();
  77. for(int i = 1; i <= N - 1; i++) {
  78. int x = read(), y = read();
  79. v[x].push_back(y); v[y].push_back(x);
  80. }
  81. dfs(1, 0);
  82. Pre();
  83. while(Q--) {
  84. int x = read(), y = read();
  85. printf("%lld\n", Query(x, y));
  86. }
  87. return 0;
  88. }
  89. /*
  90. 8 4
  91. 45654 251 321 54687 321321 654 6321432 5646
  92. 1 2
  93. 2 3
  94. 2 4
  95. 1 5
  96. 4 6
  97. 6 7
  98. 5 8
  99. 7 8
  100. 7 5
  101. 6 8
  102. 4 1
  103. */
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号