CodeForces 367D Vasiliy's Multiset Trie树

来源:互联网 发布:电大和网络教育的区别 编辑:程序博客网 时间:2024/05/16 14:55

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
10+ 8+ 9+ 11+ 6+ 1? 3- 8? 3? 8? 11
output
11101413
Note

After first five operations multiset A contains integers 089116 and 1.

The answer for the sixth query is integer  — maximum among integers and .


题意:你有一个多重集合,一开始只有0,现在有3种操作:1.增加一个元素。2.擦除一个元素(如果有多个只擦除一个)。3给你一个x,问你集合里和他亦或以后最大是多少。

题解:把里面的元素弄成二进制,然后弄一颗Trie树,每次询问的时候从高位贪心的选择就好了。

//************************************************************************////*Author : Handsome How                                                 *////************************************************************************////#pragma comment(linker, "/STA    CK:1024000000,1024000000")#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <sstream>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <string>#include <ctime>#if defined(_MSC_VER) || __cplusplus > 199711L#define aut(r,v) auto r = (v)#else#define aut(r,v) __typeof(v) r = (v)#endif#define foreach(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)#define fur(i,a,b) for(int i=(a);i<=(b);i++)#define furr(i,a,b) for(int i=(a);i>=(b);i--)#define cl(a) memset((a),0,sizeof(a))#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))#ifdef HandsomeHow#define debug(...) fprintf(stderr, __VA_ARGS__)#define dbg(x) cout << #x << " = " << x << endl#else#define debug(...)#define dbg(x)#endifusing namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair <int, int> pii;const int inf=0x3f3f3f3f;const double eps=1e-8;const int mod=1000000007;const double pi=acos(-1);inline void gn(long long&x){    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;}inline void gn(int&x){long long t;gn(t);x=t;}inline void gn(unsigned long long&x){long long t;gn(t);x=t;}ll gcd(ll a,ll b){return a? gcd(b%a,a):b;}ll powmod(ll a,ll x,ll mod){ll t=1ll;while(x){if(x&1)t=t*a%mod;a=a*a%mod;x>>=1;}return t;}// (づ°ω°)づe★//-----------------------------------------------------------------struct Node{int v;Node* son[2];}Root,*root;void add(int x){Node *p = root;furr(i,30,0){int t = (x>>i)&1;if(p->son[t] == NULL){p->son[t] = new Node;p = p->son[t];p->son[0] = p->son[1] = NULL;p->v = 1;}else{p = p->son[t];p->v++;}}}void era(int x){Node *p = root;furr(i,30,0){int t = (x>>i)&1;p = p->son[t];p->v--;}}int query(int x){Node *p = root;int v = 0;add(0);furr(i,30,0){int t = (x>>i)&1;if(p->son[!t]!=NULL && p->son[!t]->v>0){p = p->son[!t];v = v * 2 + (!t);}else{p = p->son[t];v = v * 2 + t;}}return x^v;}char op[10];int x;int main(){#ifdef HandsomeHow    //freopen("data.in","r",stdin);    //freopen("data.out","w",stdout);    time_t beginttt = clock();#endifroot = &Root;root->son[0] = root->son[1] = NULL;root->v = 0;int q;scanf("%d",&q);while(q--){scanf("%s %d",op,&x);if(op[0] == '+') add(x);if(op[0] == '-') era(x);if(op[0] == '?') printf("%d\n",query(x));}#ifdef HandsomeHowtime_t endttt = clock();    debug("time: %d\n",(int)(endttt - beginttt));#endifreturn 0;}


0 0