Codeforces 706D Vasiliy's Multiset (字典树求异或最大值)

模板
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e7+5;
int ch[maxn][2];
int sum[maxn];
int le;

void add(int x)
{
    int p=1;
    for(int i=31;i>=0;i--)
    {
        int a=(x&(1<<i))>0;
        if(ch[p][a]==0)
        {
            ch[p][a]=++le;
        }
        p=ch[p][a];
        sum[p]++;
    }
}

void del(int x)
{
    int p=1;
    for(int i=31;i>=0;i--)
    {
        int a=(x&(1<<i))>0;
        p=ch[p][a];
        sum[p]--;
    }
}

void query(int x)
{
    long long p=1,ans=0;
    for(int i=31;i>=0;i--)
    {
        int a=(x&(1<<i))==0;
        if(sum[ch[p][a]])
        {
            p=ch[p][a];
            ans=ans|(1<<i);
        }
        else p=ch[p][1-a];
    }
    printf("%I64d\n",ans);
}

int main()
{
    memset(ch,0,sizeof(ch));
    memset(sum,0,sizeof(sum));
    int t;  scanf("%d",&t);
    le=1;
    add(0);
    getchar();
    for(int i=0;i<t;i++)
    {
        char s[2];int c;
        scanf("%s%d",s,&c);
        if(s[0]=='+')   add(c);
        else if(s[0]=='-')  del(c);
        else if(s[0]=='?')  query(c);
    }
}

<div style="margin: 0px; padding: 0px; font-family: verdana, arial, sans-serif; font-size: 14px; position: relative;"><div id="pageContent" class="content-with-sidebar" style="margin: 1em; padding: 1em 0px 0px; min-height: 20em;"><div class="problemindexholder" problemindex="D" style="margin: 0px; padding: 0px; position: relative;"><div class="ttypography" style="margin: 1.5em 0px 0px !important; padding: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; line-height: 21px; color: rgb(34, 34, 34); font-size: 1.4rem; word-wrap: break-word;"><div class="problem-statement" style="margin: 0.5em; padding: 0px; line-height: 1.5em; font-size: 1.4rem;"><div class="header" style="margin: 0px 0px 1em; padding: 0px; text-align: center;"><div class="title" style="margin: 0px 0px 0.5em; padding: 0px; font-size: 21px;">D. Vasiliy's Multiset</div><div class="time-limit" style="margin: 0px auto; padding: 0px;"><div class="property-title" style="margin: 0px; padding: 0px 4px 0px 0px; display: inline;">time limit per test</div>4 seconds</div><div class="memory-limit" style="margin: 0px auto; padding: 0px;"><div class="property-title" style="margin: 0px; padding: 0px 4px 0px 0px; display: inline;">memory limit per test</div>256 megabytes</div><div class="input-file" style="margin: 0px auto; padding: 0px;"><div class="property-title" style="margin: 0px; padding: 0px 4px 0px 0px; display: inline;">input</div>standard input</div><div class="output-file" style="margin: 0px auto; padding: 0px;"><div class="property-title" style="margin: 0px; padding: 0px 4px 0px 0px; display: inline;">output</div>standard output</div></div><div style="margin: 0px; padding: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em; margin-top: 0px !important; margin-bottom: 1em !important;">Author has gone out of the stories about Vasiliy, so here is just a formal task description.</p><p style="margin-top: 1.5em; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em;">You are given <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>q</em></span> queries and a multiset <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span>, initially containing only integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;">0</span>. There are three types of queries:</p><ol style="margin: 0.5em 0px 0px 1em; padding: 0px; list-style-position: outside; font-size: 1em; line-height: 1.5em;"><li style="margin: 0px 0px 0px 0.8em; padding: 0px; line-height: 1.5em; font-size: 1em;">"<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">+ x</span>" — add integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em></span> to multiset <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span>.</li><li style="margin: 0px 0px 0px 0.8em; padding: 0px; line-height: 1.5em; font-size: 1em;">"<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">- x</span>" — erase one occurrence of integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em></span> from multiset <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span>. It's guaranteed that at least one <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em></span> is present in the multiset <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span> before this query.</li><li style="margin: 0px 0px 0px 0.8em; padding: 0px; line-height: 1.5em; font-size: 1em;">"<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">? x</span>" — you are given integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em></span> and need to compute the value <img align="middle" class="tex-formula" src="http://codeforces.com/predownloaded/af/1b/af1b4d5401ef843e35de22bbd1367fa78816de31.png" style="border: medium none; font-family: "times new roman", sans-serif; vertical-align: middle; margin: 0px; position: relative; bottom: 2px; max-width: 100%; max-height: 100%;" alt="" />, i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em></span> and some integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>y</em></span> from the multiset <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span>.</li></ol><p style="margin-top: 1.5em; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em;">Multiset is a set, where equal elements are allowed.</p></div><div class="input-specification" style="margin: 0px; padding: 0px;"><div class="section-title" style="margin: 0px; padding: 0px; font-size: 16.1px; font-weight: bold;">Input</div><p style="margin-top: 1.5em; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em;">The first line of the input contains a single integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>q</em></span> (<span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;">1 ≤ <em>q</em> ≤ 200 000</span>) — the number of queries Vasiliy has to perform.</p><p style="margin-top: 1.5em; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em;">Each of the following <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>q</em></span> lines of the input contains one of three characters '<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">+</span>', '<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">-</span>' or '<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">?</span>' and an integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em><span class="lower-index" style="font-size: 13.125px; line-height: 0; position: relative; vertical-align: baseline; bottom: -0.25em;"><em>i</em></span></span> (<span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;">1 ≤ <em>x</em><span class="lower-index" style="font-size: 13.125px; line-height: 0; position: relative; vertical-align: baseline; bottom: -0.25em;"><em>i</em></span> ≤ 10<span class="upper-index" style="font-size: 13.125px; line-height: 0; position: relative; vertical-align: baseline; top: -0.5em;">9</span></span>). It's guaranteed that there is at least one query of the third type.</p><p style="margin-top: 1.5em; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em;">Note, that the integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;">0</span> will always be present in the set <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span>.</p></div><div class="output-specification" style="margin: 0px 0px 1em; padding: 0px;"><div class="section-title" style="margin: 0px; padding: 0px; font-size: 16.1px; font-weight: bold;">Output</div><p style="margin-top: 1.5em; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 1em; line-height: 1.4em;">For each query of the type '<span class="tex-font-style-tt" style="font-size: 15.4px; font-family: "courier new", monospace;">?</span>' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>x</em><span class="lower-index" style="font-size: 13.125px; line-height: 0; position: relative; vertical-align: baseline; bottom: -0.25em;"><em>i</em></span></span> and some integer from the multiset <span class="tex-span" style="font-size: 17.5px; font-family: "times new roman", sans-serif; white-space: nowrap;"><em>A</em></span>.</p></div><div class="sample-tests" style="margin: 0px; padding: 0px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; font-size: 0.9em;"><div class="section-title" style="margin: 0px; padding: 0px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14.49px; font-weight: bold;">Example</div><div class="sample-test" style="margin: 0px; padding: 0px;"><div class="input" style="margin: 0px; padding: 0px; border: 1px solid rgb(136, 136, 136);"><div class="title" style="margin: 0px; padding: 0.25em; font-size: 1.3em; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(136, 136, 136); text-transform: lowercase; font-weight: bold;">input</div><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">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 089116 and 1.

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



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值