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;
}
發佈了68 篇原創文章 · 獲贊 23 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章