CodeForces 367D Vasiliy's Multiset Trie树
来源:互联网 发布:电大和网络教育的区别 编辑:程序博客网 时间:2024/05/16 14:55
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.
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.
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.
10+ 8+ 9+ 11+ 6+ 1? 3- 8? 3? 8? 11
11101413
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)#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;}
- CodeForces 367D Vasiliy's Multiset Trie树
- Codeforces 706D-Vasiliy's Multiset(Trie树)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (01trie树)
- HDU-4825 Xor Sum &&Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (Trie树)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 二进制树、Trie
- 【 Codeforces Round #367 (Div. 2) D】 Vasiliy's Multiset (Trie 按数位建字典树)
- Codeforces Round #367 D. Vasiliy's Multiset
- Codeforces Round #367 (Div. 2) D Vasiliy's Multiset(01Trie)
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (multiset)
- Codeforces Round #367 (Div. 2) Vasiliy's Multiset xor trie
- CodeForces 706D Vasiliy's Multiset 字典树
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
- CodeForces 706D Vasiliy's Multiset(0/1树)
- Codeforces 706D Vasiliy's Multiset【贪心+字典树】
- Codeforces-706D Vasiliy's Multiset
- CodeForces 706D Vasiliy's Multiset
- 【Codeforces Round 367 (Div 2) D】【字典树典型题】Vasiliy's Multiset
- Codeforces Round #367 (Div. 2) D——Vasiliy's Multiset(异或字典树)
- MFC 获取App,Doc,MainFrame,View指针
- Jquery点击事件只触发一次的解决方法
- C++ 课程设计学生成绩管理系统源代码
- [OS复习]程序装入技术、简单存储管理技术
- 把二叉树打印成多行
- CodeForces 367D Vasiliy's Multiset Trie树
- android 之ListView
- 解决jdk1.8中发送邮件失败(handshake_failure)问题
- POJ 2195 Going Home [二分图带权匹配] [费用流]
- android studio导入项目报错Plugin with id 'com.android.application' not found.解决
- 如何使用drawerlayout
- 【opencv的学习】各类变换的尝试
- 对C#的exe更改图标
- ubuntu搭建Git服务器,windowns建立git客户端