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

11101413

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 }

D. Vasiliy's Multiset 异或字典树

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

时间: 2024-08-02 08:07:36

D. Vasiliy's Multiset 异或字典树的相关文章

POJ 3764 (异或+字典树)

早就听过用字典树求异或最大值,然而没做过.发现一碰到异或的题就GG,而且因为以前做过的一道类似的题(事实上并不类似)限制了思路,蠢啊= =. 题意:一棵带权的树,求任意两点间路径异或的最大值. 题解:设xor(a,b)是求a,b间路径的异或值,那么xor(a,b)=xor(root,a)^xor(root,b).因为如果LCA(a,b)==root时结论显然成立,不然的话就会有重复走过的部分,但是异或有性质x^x=0,所以LCA(a,b)!=root结论依然成立. 这样题目就很简单了.对每一个x

CodeForces 706D Vasiliy&#39;s Multiset (字典树查询+贪心)

题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大值为多少 析:这是一个字典树的应用,不过确实没看出来....主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除, 剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个.这

Codeforces 706D Vasiliy&#39;s Multiset(可持久化字典树)

[题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个元素x(保证x存在),3:要求从集合中选出一个数,使得其与给出的数x的异或值最大,输出这个异或值. [题解] 可以将所有的以二进制形式存在01字典树上,删除即插入权值为-1的二进制串,对于异或值最大的操作,我们只要在字典树上按位贪心,从最高位开始尽量保证该位存在最后就能得到答案.写代码的时候直接写了

(字典树)codeforces - 706D Vasiliy&#39;s Multiset

原题链接:http://codeforces.com/problemset/problem/706/D 题意:需要你模拟一个多重集合(初始拥有0),该集合拥有三个功能 1.+ x  增加一个x(可以同时存在多个x,因为是多重集合) 2.-  x 去掉一个x 3.? x  输出在这个集合中和x异或最大的值. 分析:有20万个操作,很容易想到树形数据结构,我们可以看到每个x最多只有10^9,而1<<30比10^9大一点,所以10^9次方最多就是29位的01串,可以维护一个字典树. 每个节点有两个分

Vasiliy&#39;s Multiset CodeForces -706D || 01字典树模板

就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int _LEN=30; 5 //上限为2^30-1,即二进制最多30位 6 int lft[40]; 7 namespace Trie 8 { 9 int ch[7001000][2],sz[7001000]; 10 int mem; 11 void insert(int

trie树 Codeforces Round #367 D Vasiliy&#39;s Multiset

1 // trie树 Codeforces Round #367 D Vasiliy's Multiset 2 // 题意:给一个集合,初始有0,+表示添加元素,-去除元素,?询问集合里面与x异或最大的值 3 // 思路:思路很好想,建立trie树,再贪心当前位是1则选0,0则选1 4 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define LL long long 9 const double inf = 123456789

POJ 3764 The xor-longest Path 字典树求最大异或

题意,一颗树,每个边有个值,在树上找一条简单路径,使得这条路径上的边权异或值最大 把这题模型转换一下, 对于任意一条路径的异或,表示为f(u, v) 则f(u, v) = f(1, u) ^ f(1, v) 这是显然的 其中f(1, i)是可以再O(n)内处理出来 然后就是在一个数组内,找两个数异或值最大 然后就可以用字典树来搞 每个数变成01串,  然后插入字典树, 第30位在最前,然后29,依次到0位 就建立成了一个深度为31的字典树 对于一个询问,在字典树上找,就是尽量找跟其相反的路径.

关于异或_字典树的一些性质

异或的性质 1. a ⊕ a = 0 2. a ⊕ b = b ⊕ a 3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c; 4. d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c. 5. a ⊕ b ⊕ a = b.     自反性 6.若x是二进制数0101,y是二进制数1011: 则x⊕y=1110 只有在两个比较的位不同时其结果是1,否则结果为0 即"两个输入相同时为0,不同则为1"! 字典树:又称为Trie,是一种用于快速检索的多叉

hdu5269 ZYB loves Xor I 异或,字典树

hdu5269  ZYB loves Xor I   异或,字典树 ZYB loves Xor I Accepts: 142 Submissions: 696 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 ZYB喜欢研究Xor,现在他得到了一个长度为nn的数组A.于是他想知道:对于所有数对(i,j)(i \in [1,n],j \in [1,n])(i,j)(i∈[1,n