Submission #673796

#TimeUsernameProblemLanguageResultExecution timeMemory
673796BackNoobKlasika (COCI20_klasika)C++14
110 / 110
2228 ms448892 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
 
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
    if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
    if (a < b) {a = b; return true;} return false;
}

struct Node{
    Node* child[2];
    set<int> s;

    Node(){
        child[0] = child[1] = nullptr;
    }
};

 
int q;
int sta[mxN];
int fin[mxN];
int dist[mxN];
vector<pair<int, int>> adj[mxN];
pair<int, int> qr[mxN];
string cmd[mxN];
int timer = 0;
Node *root = new Node();

void add_trie(int x, int idx)
{
    Node* p = root;
    for(int i = 29; i >= 0; i--) {
        int nxt = BIT(x, i);
        if(p -> child[nxt] == nullptr) p -> child[nxt] = new Node();
        p = p -> child[nxt];
        (p -> s).insert(idx);
    }
}

void dfs_pre(int u, int p)
{
    sta[u] = ++timer;
    for(auto it : adj[u]) {
        int v = it.fi;
        int c = it.se;
        if(v == p) continue;
        dist[v] = (dist[u] ^ c);
        dfs_pre(v, u);
    }
    fin[u] = timer;
}

int findmaxxor(int x, int l, int r)
{
    Node* p = root;
    int res = 0;
    for(int i = 29; i >= 0; i--) {
        int nxt = !BIT(x, i);
        if(p -> child[nxt] == nullptr) {
            p = p -> child[!nxt];
        }
        else {
            auto it = (p -> child[nxt] -> s).lower_bound(l);
            if(it != (p -> child[nxt] -> s).end() && *it <= r) {
                res |= (1 << i);
                p = p -> child[nxt];
            }
            else {
                p = p -> child[!nxt];
            }
        }
    }
    return res;
}
 
void solve()
{
    cin >> q;
    int node = 1;
    for(int i = 1; i <= q; i++) {
        cin >> cmd[i] >> qr[i].fi >> qr[i].se;  
        if(cmd[i] == "Add") {
            ++node;
            adj[node].pb({qr[i].fi, qr[i].se});
            adj[qr[i].fi].pb({node, qr[i].se});
        }
    }

    dfs_pre(1, -1);
    node = 1;
    add_trie(dist[node], sta[node]);
    for(int i = 1; i <= q; i++) {
        if(cmd[i] == "Add") {
            ++node;
            add_trie(dist[node], sta[node]);
        }
        else {
            cout << findmaxxor(dist[qr[i].fi], sta[qr[i].se], fin[qr[i].se]) << endl;
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    // freopen(TASK".inp" , "r" , stdin);
    // freopen(TASK".out" , "w" , stdout);
 
    int tc = 1;
    //cin >> tc;
    while(tc--) {
        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...