Vasiliy's Multiset (异或字典树)

题目

题意:
根据题目定义的三个操作,增加删除和查找异或最大的值。
+ x: 表示向集合中添加一个元素x
- x:表示删除集合中值为x的一个元素
? x:表示查询集合中与x异或的最大值为多少

input
10
+ 8
+ 9
+ 11
+ 6
+ 1
? 3
- 8
? 3
? 8
? 11
output
11
10
14
13

思路:
一种字典树的应用,当时没看出来,因为没遇到过…..
先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除,
对于查询,尽量让0和1XOR为最大的,所以,对于给定数,要去尽量他的XOR数,找到就加上,找不到,找下一个,从而得最大的情况.

#include <cstdio>
#include <string>
#include <iostream>
#include <cctype>
#include <stack>
using namespace std;

typedef long long LL;
const int maxn = 4e6 + 5;
int n,m; 

inline LL Max(LL a, LL b){  return a < b ? b : a; }
inline LL Min(LL a, LL b){  return a > b ? b : a; }
inline int Max(int a, int b){  return a < b ? b : a; }
inline int Min(int a, int b){  return a > b ? b : a; }
int ch[maxn][2];
int t[maxn];
int cnt = 0;

void add(int x){
    int k = 1;
    for(int i = 30; i >= 0; --i){
        int j = ((1<<i) & x) > 0;
        if(!ch[k][j])  ch[k][j] = ++cnt;
        k = ch[k][j];
        ++t[k];
    }
}

void del(int x){
    int k = 1;
    for(int i = 30; i >= 0; --i){
        int j = ((1<<i) & x) > 0;
        k = ch[k][j];
        --t[k];
    }
}

int query(int x){
    int k = 1;
    int ans = 0;
    for(int i = 30; i >= 0; --i){
        int j = ((1<<i) & x) == 0;
        if(t[ch[k][j]]){
            ans |= (1<<i);
            k = ch[k][j];
        }
        else k = ch[k][1-j];
    }
    return ans;
}

int main(){
    while(scanf("%d", &n) == 1){
        cnt = 1;
        memset(ch, 0, sizeof(ch));
        memset(t, 0, sizeof(t));
        add(0);
        while(n--){
            char s[3];
            int x;
            scanf("%s %d", s, &x);
            if(s[0] == '+')  add(x);
            else if(s[0] == '-')  del(x);
            else   printf("%d\n", query(x));
        }
    }
    return 0;
}

然而在其他博客上还看到了直接用multiset来模拟位运算的做法…然而并不是很明白,先贴一下….

假设x的二进制形式为0000 1000(8),那么要想取得最大值, 则y应该与x的二进制形式相反才可以, 如0111 0111(119), x^y = 0111 1111(127)

所以我们尽可能使y的二进制形式  与x对应0位置  从左到右 存在1, (可能有点拗口)继续以x的二进制形式为0000 1000(8), 如果存在0100 0000(64), 0011 1111(63)这两个数,我们肯定会选64, 就是这个从左向右选择1的道理

那么我们怎么判断最高为是否为1呢, 我们可以用(1 << i)表示最高位为1, ( 注 i :  29 -> 0) 然后 ~x&(1 << i)就可以得到最高位了。最后再用个数ans记录当前能取到的最大值

ans = ans | (~x&(1 << i)), 判断这个ans是否在multimap中, 如果不存在就返回到之前的ans, ans = ans ^ (1 << i)

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
multiset<int>s;
int main()
{
    ios::sync_with_stdio(false);
    int n, x;
    char op;
    cin >> n;
    s.insert(0);
    while(n--){
        cin >> op >> x;
        if(op == '+'){
            s.insert(x);//添加一个数
        }else if(op == '-'){
            s.erase(s.find(x));//删除一个数
        }else if(op == '?'){
            int ans = 0;
            for(int i = 29; i >= 0; i--){
                ans |= (~x&(1 << i));//求出最高位的数,然后不断往下搜
                auto it  = s.lower_bound(ans);//二分搜索, 得到一个
                if(it == s.end() || *it >= ans + (1 << i)){//没有搜到, 第一个是搜到末尾了,第二个是搜到了一个大于ans的数,具体可以查阅lower_bound函数
                    ans ^= 1<<i;
                }
            }
            x ^= ans;
            cout << x << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值