제출 #1289016

#제출 시각아이디문제언어결과실행 시간메모리
1289016nemkhoKlasika (COCI20_klasika)C++17
33 / 110
1725 ms562176 KiB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int n, cnt, sta[N], q, en[N];
ll XOR[N];
vector <int> a[N];
struct haha
{
    int from, to, type;
};
vector<haha> query;
struct Trie
{
    set <int> tour[30*N];
    int child[30*N][2], m = 0;
    void add (ll x, int st)
    {
        int u = 0;
        for (int i = 30; i >= 0; i--)
        {
            bool k = ((x >> i) & 1);
            if (child[u][k] == 0)
                child[u][k] = ++m;
            tour[u].insert(st);
            u = child[u][k];
        }
        tour[u].insert(st);
    }
    ll get (ll x, int l, int r)
    {
        int u = 0;
        ll res = 0;
        for (int i = 30; i >= 0; i--)
        {
            bool k = ((x >> i) & 1);
            if (child[u][k^1] != 0)
            {
                set <int>::iterator it = tour[child[u][k^1]].lower_bound(l);
                if (it == tour[child[u][k^1]].end() || *it > r)
                    u = child[u][k];
                else
                {
                    u = child[u][k^1];
                    res += (1LL << i);
                }
            }
            else
                u = child[u][k];
        }
        return res;
    }
} trie;

void inp()
{
    n = 1;
    cin >> q;
    for (int i = 1; i <= q; i++)
    {
        string task;
        cin >> task;
        if (task == "Add")
        {
            int x, y;
            cin >> x >> y;
            n++;
            XOR[n] = XOR[x] ^ y;
            a[x].push_back(n);
            a[n].push_back(x);
            query.push_back({n, -1, 1});
        }
        else
        {
            int x, y;
            cin >> x >> y;
            query.push_back({x, y, 0});
        }
    }
}
void dfs (int u, int pre)
{
    sta[u] = ++cnt;
    for (int v : a[u])
    {
        if (v == pre)
            continue;
        dfs(v, u);
    }
    en[u] = cnt;
}
void solve()
{
    dfs(1, 0);
    trie.add(0, 1);
    for (haha i : query)
    {
        int u = i.from, v = i.to, task = i.type;
        if (task == 0)
            cout << trie.get(XOR[u], sta[v], en[v]) << "\n";
        else
            trie.add(XOR[u], sta[u]);
    }
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    inp();
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...