Submission #1344530

#TimeUsernameProblemLanguageResultExecution timeMemory
1344530_tesla_Klasika (COCI20_klasika)C++20
110 / 110
1713 ms518260 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define i64 long long
/*
 * الاستمرارية تصنع الكمال *
*/
struct Trie {

    struct Node {
        int next[2];
        set<int> id;
        Node () {
            for (int i = 0; i < 2; i++) next[i] = -1;
        }
    };

    vector<Node> adj;
    int idx;
    Trie () {
        adj.emplace_back(Node());
        idx = 1;
    }

    void insert(int x, int node){
        int cur = 0;
        string s = bitset<31>(x).to_string();
        adj[cur].id.insert(node);
        for (char c: s){
            if (adj[cur].next[c - '0'] == -1){
                adj[cur].next[c - '0'] = idx++;
                adj.emplace_back(Node());
            }
            cur = adj[cur].next[c - '0'];
            adj[cur].id.insert(node);
        }
    }

    int query(int x, int in, int out){
        int cur = 0;
        int ret = 0;
        int gun = (1 << 30);
        string s = bitset<31>(x).to_string();
        for (char c: s){
            if (adj[cur].next[1 - (c - '0')] == -1){
                cur = adj[cur].next[c - '0'];
            }
            else {
                int nxt = adj[cur].next[1 - (c - '0')];
                auto itr = adj[nxt].id.lower_bound(in);
                if (itr != adj[nxt].id.end() && *itr <= out){
                    cur = nxt;
                    ret += gun;
                }
                else {
                    cur = adj[cur].next[c - '0'];
                }
            }
            gun /= 2ll;
        }

        return ret;
    }

    void trav(int x){
        int cur = 0;
        string s = bitset<31>(x).to_string();
        string check = "";
        for (char c: s){
            cur = adj[cur].next[c - '0'];
            check += c;
            cout << check << " -> ";
            for (auto elem: adj[cur].id) cout << elem << " ";
            cout << "\n";
        }
    }

};

void solve() {
    int n ; cin >> n ;
    vector<array<int, 3>>qu(n) ;
    vector<array<int, 2>>g[n];
    vector<int> val{0} ;
    int cur = 1 ;
    for (int i = 0 ; i < n ; i ++) {
        string s ; cin >> s ;
        int x, y ; cin >> x >> y ;
        if (s == "Add") {
            g[x - 1].push_back({cur ++, y}) ;
            val.push_back(val[x - 1] ^ y) ;
        }
        qu[i] = {s == "Add", x, y};
    }
    vector<int>tin(cur), tout(cur) ;
    int timer = 0 ;
    function<void(int)> dfs = [&](int u) {
        tin[u] = timer ++ ;
        for (auto [v, w] : g[u]) {
            dfs(v) ;
        }
        tout[u] = timer - 1 ;
    };
    dfs(0) ;

    Trie tr ;
    tr.insert(0, tin[0]) ;

    cur = 1 ;
    cerr << val.size() << endl;
    for (auto [t, x, y] : qu) {
        x -- ;
        if (t) {
            tr.insert(val[cur], tin[cur]) ;
            cur ++ ;
        }else {
            y -- ;
            cout << tr.query(val[x], tin[y], tout[y]) << '\n' ;
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    clock_t start = clock();
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
    int tc = 1;
    // cin >> tc;
    for (int ttt = 1; ttt <= tc; ttt++) {
#ifdef LOCAL
        cout << "Case " << ttt << ": ";
#endif
        solve();
    }
#ifdef LOCAL
    clock_t end = clock();
    double time_taken = double(end - start) / CLOCKS_PER_SEC;
    cout << "\n\n\n\n\nTime taken: " << time_taken << " seconds\n";
#endif
    return 0 ;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...