题意:给定一个非负整数序列{a},初始长度为N。

有M个操作,有以下两种操作类型:

1、Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1。

2、Qlrx:询问操作,你需要找到一个位置p,满足l<=p<=r,使得:

a[p] xor a[p+1] xor ... xor a[N] xor x 最大,输出最大是多少。

题解:可持久化trie

用前缀异或来建树,查询就变成了last^x和l到r中a【p】异或最大值是多少

先插入一个0,然后像可持久化线段树那样建树即可,还是挺简单的

  1. /**************************************************************
  2. Problem: 3261
  3. User: walfy
  4. Language: C++
  5. Result: Accepted
  6. Time:4584 ms
  7. Memory:179416 kb
  8. ****************************************************************/
  9. //#pragma GCC optimize(2)
  10. //#pragma GCC optimize(3)
  11. //#pragma GCC optimize(4)
  12. //#pragma GCC optimize("unroll-loops")
  13. //#pragma comment(linker, "/stack:200000000")
  14. //#pragma GCC optimize("Ofast,no-stack-protector")
  15. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  16. #include<bits/stdc++.h>
  17. #define fi first
  18. #define se second
  19. #define db double
  20. #define mp make_pair
  21. #define pb push_back
  22. #define pi acos(-1.0)
  23. #define ll long long
  24. #define vi vector<int>
  25. #define mod 998244353
  26. #define ld long double
  27. #define C 0.5772156649
  28. #define ls l,m,rt<<1
  29. #define rs m+1,r,rt<<1|1
  30. #define pll pair<ll,ll>
  31. #define pil pair<int,ll>
  32. #define pli pair<ll,int>
  33. #define pii pair<int,int>
  34. //#define cd complex<double>
  35. #define ull unsigned long long
  36. #define base 1000000000000000000
  37. #define Max(a,b) ((a)>(b)?(a):(b))
  38. #define Min(a,b) ((a)<(b)?(a):(b))
  39. #define fin freopen("a.txt","r",stdin)
  40. #define fout freopen("a.txt","w",stdout)
  41. #define fio ios::sync_with_stdio(false);cin.tie(0)
  42. template<typename T>
  43. inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
  44. template<typename T>
  45. inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
  46. inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
  47. inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
  48. inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  49. inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
  50. inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
  51. using namespace std;
  52. const double eps=1e-8;
  53. const ll INF=0x3f3f3f3f3f3f3f3f;
  54. const int N=600000+10,maxn=100000+10,inf=0x3f3f3f3f;
  55. struct trie{
  56. int sz,cnt,root[N],ch[N*25][2],num[N*25];
  57. void init()
  58. {
  59. sz=cnt=0;
  60. root[++sz]=++cnt;
  61. int now=root[sz];
  62. for(int i=24;i>=0;i--)
  63. {
  64. ch[now][0]=++cnt;
  65. now=ch[now][0];
  66. num[now]++;
  67. }
  68. }
  69. void add(int x)
  70. {
  71. root[++sz]=++cnt;
  72. int now=root[sz],last=root[sz-1];
  73. for(int i=24;i>=0;i--)
  74. {
  75. ch[now][(x>>i)&1]=++cnt;
  76. ch[now][!((x>>i)&1)]=ch[last][!((x>>i)&1)];
  77. now=ch[now][(x>>i)&1];
  78. num[now]=num[ch[last][(x>>i)&1]]+1;
  79. last=ch[last][(x>>i)&1];
  80. }
  81. }
  82. void query(int l,int r,int x)
  83. {
  84. int now=root[r],last=root[l],ans=0;
  85. for(int i=24;i>=0;i--)
  86. {
  87. // if(now==128)printf("%d %d %d %d\n",now,last,num[ch[now][!((x>>i)&1)]],num[ch[last][!((x>>i)&1)]]);
  88. if(num[ch[now][!((x>>i)&1)]]>num[ch[last][!((x>>i)&1)]])
  89. {
  90. now=ch[now][!((x>>i)&1)];
  91. last=ch[last][!((x>>i)&1)];
  92. ans+=(1<<i);
  93. // printf("%d %d\n",now,0);
  94. }
  95. else
  96. {
  97. now=ch[now][(x>>i)&1];//,printf("%d %d\n",now,1);
  98. last=ch[last][(x>>i)&1];
  99. }
  100. // printf("%d--\n",now);
  101. }
  102. printf("%d\n",ans);
  103. }
  104. void debug(int now)
  105. {
  106. printf("%d %d %d %d\n",now,num[now],ch[now][0],ch[now][1]);
  107. if(ch[now][0])debug(ch[now][0]);
  108. if(ch[now][1])debug(ch[now][1]);
  109. }
  110. }tree;
  111. int main()
  112. {
  113. int n,m,sum=0;
  114. scanf("%d%d",&n,&m);
  115. tree.init();
  116. for(int i=1;i<=n;i++)
  117. {
  118. int a;scanf("%d",&a);
  119. a^=sum;sum=a;tree.add(a);
  120. // printf("%d---\n",a);
  121. }
  122. while(m--)
  123. {
  124. char op[5];scanf("%s",&op);
  125. if(op[0]=='A')
  126. {
  127. int x;scanf("%d",&x);
  128. x^=sum;sum=x;tree.add(x);
  129. // printf("%d---\n",x);
  130. }
  131. else
  132. {
  133. int l,r,x;
  134. scanf("%d%d%d",&l,&r,&x);
  135. x^=sum;tree.query(l-1,r,x);
  136. // printf("%d+++\n",x);
  137. }
  138. }
  139. // tree.debug(tree.root[2]);
  140. return 0;
  141. }
  142. /*****************
  143. 5 5
  144. 2 6 4 3 6
  145. A 1
  146. Q 3 5 4
  147. A 4
  148. Q 5 7 0
  149. Q 3 6 6
  150. *****************/

