CodeForces 706D Vasiliy's Multiset

来源:互联网 发布:安全数据交换控制系统 编辑:程序博客网 时间:2024/05/16 14:09
传送门:706D
D. Vasiliy's Multiset
time limit per test
4 seconds
memory limit per test
256 megabytes

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 .


一共三种操作:“+ x”将x插入数列,“- x”将x从数列中删除,“? x”询问数列中的数异或上x的最大值,用字典树解决即可,注意删除操作,遇到一个节点的访问次数为1时,将其置为nullptr即可。

#include <iostream>#include <cstdio>#include <cstring>#include <malloc.h>using namespace std;const int maxn=2;struct trie {    trie *next[maxn];//指向该节点的下两个节点    int v;//该节点的访问次数};inline int read() {//读入外挂    int x = 0, f = 1; char ch = getchar();    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }    while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }    return x*f;}inline void bulid(trie *root,int num) {    trie *p=root;    for(int i=30;i>=0;i--) {        int id=((1<<i)&num)>>i;//取num的二进制第i位        if(p->next[id]==nullptr) {//num的二进制第i位不存在于字典树,新建节点            trie *q=(trie *)malloc(sizeof(trie));            for(int j=0;j<maxn;j++)                q->next[j]=nullptr;//新节点的下面的节点置为nullptr            q->v=1;//新节点的访问次数置为1            p->next[id]=q;//p指向下一节点            p=p->next[id];        }        else {            p->next[id]->v++;//否则该节点的访问次数++            p=p->next[id];        }    }}inline int ask(trie *root,int num) {    int res=0;//异或运算的性质,按如下方式取节点,如果num的第i位为1,则取下一节点值为0的节点,并res+=(1<<i),没有0节点取1节点,但是不加,反之亦然    trie *p=root;    for(int i=30;i>=0;i--) {        int id = ((1 << i)&num) >> i;        if(id) {            if(p->next[0]!=nullptr) {                p=p->next[0];                res+=(1<<i);            }            else                p=p->next[1];        }        else {            if(p->next[1]!=nullptr) {                p=p->next[1];                res+=(1<<i);            }            else                p=p->next[0];        }    }    return res;}inline void del(trie *root,int num) {    trie *p=root;//删除操作时找到第一个访问次数为1的节点,将其置为nullptr    for(int i=30;i>=0;i--) {        int id=((1<<i)&num)>>i;        if(p->next[id]!=nullptr) {            if(p->next[id]->v>1) {                p->next[id]->v--;                p=p->next[id];            }            else {                if(p->next[id]->v==1) {                    p->next[id]=nullptr;                    return ;                }            }        }    }}void dele(trie* T) {    int i;    if (T == NULL)        return ;//释放字典树的储存空间,非必需    for (i = 0; i<maxn; i++)        if (T->next[i] != NULL)            dele(T->next[i]);    free(T);    return ;}int main() {    int n;    n=read();    trie *root=(trie *)malloc(sizeof(trie));    for(int i=0;i<maxn;i++)        root->next[i]=nullptr;    bulid(root,0);//先把0存入字典树,否则缺值,也是题目要求    while(n--) {        char op[2];        int num;        scanf("%s",op);        num=read();        if(op[0]=='+')            bulid(root,num);//+操作,bulid字典树        if(op[0]=='-')            del(root,num);//-操作,删除字典树的num节点        if(op[0]=='?')            printf("%d\n",ask(root,num));//询问num异或的最大值    }    dele(root);    return 0;}



0 0