Vasiliy's Multiset CodeForces - 706D

原题链接

Trie+位运算

和前面题一样标记下删除的路径即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int N = 200010;
 6 int idx,son[N*32][2],cnt[N*32];
 7 void insert_t(int x)
 8 {
 9     int p = 0;
10     for(int i=31;~i;i--){
11         int t = x>>i&1;
12         if(!son[p][t]) son[p][t] = ++idx;
13         p = son[p][t];
14         cnt[p]++;
15     }
16 }
17 int query(int x)//查询最大值 
18 {
19     int p = 0,ans = 0;
20     for(int i=31;~i;i--){
21         int t = x>>i&1;
22         if(son[p][!t]&&cnt[son[p][!t]]){
23             p = son[p][!t];
24             ans+=1<<i;
25         }else if((son[p][!t]&&!cnt[son[p][!t]])||!son[p][!t]){
26             p = son[p][t];
27         }
28     }
29     return ans;
30 }
31 void deletes(int x)
32 {
33     int p = 0;
34     for(int i=31;~i;i--){
35         int t = x>>i&1;
36         p = son[p][t];
37         cnt[p]--;
38     }
39 }
40 int main()
41 {
42 //    freopen("in.txt","r",stdin);
43     int q;
44     insert_t(0);
45     scanf("%d",&q);
46     while(q--){
47         char op[2]; int x;
48         scanf("%s%d",op,&x);
49         if(op[0]=='+') insert_t(x);
50         else if(op[0]=='-') deletes(x);
51         else if(op[0]=='?') printf("%d\n",query(x));
52     }
53     return 0;
54 }

 

posted @ 2021-01-02 19:23  acmloser  阅读(80)  评论(0编辑  收藏  举报