D. Vasiliy's Multiset 异或字典树

Posted dreamkill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Vasiliy's Multiset 异或字典树相关的知识,希望对你有一定的参考价值。

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
Copy
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11
output
Copy
11
10
14
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 技术分享图片.

 

就是有3种操作

+ x向集合里添加 x

- x 删除x元素,(保证存在

? x 查询 x |  集合中元素的最大值

新姿势 get,就是利用字典树,从高位到低位进行贪心。想到从高位求,但是不知道怎么用字典树..orz.新姿势

技术分享图片
 1 #include <algorithm>
 2 #include <stack>
 3 #include <istream>
 4 #include <stdio.h>
 5 #include <map>
 6 #include <math.h>
 7 #include <vector>
 8 #include <iostream>
 9 #include <queue>
10 #include <string.h>
11 #include <set>
12 #include <cstdio>
13 #define FR(i,n) for(int i=0;i<n;i++)
14 #define MAX 2005
15 #define mkp pair <int,int>
16 using namespace std;
17 //#pragma comment(linker, "/STACK:10240000000,10240000000")
18 const int maxn = 4e6+4;
19 typedef long long ll;
20 const int  inf = 0x3fffff;
21 void read(int  &x) {
22     char ch; bool flag = 0;
23     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == -)) || 1); ch = getchar());
24     for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
25     x *= 1 - 2 * flag;
26 }
27 
28 int tree[maxn][3];
29 int sum[maxn];
30 
31 int cnt = 2;
32 void Insert(int val,ll t)
33 {
34     int root = 1;
35     for(int i=31;i>=0;i--)
36     {
37         int id=(t>>i)&1;
38         if(!tree[root][id])
39         {
40             tree[root][id]=cnt++;
41         }
42         root = tree[root][id];
43         sum[root]+=val;
44     }
45 }
46 
47 ll ans(ll tmp)
48 {
49     tmp=~tmp;
50     int root = 1;
51     ll res = 0;
52     for(int i=31;i>=0;i--)
53     {
54         int id = (tmp>>i)&1;
55         res*=2;
56         if(tree[root][id]&&sum[tree[root][id]])
57         {
58             res++;
59             root = tree[root][id];
60         }
61         else
62         {
63             root=tree[root][1-id];
64         }
65     }
66     return res;
67 }
68 
69 int main() {
70     int n;
71     read(n);
72     memset(sum,0,sizeof(sum));
73     memset(tree,0,sizeof(tree));
74     Insert(1,0);
75     /*for(int i=0;i<32;i++){
76         printf("%d %d
",tree[i][0],tree[i][1]);
77     }*/
78     for(int i=0;i<n;i++){
79         char c;
80         ll t;
81         cin>>c>>t;
82         if(c==+)Insert(1,t);
83         else if(c==-)Insert(-1,t);
84         else {
85             printf("%lld
",ans(t));
86         }
87     }
88     return 0;
89 }
View Code

 

 














以上是关于D. Vasiliy's Multiset 异或字典树的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 706D Vasiliy's Multiset

(字典树)codeforces - 706D Vasiliy's Multiset

Vasiliy's Multiset CodeForces -706D || 01字典树模板

trie树 Codeforces Round #367 D Vasiliy's Multiset

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

D. Multiset(带排名的数组数组)