题目链接:

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
    + 8
    + 9
    + 11
    + 6
    + 1
    ? 3
    - 8
    ? 3
    ? 8
    ? 11
output
  1. 11
    10
    14
    13
题意:
 
+表示吧这个数加到集合中,-表示把这个数从集合中减去一次,?表示集合里面的一个y使的x^y最大;
 
思路:
 
trie树,把这些数都转化成01串插入到trie树种,?的时候从高位到低位,是0的时候就尽量取1,是1的时候就尽量取0这样贪心就好了;比赛的时候数组开小了;
 
AC代码;
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <bits/stdc++.h>
  7. #include <stack>
  8. #include <map>
  9.  
  10. using namespace std;
  11.  
  12. #define For(i,j,n) for(int i=j;i<=n;i++)
  13. #define mst(ss,b) memset(ss,b,sizeof(ss));
  14.  
  15. typedef long long LL;
  16.  
  17. template<class T> void read(T&num) {
  18. char CH; bool F=false;
  19. for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
  20. for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
  21. F && (num=-num);
  22. }
  23. int stk[70], tp;
  24. template<class T> inline void print(T p) {
  25. if(!p) { puts("0"); return; }
  26. while(p) stk[++ tp] = p%10, p/=10;
  27. while(tp) putchar(stk[tp--] + '0');
  28. putchar('\n');
  29. }
  30.  
  31. const LL mod=1e9+7;
  32. const double PI=acos(-1.0);
  33. const int inf=1e18;
  34. const int N=2e5+10;
  35. const int maxn=5e3+4;
  36. const double eps=1e-12;
  37.  
  38. char s[100];
  39. int x;
  40. int ch[32*N][2],sz=1,val[32*N][2],temp[50];
  41. void in(int num)
  42. {
  43. int u=0;
  44. for(int i=31;i>=0;i--)
  45. {
  46. int c=temp[i];val[u][c]+=num;
  47. if(!ch[u][c])
  48. {
  49. ch[sz][0]=ch[sz][1]=0;
  50. ch[u][c]=sz++;
  51. }
  52. u=ch[u][c];
  53. }
  54. }
  55. int query()
  56. {
  57. int u=0,ans=0;
  58. for(int i=31;i>=0;i--)
  59. {
  60. int c=temp[i];
  61. if(ch[u][c^1]&&val[u][c^1])
  62. {
  63. ans|=(1<<i);
  64. u=ch[u][c^1];
  65. }
  66. else u=ch[u][c];
  67. }
  68. return ans;
  69. }
  70.  
  71. int main()
  72. {
  73. mst(temp,0);
  74. in(1);
  75. int q;
  76. read(q);
  77. while(q--)
  78. {
  79. scanf("%s%d",s,&x);
  80. mst(temp,0);
  81. int cnt=0;
  82. while(x)
  83. {
  84. temp[cnt++]=x%2;
  85. x>>=1;
  86. }
  87. if(s[0]=='+')in(1);
  88. else if(s[0]=='-')in(-1);
  89. else printf("%d\n",query());
  90. }
  91. return 0;
  92. }

  

codeforces 706D D. Vasiliy's Multiset(trie树)的更多相关文章

  1. 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 ...

  2. Codeforces 706 D. Vasiliy's Multiset (字典树贪心)

    题目链接:http://codeforces.com/contest/706/problem/D 题意很简单不多说. 把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可. 注意 ...

  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 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  6. Codeforces 633C Spy Syndrome 2 【Trie树】+【DFS】

    <题目链接> 题目大意:给定一个只有小写字母组成的目标串和m个模式串(里面可能有大写字母),记目标串反过来后的串为S,让你从m个模式串中选出若干个组成S串(不区分大小写).输出任意一种方案 ...

  7. Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】

    <题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...

  8. CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...

  9. 【35.20%】【CF 706D】Vasiliy's Multiset

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. Linux 安装、卸载程序

    一, RPM 安装:        rpm -ivh xxx.rpm 重新安装: rpm -ivh -replacepkgs xxx.rpm 卸载:       rpm -e xxx.rpm 二,ta ...

  2. 浅谈js中的MVC

    MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据).视图.控制器(分发器) 本文将用一个经典的例子todoList来展开 一个事件发生的过程(通信单向流动): 1.用户在视图V ...

  3. cocos2d-x-lua基础系列教程五(lua单例)

    lua-单例 function newAccount(initlizedBanlance) local self = {balance = initlizedBanlance} local show ...

  4. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--解析SQL语法树

    OceanBase是阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作. 在阿里巴巴集团下,OceanBase数据库支持了多个重要业务的数据 ...

  5. hadoop权威指南学习

    通常情况下,处理少量的大型文件更容易.更有效,为什么呢? map阶段中的键如果不需要可以忽略掉? MapReduce过程也可以用于本地文件的处理,但是如果是要使用到集群的话还需要HDFS. Data ...

  6. 计算两个GPS坐标点的距离

    计算两个GPS坐标点的距离,第一个参数是第一个点的维度,第二个参数是第一个点的经度 http://yuninglovekefan.blog.sohu.com/235655696.html /** * ...

  7. [nio]dawn的基本概念

    1.dawn是单线程的: 为什么单线程?现实中非常多程序都是单线程的.比方redis,memcache,nodejs.mmorpgserver..... . 採用单线程有两大优点,首先,不须要使用锁, ...

  8. 【题解】DZY Loves Chinese

    [题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...

  9. CentOS 7 安装、配置、使用 PostgreSQL 9.5(一)安装及基础配置

    一直不知道怎么读这个数据库的名字,在官网上找到了文档.PostgreSQL is pronounced Post-Gres-Q-L. 读音 What is PostgreSQL? How is it ...

  10. ABAP-创建客户

      CALL METHOD CMD_EI_API=>MAINTAIN_BAPI FUNCTION Z_CS_RFC_OA002 . *"------------------------ ...