Submission #1312828

#TimeUsernameProblemLanguageResultExecution timeMemory
1312828crazycoder00Klasika (COCI20_klasika)C++20
110 / 110
3208 ms475668 KiB
#include <bits/stdc++.h>
using namespace std;

// Template
// ==============================================
    // pbds
    // #include <ext/pb_ds/assoc_container.hpp>
    // #include <ext/pb_ds/tree_policy.hpp>
    // using namespace __gnu_pbds;
    // template<typename T, typename comp = less<T>>
    // using ordered_set =  tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>;

    // Debugging
    #ifdef LOCAL
    #include "debug.h"
    #else
    #define debug(...)
    #define see(x)
    #endif

    typedef long long ll;
    typedef vector<int> VI;
    typedef vector<long long> VLL;
    typedef vector<bool> VB;
    typedef vector<vector<int>> VVI;
    typedef pair<int, int> PI;
    typedef pair<ll, ll> PLL;
    typedef vector<pair<int, int>> VPI;

    #define pb push_back
    #define ff first
    #define ss second
    #define mp make_pair
    #define all(a) a.begin(), a.end()
    #define revall(a) a.rbegin(), a.rend()

    #define loop(i, s, e) for (int i = s; i < e; ++i)
    #define inp(v) for (auto& x : v) cin >> x
    #define outp(v, st) for (int i = st, n = v.size(); i < n; ++i) cout << v[i] << " \n"[i == n - 1]

    #define nl "\n"
    #define yep cout << "YES\n"
    #define nope cout << "NO\n"

    #define INF (int) 1e9
    #define INFL (ll) 1e18
    #define MOD 998244353
    // #define MOD 1000000007
    #define MAXN 2000002
// =============================================

struct trienode
{
    int child[2];
    set<int> tm;

    trienode()
    {
        child[0] = child[1] = 0;
    }
};

struct trie
{
    vector<trienode> nodes;

    trie()
    {
        nodes.pb(trienode());
    }

    void insert(int x, int disc)
    {
        int cur = 0;
        for (int i = 30; i >= 0; --i)
        {
            int nxt = (bool) (x & (1 << i));

            if (!nodes[cur].child[nxt])
            {
                nodes[cur].child[nxt] = nodes.size();
                nodes.pb(trienode());
            }

            cur = nodes[cur].child[nxt];
            nodes[cur].tm.insert(disc);
        }
    }

    int search(int x, int start, int end)
    {
        int cur = 0, ans = 0;
        for (int i = 30; i >= 0; --i)
        {
            int nxt = 1 - (bool) (x & (1 << i));

            if (!nodes[cur].child[nxt]) nxt = 1 - nxt;
            else
            {
                auto it = nodes[nodes[cur].child[nxt]].tm.lower_bound(start);
                if (it == nodes[nodes[cur].child[nxt]].tm.end() || *it > end)
                    nxt = 1 - nxt;
            }

            ans |= nxt << i;
            cur = nodes[cur].child[nxt];
        }

        return ans;
    }
};

struct query
{
    bool tp;
    int u, v;
};

int tim = 0;
void dfs(int u, int p, VVI& tree, VI& start, VI& end)
{
    start[u] = tim++;

    for (int v : tree[u])
    {
        if (v == p) continue;

        dfs(v, u, tree, start, end);
    }

    end[u] = tim++;
}

void solve(int tc)
{
    int q; cin >> q;
    vector<query> qry(q);

    int n = 2;
    VVI tree = VVI(2);
    VI xo(2);

    loop(i, 0, q)
    {
        string s; cin >> s;

        cin >> qry[i].u >> qry[i].v;

        if (s == "Add")
        {
            qry[i].tp = false;
            tree[qry[i].u].pb(n);
            tree.pb({qry[i].u});

            xo.pb(xo[qry[i].u] ^ qry[i].v);
            qry[i].v = n++;
        }
        else qry[i].tp = true;
    }

    VI vis_start(n + 1), vis_end(n + 1);
    dfs(1, 0, tree, vis_start, vis_end);

    trie tri;
    tri.insert(0, 0);
    for (auto& [tp, u, v] : qry)
    {
        if (!tp)
        {
            tri.insert(xo[v], vis_start[v]);
        }
        else
        {
            int x = xo[u];
            cout << (x ^ tri.search(x, vis_start[v], vis_end[v])) << nl;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t;
    int x = t;
    while(t--) solve(x - t);
    
    #ifdef LOCAL
    cerr << "Execution time: " << 1000.f * clock() / CLOCKS_PER_SEC << " ms." << nl;
    #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...