国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Codeforces 706D Vasiliy's Multiset【貪心+字典樹】

2019-11-06 06:12:47
字體:
來源:轉載
供稿:網友

D. Vasiliy's Multisettime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 integerx from multiset A. It's guaranteed that at least onex 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 integery 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 setA.

Output

For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integerxi and some integer from the multisetA.

ExampleInput
10+ 8+ 9+ 11+ 6+ 1? 3- 8? 3? 8? 11Output
11101413Note

After first five Operations multiset A contains integers0, 8, 9, 11, 6 and 1.

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

題目大意:

一共有三種操作:

+ x 表示我們新添加一個元素x(可以重復添加).

- x 表示我們刪除一個元素x.

? x 詢問此時有的元素可以和x亦或的最大值,(最終值可能是本身);

思路:

按位壓縮為01串,然后將其按照三種操作處理:

+x -x分別就是增添和刪除操作。

?x我們就貪心去查詢即可。

1、我們按照長度為35位的二進制01字符串建樹。將所有數據都建到樹中。

因為最終解可能是本身,那么最初的時候添加到樹中一個全是0的字符串。

2、接下來對于每個查詢,同樣處理成35位二進制01字符串,對應進行查詢,如果當前位子是0,那么盡量往1那邊走,同理,如果當前位子是1,那么盡量往0那邊走即可。

Ac代碼:

#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;#define maxn 2typedef struct tree{    tree *nex[maxn];    int v;    int val;}tree;tree root;void init(){    for(int i=0;i<maxn;i++)    {        root.nex[i]=NULL;    }}void creat(char *str,int va){    int len=strlen(str);    tree *p=&root,*q;    for(int i=0;i<len;i++)    {        int id=str[i]-'0';        if(p->nex[id]==NULL)        {            q=(tree *)malloc(sizeof(root));            q->v=1;            for(int j=0;j<2;j++)            {                q->nex[j]=NULL;            }            p->nex[id]=q;        }        else        {            p->nex[id]->v++;        }        p=p->nex[id];        if(i==len-1)        {            p->val=va;        }    }}void del(char *str){    int len=strlen(str);    tree *p=&root;    for(int i=0;i<len;i++)    {        int id=str[i]-'0';        p->nex[id]->v--;        tree *tmp=p->nex[id];        if(p->nex[id]->v==0)        {            p->nex[id]=NULL;        }        p=tmp;    }    return ;}void find(char *str,int query){    int len=strlen(str);    tree *p=&root;    for(int i=0;i<len;i++)    {        int id=str[i]-'0';        if(p->nex[1-id]!=0)        {            p=p->nex[1-id];        }        else        p=p->nex[id];        if(p==NULL)        return ;        if(i==len-1)printf("%d/n",p->val^query);    }}int main(){    int n;    while(~scanf("%d",&n))    {        init();        char s[50];        s[36]='/0';        for(int i=0;i<=35;i++)s[i]='0';        creat(s,0);        while(n--)        {            char tmp[5];            char s[50];            scanf("%s",tmp);            int val;            scanf("%d",&val);            int a=val;            s[36]='/0';            for(int j=35;j>=0;j--)            {                if(a)                {                    s[j]=a%2+'0';                    a/=2;                }                else                {                    s[j]='0';                }            }            if(tmp[0]=='+')creat(s,val);            if(tmp[0]=='?')find(s,val);            if(tmp[0]=='-')del(s);        }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 凤庆县| 西昌市| 上高县| 靖宇县| 霍城县| 易门县| 峡江县| 双柏县| 左云县| 汝阳县| 从江县| 安化县| 电白县| 周口市| 寿宁县| 龙游县| 嘉义市| 广东省| 上栗县| 尖扎县| 稻城县| 福安市| 浙江省| 北流市| 西盟| 长海县| 英山县| 石阡县| 青河县| 兴山县| 邹平县| 汽车| 民乐县| 万源市| 徐水县| 建瓯市| 团风县| 万安县| 江都市| 柘荣县|