题目描述

There is an integer sequence a of length n and there are two kinds of operations:
0 l r: select some numbers from al...ar so that their xor sum is maximum, and print the maximum value.
1 x: append x to the end of the sequence and let n=n+1.

输入

There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
For each test case: 
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,...,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It's guaranteed that ∑n≤106,∑m≤106,0≤x<230.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero: 
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.

输出

For each type 0 operation, please output the maximum xor sum in a single line.

样例输入

  1. 1
  2. 3 3
  3. 0 1 2
  4. 0 1 1
  5. 1 3
  6. 0 3 4

样例输出

  1. 1
  2. 3
  1. 题意:
  2. 查询区间[l,r]的最大异或和。
  3. 官方题解:
  4. 暴力的做法可以用数据结构维护区间线性基,但肯定过不了。
  1. 贪心地维护序列的前缀线性基 (上三角形态),对于每个线性基,将出现位置靠右的数字尽可能地放在高位
    也就是说在插入新数字的时候,要同时记录对应位置上数字的出现位置,并且在找到可以插入的位置的时候,如果新数字比位置上原来的数字更靠右,就将该位置上原来的数字向低位推。
  1. 在求最大值的时候,从高位向低位遍历,如果该位上的数字出现在询问中区间左端点的右侧且可以使答案变大,就异或到答案里。
    对于线性基的每一位,与它异或过的线性基更高位置上的数字肯定都出现在它右侧 (否则它就会被插入在那个位置了),因此做法的正确性显然。
  1. 线性基:
    https://blog.csdn.net/a_forever_dream/article/details/83654397
    https://blog.sengxian.com/algorithms/linear-basis?tdsourcetag=s_pctim_aiomsg
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. const int N=5e5+;
  5. int T,n,m;
  6. int p[N][],pos[N][];
  7. void add(int val,int x)
  8. {
  9. for (int i=;i<=;i++) p[x][i]=p[x-][i],pos[x][i]=pos[x-][i];
  10. int now=x;
  11. for (int i=;i>=;i--)
  12. if (val&(<<i))
  13. {
  14. if (!p[x][i])
  15. {
  16. p[x][i]=val;
  17. pos[x][i]=now;
  18. break;
  19. }
  20. if (pos[x][i]<now) swap(p[x][i],val),swap(pos[x][i],now);
  21. val^=p[x][i];
  22. }
  23. }
  24. int query(int l,int r)
  25. {
  26. int ret=;
  27. for (int j=;j>=;j--)
  28. if (pos[r][j]>=l && (ret^p[r][j])>ret) ret^=p[r][j];
  29. return ret;
  30. }
  31. int main()
  32. {
  33. // freopen("1.in","r",stdin);
  34. // freopen("1.out","w",stdout);
  35. scanf("%d",&T);
  36. while (T--)
  37. {
  38. scanf("%d%d",&n,&m);
  39. int op,x,l,r;
  40. for (int i=;i<=n;i++)
  41. {
  42. scanf("%d",&x);
  43. add(x,i);
  44. }
  45. int lastans=;
  46. while (m--)
  47. {
  48. scanf("%d",&op);
  49. if (op==)
  50. {
  51. scanf("%d%d",&l,&r);
  52. l=(l^lastans)%n+;
  53. r=(r^lastans)%n+;
  54. if (l>r) swap(l,r);
  55. lastans=query(l,r);
  56. printf("%d\n",lastans);
  57. }
  58. else
  59. {
  60. scanf("%d",&x);
  61. x^=lastans;
  62. n++;
  63. add(x,n);
  64. }
  65. }
  66. }
  67. // fclose(stdin);
  68. // fclose(stdout);
  69. return ;
  70. }
  1.  

杭电多校第一场-B-Operation的更多相关文章

  1. 2019杭电多校第一场hdu6579 Operation(线性基)

    Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...

  2. [2019杭电多校第一场][hdu6579]Operation(线性基)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题目大意是两个操作,1个是求[l,r]区间子序列的最大异或和,另一个是在最后面添加一个数. 如果 ...

  3. 2018 Multi-University Training Contest 1 杭电多校第一场

    抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001  Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...

  4. 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)

    题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...

  5. 2019杭电多校第一场hdu6581 Vacation

    Vacation 题目传送门 update(O(n)) 看了那个O(n)的方法,感觉自己想的那个O(nlogn)的好傻,awsl. 0车最终通过停车线的时候,状态一定是某个车堵住后面的所有车(这个车也 ...

  6. 2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)

    题目链接 传送门 题意 给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小. 思路 做这种题目就是要保持思路 ...

  7. 2019年杭电多校第一场 1004题Vacation(HDU6581+数学)

    题目链接 传送门 题意 有\(n+1\)辆车要过红绿灯,告诉你车的长度.与红绿灯的起点(题目假设红绿灯始终为绿).车的最大速度,问你第\(0\)辆车(距离最远)车头到达红绿灯起点的时间是多少(每辆车最 ...

  8. [2019杭电多校第一场][hdu6582]Path(最短路&&最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 题意:删掉边使得1到n的最短路改变,删掉边的代价为该边的边权.求最小代价. 比赛时一片浆糊,赛后 ...

  9. [2019杭电多校第一场][hdu6583]Typewriter(后缀自动机&&dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6583 大致题意是说可以花费p在字符串后添加一个任意字符,或者花费q在字符串后添加一个当前字符串的子串. ...

随机推荐

  1. Python之lambda && reduce

    lambda类似于C里面的#define或者C++里面的内联函数(inline),一般都小巧精悍 >>> g=lambda x,y:x*y >>> g(3,7) 2 ...

  2. Inversion of Control 控制反转 有什么好处

    作者:Mingqi链接:https://www.zhihu.com/question/23277575/answer/169698662来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  3. sysprep

    今天做虚拟机模板,以及克隆.单纯的克隆会造成很多冲突问题的产生,所以在这里,windows自带的sysprep功能很好的解决了这一点. 路径位于:C:\Windows\System32\Sysprep ...

  4. 【读书笔记】剑指offer

    导语 所有的编程练习都在牛客网OJ提交,链接: https://www.nowcoder.com/ta/coding-interviews 九章算法的 lintcode 也有这本书的题目.https: ...

  5. js 连等操作,,

    奥术大师 var hu = { a : , c : , name : }; (function (){ var ccc = bbb = aaa = hu; })() console.log(bbb)* ...

  6. vue eslint修改为4个空格

  7. mybatis xml中大于号小于号的代替

    第一种写法(1): 原符号 < <= > >= & ' "替换符号 < <= > >= & &apos; " ...

  8. SqlServer 查询的时候过滤条件有参数导致速度很慢的问题-参数嗅探

    何谓SQLSERVER参数嗅探 大家听到“嗅探”这个词应该会觉得跟黑客肯定有关系吧,使用工具嗅探一下参数,然后截获,脱裤o(∩_∩)o . 事实上,我觉得大家太敏感了,其实这篇文章跟数据库安全没有什么 ...

  9. 什么是平衡树B-Tree?【转】

    转载自:https://www.cnblogs.com/dongguacai/p/7239599.html#commentform B-Tree就是我们常说的B树,一定不要读成B减树,否则就很丢人了. ...

  10. 【leetcode】977. Squares of a Sorted Array

    题目如下: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...