bzoj3261: 最大异或和 可持久化trie的更多相关文章

  1. BZOJ3261: 最大异或和(可持久化trie树)

    题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...

  2. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.       有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...

  3. BZOJ 3261: 最大异或和( 可持久化trie )

    搞成前缀和然后就可以很方便地用可持久化trie维护了.时间复杂度O((N+M)*25) -------------------------------------------------------- ...

  4. [十二省联考2019]异或粽子——可持久化trie树+堆

    题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...

  5. 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)

    LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...

  6. 【xsy1147】 异或(xor) 可持久化trie

    我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...

  7. [BZOJ4103][Thu Summer Camp 2015]异或运算 可持久化Trie树

    4103: [Thu Summer Camp 2015]异或运算 Time Limit: 20 Sec  Memory Limit: 512 MB Description 给定长度为n的数列X={x1 ...

  8. 【bzoj3689】异或之 可持久化Trie树+堆

    题目描述 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n*(n ...

  9. BZOJ 3261 最大异或和 可持久化Trie树

    题目大意:给定一个序列,提供下列操作: 1.在数组结尾插入一个数 2.给定l,r,x,求一个l<=p<=r,使x^a[p]^a[p+1]^...^a[n]最大 首先我们能够维护前缀和 然后 ...

随机推荐

  1. Sample Classification Code of CIFAR-10 in Torch

    Sample Classification Code of CIFAR-10 in Torch from: http://torch.ch/blog/2015/07/30/cifar.html req ...

  2. HDU 5583 Kingdom of Black and White(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...

  3. python 安装插件 requests、BeautifulSoup

    安装第三方插件库 1. requests  , 下载地址 https://github.com/requests/requests 安装: 利用 pip 安装 pip3 install request ...

  4. hdu 6134 Battlestation Operational 莫比乌斯反演

    Battlestation Operational Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  5. MAC office2016 安装及激活

    Office 下载地址: http://www.xitongtiandi.net/soft_yy/4285.html 破解补丁下载地址: https://bbs.feng.com/tencentdow ...

  6. str_replace 批量查找替换字符串

    <?php $str = 'I Love You!'; $str = str_replace('o','O',$str,$count); echo $str.PHP_EOL; // I LOve ...

  7. maven阿里云镜像及本地仓库

    一.添加阿里云镜像 1 找到maven的安装目录,conf文件夹下的setting.xml文件 2 打开setting.xml文件,找到mirrors节点添加阿里镜像库地址: <mirror&g ...

  8. [原][粒子特效][spark]粒子系统system、主节点group、渲染器render

    深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html system: A class defining a complete sy ...

  9. Codeforces 36B - Fractal

    36B - Fractal 思路:分形 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #def ...

  10. 类似于placehoder效果的图标展示

    在做app开发的时候往往会有那个注册登录啊,什么的页面,里面就会包含这那种类似于placeholder的效果的图标,当时我也是和ios和安卓混合开发一款app里面的页面全是我写,最开始就是登陆啊,注册 ...