Submission #1349108

#TimeUsernameProblemLanguageResultExecution timeMemory
1349108BehruzbekXKlasika (COCI20_klasika)C++17
110 / 110
2198 ms494548 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<class T>
using os = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct persistent_trie{
    using T = persistent_trie;
    T* a[2];
    os<int> id;
    persistent_trie () {
        a[0] = a[1] = nullptr;
    }  
    void add (int x, int y) {
        T* root = this;
        for (int i = 30; i >= 0; --i) {
            int bit = (x >> i) & 1;
            if (!root->a[bit]) root->a[bit] = new T();
            root = root->a[bit];
            root->id.insert(y);
        }
    }
    int cnt (os<int> &a, int l, int r) {
        return a.order_of_key(r + 1) > a.order_of_key(l);
    }
    int get (int x, int l, int r) {
        T* root = this;
        int ans = 0;
        for (int i = 30; i >= 0; --i) {
            int bit = (x >> i) & 1, rev = bit ^ 1;
            if (root->a[rev] && cnt(root->a[rev]->id, l, r)) root = root->a[rev], ans |= 1 << i;
            else if (root->a[bit] && cnt(root->a[bit]->id, l, r)) root = root->a[bit];
        }
        return ans;
    }
};
constexpr int N = 2'00'000;
int q, tin[N], tout[N], d[N], rt = 0, tim = 0;
vector<int> a[N];
void dfs (int v) {
    tin[v] = tim++;
    for (int u : a[v]) {
        dfs(u);
    }
    tout[v] = tim - 1;
}
vector<tuple<int, int, int>> Q;
persistent_trie ds;

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> q;
    while (q--) {
        string s;
        cin >> s;
        int x, y;
        if (s == "Add") {
            cin >> x >> y, --x;
            ++rt;
            a[x].emplace_back(rt);
            d[rt] = d[x] ^ y;
            Q.emplace_back(0, x, y);
        }
        else {
            cin >> x >> y, --x, --y;
            Q.emplace_back(1, x, y);
        }
    }
    dfs(0);
    rt = 0;
    ds.add(d[0], tin[0]);
    for (auto [typ, x, y] : Q) {
        if (typ) {
            cout << ds.get(d[x], tin[y], tout[y]) << '\n';
        }
        else {
            ++rt;
            ds.add(d[rt], tin[rt]);
        }
    }
    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...