D. Vasiliy's Multiset
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Author has gone out of the stories about Vasiliy, so here is just a formal task description.

You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:

  1. "+ x" — add integer x to multiset A.
  2. "- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query.
  3. "? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.

Multiset is a set, where equal elements are allowed.

Input

The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.

Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type.

Note, that the integer 0 will always be present in the set A.

Output

For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.

Example
input
  1. 10
  2. + 8
  3. + 9
  4. + 11
  5. + 6
  6. + 1
  7. ? 3
  8. - 8
  9. ? 3
  10. ? 8
  11. ? 11
output
  1. 11
  2. 10
  3. 14
  4. 13
Note

After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.

The answer for the sixth query is integer  — maximum among integers and .

题目链接:CF 706D

字典树水题,只是最近在膜可持久化字典树,拿这道题当模版写了一下,感觉跟主席树非常像,毕竟都是可持久化的思想,新建节点信息代替更改节点信息,写法也简单明了如果会主席树的话代码是不难理解的,只是最后返回的值是异或过的值,因为一个节点不再是模拟地去初始化、连接到链表尾,而是直接把节点的位置拿来标记为已使用,把信息复制到对应节点上去,因此需要一个变量cnt来记录这条路径有没有被覆盖过或者说有没有被使用过,有的话说明这条路到底是存在一个数,即用普通字典树的话来说就是这个点不为NULL,可以往下走。滋瓷范围内的Xor最大值的贪心查询

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=200010;
  4. struct Trie
  5. {
  6. int nxt[2];
  7. int cnt;
  8. };
  9. Trie L[N*34];
  10. int tot;
  11. int root[N];
  12.  
  13. void init()
  14. {
  15. memset(L,0,sizeof(L));
  16. tot=0;
  17. }
  18. void update(int &cur,int ori,int step,int n,int v)
  19. {
  20. cur=++tot;
  21. L[cur]=L[ori];
  22. L[cur].cnt+=v;
  23. if(step<0)
  24. return ;
  25. int t=(n>>step)&1;
  26. update(L[cur].nxt[t],L[ori].nxt[t],step-1,n,v);
  27. }
  28. int Find(int S,int E,int step,int n)
  29. {
  30. if(step<0)
  31. return 0;
  32. int t=(n>>step)&1;
  33. if(L[L[E].nxt[t^1]].cnt-L[L[S].nxt[t^1]].cnt>0)
  34. return (1<<step)+Find(L[S].nxt[t^1],L[E].nxt[t^1],step-1,n);
  35. else
  36. return Find(L[S].nxt[t],L[E].nxt[t],step-1,n);
  37. }
  38. int main(void)
  39. {
  40. int n,x,i;
  41. char ops[3];
  42.  
  43. while (~scanf("%d",&n))
  44. {
  45. init();
  46. update(root[1],root[0],31,0,1);
  47. int op=1;
  48. int sz=0;
  49. for (i=1; i<=n; ++i)
  50. {
  51. scanf("%s",ops);
  52. if(ops[0]=='+')
  53. {
  54. ++sz;
  55. ++op;
  56. scanf("%d",&x);
  57. update(root[op],root[op-1],31,x,1);
  58. }
  59. else if(ops[0]=='-')
  60. {
  61. --sz;
  62. ++op;
  63. scanf("%d",&x);
  64. update(root[op],root[op-1],31,x,-1);
  65. }
  66. else
  67. {
  68. scanf("%d",&x);
  69. if(!sz)
  70. {
  71. printf("%d\n",x);
  72. continue;
  73. }
  74. printf("%d\n",Find(root[0],root[op],31,x));
  75. }
  76. }
  77. }
  78. }

Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)的更多相关文章

  1. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset

    题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...

  2. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  3. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie

    题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...

  4. Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  5. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  7. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  8. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  9. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

随机推荐

  1. dubbox编译

    dubbox编译要在命令行 切记切记 设置JAVA_HOME 设置maven路径 命令编译dubbox 设置M2_HOME环境变量 设置idea M2_HOME dubbox 服务端 http://w ...

  2. HRESULT:0x80070057 (E_INVALIDARG)的异常

    错误信息: 未能加载文件或程序集……或它的某一个依赖项.参数不正确. (异常来自 HRESULT:0x80070057 (E_INVALIDARG)) English:Could not load f ...

  3. Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法2 - SingleLaunchActivityTestCase

    本文来源于:http://blog.csdn.net/zhubaitian/article/details/39296753 在上一遍笔记博客中本以为只能在Setup和TearDown中做条件判断来实 ...

  4. jetty 长时间运行之后出现 PWC6117 file not found

    严重: PWC6117: File "%2Ftmp%2Fjetty-0.0.0.0-9090-admin.war-_admin-any-%2Fwebapp%2Ferror%2F404.jsp ...

  5. SqlServer事务回滚(2)

    SQL Server 2008中SQL应用系列--目录索引 SQL事务 一.事务概念    事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是 ...

  6. iOS之08-核心语法

    1.点语法 点语法( . )的本质还是方法调用, java中的点是访问成员变量, 在OC中直接访问成员变量的方式只有 -> p.age = ; // [p setAge:10] int a = ...

  7. BZOJ2149 : 拆迁队

    设$c[i]=g[i]+\frac{i(i+1)}{2}-a[i]\times i-a[i]$,$d[i]=a[i]-i$ $f[i]$表示以$i$为结尾最多保留多少个建筑,则 $f[i]=\max( ...

  8. 163源报错Hash Sum mismatch 解决方法

    Ubuntu server 用的163的源,报错: W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-updates/mai ...

  9. Javascript中括号“[]”的多义性

    摘要:本文就是主要是分享JavaScript中括号的四种语义. Javascript中括号有四种语义 语义1,声明数组 var ary = []; // 声明一个空数组 var ary = [1,3] ...

  10. ACM Same binary weight

    Same binary weight 时间限制:300 ms  |  内存限制:65535 KB 难度:3   描述 The binary weight of a positive  integer ...