Codeforces 706D Trie树/multiset

题目:http://codeforces.com/problemset/problem/706/D
题意:

+表示吧这个数加到集合中,-表示把这个数从集合中减去一次,?表示集合里面的一个y使的x^y最大

分析:

要使异或最大,那么尽量从高位使两个数不同即可。

Trie树。

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+9;
int a[N][2],cal[N];
int tot;
void add(int x,int y) //插入或删除一个元素
{
    int cur=0;
    for(int i=31;i>=0;i--){
        int t=(x>>i)&1;
        if(!a[cur][t])a[cur][t]=tot++;
        cur=a[cur][t];
        cal[cur]+=y;
    }
}

int query(int x)
{
    int cur=0,ans=0;
    for(int i=31;i>=0;i--){
        int t=(x>>i)&1;
        if(cal[a[cur][t^1]]){
            ans+=(1<<i);
            cur=a[cur][t^1];
        }
        else cur=a[cur][t];
    }
    return ans;
}
int main()
{
    int n,x;
    char op;
    tot=1;
    add(0,1);
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        cin>>op>>x;
        if(op=='+'){
            add(x,1);
        }
        else if(op=='-'){
            add(x,-1);
        }
        else{
            printf("%d\n",query(x));
        }
    }
    return 0;
}
/*
Input
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11

Output
11
10
14
13
*/

或者可以直接用multiset模拟:

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+9;

int main()
{
    int n,x;
    char op;
    multiset<int>s;
    s.insert(0);
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        cin>>op>>x;
        if(op=='+'){
            s.insert(x);
        }
        else if(op=='-'){
            s.erase(s.find(x));
        }
        else{
            int ans=0;
            for(int i=31;i>=0;i--){
                ans|=(~x&(1<<i));
                multiset<int>::iterator it=s.lower_bound(ans);
                if(it==s.end()||*it>=ans+(1<<i)){
                   // cout<<ans<<endl;
                    ans^=1<<i;
                }
            }
            printf("%d\n",x^ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值