CodeForces 367D Vasiliy's Multiset Trie树
2016-08-12 22:36
288 查看
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:
"+ x" — add integer x to multiset A.
"- 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.
"? 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
output
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
.
题意:你有一个多重集合,一开始只有0,现在有3种操作:1.增加一个元素。2.擦除一个元素(如果有多个只擦除一个)。3给你一个x,问你集合里和他亦或以后最大是多少。
题解:把里面的元素弄成二进制,然后弄一颗Trie树,每次询问的时候从高位贪心的选择就好了。
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:
"+ x" — add integer x to multiset A.
"- 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.
"? 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
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
.
题意:你有一个多重集合,一开始只有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) #endif using 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(); #endif root = &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 HandsomeHow time_t endttt = clock(); debug("time: %d\n",(int)(endttt - beginttt)); #endif return 0; }
相关文章推荐
- CodeForces 24C Sequence of points (几何)
- Codeforces 347C Alice and Bob
- CodeForces 591B
- Codeforces 347C - Alice and Bob
- CodeForces - 622E dfs+贪心
- [CodeForces-721E]Road to Home
- CodeForces 245D Restoring Table
- Codeforces 460C Present (贪心 + 二分)
- 【CodeForces】[621A]Wet Shark and Odd and Even
- 【CodeForces】444A - DZY Loves Physics(图论规律)
- [线段树] Codeforces 794F Round #414 F. Leha and security system
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- CodeForces 401C
- codeforces 414B B. Mashmokh and ACM(dp)
- codeforces 354 div2 C Vasya and String 前缀和
- CodeForces 732B Cormen — The Best Friend Of a Man
- Codeforces 104C Cthulhu dfs暴力 || 点双连通缩点
- CodeForces 867A Between the Offices
- Codeforces 413C Jeopardy!(贪心)
- CodeForces 425E Sereja and Sets