817E - Choosing The Commander - CodeForces Solution


bitmasks data structures trees *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

const int nmax = 1e5;

int q;

struct trieNode{
    trieNode * nxt[2];
    int nr;
    trieNode() : nr(0) {nxt[0] = nxt[1] = nullptr;}
};

struct trie{
    trieNode * root;
    trie(){
        root = new trieNode;
    }
    void insert(int x, int v){
        trieNode * nod = root;
        for(int i = 26; i >= 0; i--){
            if(nod -> nxt[(x >> i) & 1] == nullptr)
                nod -> nxt[(x >> i) & 1] = new trieNode;
            nod = nod -> nxt[(x >> i) & 1];
            nod -> nr += v;
        }
    }
    int cmp(int x, int l){
        trieNode * nod = root;
        int ans = 0;
        for(int i = 26; i >= 0; i--){
            if((l >> i) & 1){
                ans += (nod -> nxt[((x >> i) & 1)] == nullptr ? 0 : nod -> nxt[((x >> i) & 1)] -> nr);
                nod = nod -> nxt[((x >> i) & 1) ^ 1];
            } 
            else{
                nod = nod -> nxt[((x >> i) & 1)];
            }
            if(nod == nullptr)
                return ans;
        }
        return ans;
    }
};

void solve(){
    trie arb;
    cin >> q;
    while(q--){
        int c;
        cin >> c;
        if(c == 1){
            int p;
            cin >> p;
            arb.insert(p, 1);
        }
        else if(c == 2){
            int p;
            cin >> p;
            arb.insert(p, -1);
        }
        else{
            int p, l;
            cin >> p >> l;
            cout << arb.cmp(p, l) << "\n";
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    // freopen("test.in", "r", stdin);
    // freopen("test.out", "w", stdout);

    int q = 1;
    // cin >> q;
    while(q--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro