题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种:

+ x: 表示向集合中添加一个元素x

- x:表示删除集合中值为x的一个元素

? x:表示查询集合中与x异或的最大值为多少

析:这是一个字典树的应用,不过确实没看出来。。。。主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除,

剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个。这样就是最大的。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <stack>
  15. using namespace std;
  16.  
  17. typedef long long LL;
  18. typedef pair<int, int> P;
  19. const int INF = 0x3f3f3f3f;
  20. const double inf = 0x3f3f3f3f3f3f;
  21. const LL LNF = 100000000000000000;
  22. const double PI = acos(-1.0);
  23. const double eps = 1e-8;
  24. const int maxn = 4e6 + 5;
  25. const int mod = 1e9 + 7;
  26. const char *mark = "+-*";
  27. const int dr[] = {-1, 0, 1, 0};
  28. const int dc[] = {0, 1, 0, -1};
  29. int n, m;
  30. inline bool is_in(int r, int c){
  31. return r >= 0 && r < n && c >= 0 && c < m;
  32. }
  33. inline LL Max(LL a, LL b){ return a < b ? b : a; }
  34. inline LL Min(LL a, LL b){ return a > b ? b : a; }
  35. inline int Max(int a, int b){ return a < b ? b : a; }
  36. inline int Min(int a, int b){ return a > b ? b : a; }
  37. int ch[maxn][2];
  38. int t[maxn];
  39. int cnt = 0;
  40.  
  41. void add(int x){
  42. int k = 1;
  43. for(int i = 30; i >= 0; --i){
  44. int j = ((1<<i) & x) > 0;
  45. if(!ch[k][j]) ch[k][j] = ++cnt;
  46. k = ch[k][j];
  47. ++t[k];
  48. }
  49. }
  50.  
  51. void del(int x){
  52. int k = 1;
  53. for(int i = 30; i >= 0; --i){
  54. int j = ((1<<i) & x) > 0;
  55. k = ch[k][j];
  56. --t[k];
  57. }
  58. }
  59.  
  60. int query(int x){
  61. int k = 1;
  62. int ans = 0;
  63. for(int i = 30; i >= 0; --i){
  64. int j = ((1<<i) & x) == 0;
  65. if(t[ch[k][j]]){
  66. ans |= (1<<i);
  67. k = ch[k][j];
  68. }
  69. else k = ch[k][1-j];
  70. }
  71. return ans;
  72. }
  73.  
  74. int main(){
  75. while(scanf("%d", &n) == 1){
  76. cnt = 1;
  77. memset(ch, 0, sizeof(ch));
  78. memset(t, 0, sizeof(t));
  79. add(0);
  80. while(n--){
  81. char s[3];
  82. int x;
  83. scanf("%s %d", s, &x);
  84. if(s[0] == '+') add(x);
  85. else if(s[0] == '-') del(x);
  86. else printf("%d\n", query(x));
  87. }
  88. }
  89. return 0;
  90. }

  

CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)的更多相关文章

  1. 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset

    题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...

  2. Codeforces 706D Vasiliy's Multiset(可持久化字典树)

    [题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...

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

  4. CodeForces 706D Vasiliy's Multiset

    字典树. 比较经典的题目了.把每一个数字都插入到字典树中,询问的时候如果$x$的第$i$位是$p$,那么尝试着在字典树上往$pXOR1$的节点走下去,没有$pXOR1$节点的话再走$p$的.删除操作的 ...

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

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

  6. codeforces 706D D. Vasiliy's Multiset(trie树)

    题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...

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

  8. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  9. codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...

随机推荐

  1. Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

    Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-nu ...

  2. android中getSystemService详解

        android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...

  3. Qt之启动外部程序

    简述 QProcess可以用来启动外部程序,并与它们交互. 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串. 另外,也可以 ...

  4. 解决编译报错:Unable to copy file, because it is being used by another process.

    Error    63    Unable to copy file "D:\DEV\XXX Website\trunk\4 Source Code\Common\WebControls\b ...

  5. Nginx - 指定log_format,常用于 Awstats 分析

    1. vim /etc/nginx/nginx.conf (下面格式, Awstats 使用) log_format new_log '$remote_addr - $remote_user [$ti ...

  6. Swift入门篇-Hello World

    提示:如果您使用手机和平板电脑看到这篇文章,您请在WIFI的环境下阅读,里面有很多图片, 会浪费很多流量. 博主语文一直都不好(如有什么错别字,请您在下评论)望您谅解,没有上过什么学的 最近这2天主要 ...

  7. Mac下开发常用目录

    1:Snippets    Xcode 代码段的文件表示 ~/Library/Developer/Xcode/UserData/CodeSnippets/ 2: Services  可以添加workf ...

  8. Hide Xcode8 strange log.

    Product > Scheme > Edit Scheme Environment Variables set OS_ACTIVITY_MODE = disable

  9. IT经理,你在这个位置吗

    事实上我离这个位置还远着,或者说它可能并不是我以后的方向,但是作为一个码农,这个发展路线还是需要了解的.主要的还是喜欢下面这个图,因为里面我的发展方向,有我的目标. 对 于一个IT从业者,让你谋得工作 ...

  10. Android-使用getIdentifier()获取资源Id

    使用getIdentifier()获取资源Id int i= getResources().getIdentifier("icon", "drawable", ...