Submission #1333128

#TimeUsernameProblemLanguageResultExecution timeMemory
1333128ahmedredaKlasika (COCI20_klasika)C++20
110 / 110
561 ms345160 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define int long long
#define endl "\n"
//-------------------\\

constexpr int N = 2e5 + 5,oo = 2e18, mod = 1e9 + 7;

struct Binary_Trie {
private
:
    struct Node {
        int child[2]{};
        int cnt = 0, isEnd = 0,mn = oo;

        int &operator[](int x) {
            return child[x];
        }
    };

    vector<Node> node;

public
:
    Binary_Trie() {
        node.emplace_back();
    }

    int newNode() {
        node.emplace_back();
        return node.size() - 1;
    }

    int sz(int x,int t) {
        return node[x].cnt and node[x].mn < t;
    }

    int M = 30;

    void update(int x, int op,int t) {
        // op -> 1 add || op -> -1 erase
        int cur = 0;
        for (int i = M - 1; i >= 0; --i) {
            int c = x >> i & 1;
            if (node[cur][c] == 0)
                node[cur][c] = newNode();

            cur = node[cur][c];
            node[cur].cnt += op;
            node[cur].mn = min(node[cur].mn,t);
        }
        node[cur].isEnd += op;
    }

    int max_xor(int x,int t) {
        int cur = 0, res = 0;
        for (int i = M - 1; i >= 0; --i) {
            int cx = (x >> i) & 1;
            if (sz(node[cur][!cx],t)) {
                res |= (1LL << i);
                cur = node[cur][!cx];
            } else {
                cur = node[cur][cx];
            }
        }
        return res;
    }
};

struct node {
    vector<pair<int,int>> vals;
    Binary_Trie trie;
};

vector<vector<pair<int,int>>> queries(2);
vector<vector<int>> adj(2);
vector<pair<int,int>> ans; // time, ans
vector<int> pref(2),added_time(2);

node go(int u) {
    node cur;
    cur.vals.push_back({pref[u],added_time[u]});
    cur.trie.update(pref[u],1,added_time[u]);

    for (auto v : adj[u]) {
        auto ret = go(v);

        if (ret.vals.size() > cur.vals.size()) swap(ret,cur);
        for (auto [x,t] : ret.vals) {
            cur.trie.update(x,1,t);
            cur.vals.push_back({x,t});
        }
    }

    for (auto [st,t] : queries[u]) ans.push_back({t , cur.trie.max_xor(pref[st],t)});

    return cur;
}

void solve() {
    int q; cin >> q;
    int time = 1;
    int nxt = 2;

    while (q--) {
        string s; cin >> s;
        int u,x; cin >> u >> x;

        if (s[0] == 'A') {
            adj.push_back({});
            pref.push_back({});
            queries.push_back({});
            added_time.push_back({});

            added_time[nxt] = time;
            pref[nxt] = pref[u] ^ x;
            adj[u].push_back(nxt++);
        }
        else {
            queries[x].push_back({u,time});
        }

        time++;
    }

    go(1);

    sort(ans.begin(),ans.end());

    for (auto [_,val] : ans) cout << val << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout << fixed << setprecision(9);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    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...