码迷,mamicode.com
首页 > 其他好文 > 详细

D. Vasiliy's Multiset 异或字典树

时间:2018-08-06 00:39:59      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:ring   redo   fir   nes   ems   before   turn   element   property   

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\n",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\n",ans(t));
86         }
87     }
88     return 0;
89 }
View Code

 

 

D. Vasiliy's Multiset 异或字典树

标签:ring   redo   fir   nes   ems   before   turn   element   property   

原文地址:https://www.cnblogs.com/DreamKill/p/9427765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!