Codeforces Round #367 (Div. 2) 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
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  那么我就尽量去找同层的1  如果当前是0 那么就尽量去找同层的1 .

 

/* ***********************************************
Author        :guanjun
Created Time  :2016/8/12 10:36:45
File Name     :cf367d.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

struct node{ int next[2]; int v; void init(){ v=0; memset(next,-1,sizeof next); } }; node L[210000*32]; int tot=0; void add(char s[],int len){ int now=0; for(int i=0;i<len;i++){ int t=s[i]-'0'; int next=L[now].next[t]; if(next==-1){ next=++tot; L[next].init(); L[now].next[t]=next; } now=next; L[now].v++; } //L[now].v++; } void dele(char s[],int len){ int now=0; for(int i=0;i<len;i++){ int t=s[i]-'0'; int next=L[now].next[t]; now=next; L[now].v--; } // L[now].v--; for(int i=0;i<2;i++)L[now].next[i]=-1; } int query(char s[],int len){ int now=0; int ans=0; for(int i=0;i<len;i++){ int t=s[i]-'0'; int next=L[now].next[1-t]; if(next==-1||L[next].v<=0){ //这个点不存在或者已经删除了 next=L[now].next[t]; } else{ ans+=pow(2.0,32-i-1);; } now=next; } return ans; } void cha(int x,char *s){ for(int i=0;i<=31;i++){ s[i]=x%2+'0'; x/=2; } s[32]='\0'; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); char s[32],c; int n,d; cin>>n; L[0].init(); cha(0,s); add(s,32); for(int i=1;i<=n;i++){ cin>>c>>d; cha(d,s); reverse(s,s+32); if(c=='+'){ add(s,32); } else if(c=='-'){ dele(s,32); } else{ printf("%d\n",query(s,32)); } } return 0; }

 

posted on 2016-08-12 13:49  Beserious  阅读(465)  评论(0编辑  收藏  举报