제출 #1330024

#제출 시각아이디문제언어결과실행 시간메모리
1330024sashimivssushiKlasika (COCI20_klasika)C++20
110 / 110
1568 ms434396 KiB
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define ll long long
#define db double
#define sti string
#define vt vector
#define INF ((int) 1e9)
#define MOD 1000000007
#define BASE 311
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pdd pair<db, db>
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << " = " << x << '\n';
#define bit(mask, i) ((mask >> i) & 1)
#define fi first
#define sc second

using namespace std;

const sti name = "";
const int MAXN = 2e5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

inline void file () {
    freopen ((name + ".inp").c_str (), "r", stdin);
    freopen ((name + ".out").c_str (), "w", stdout);
}

vt<int> adj[MAXN + 5];
int d[MAXN + 5], in[MAXN + 5], out[MAXN + 5], timeDFS = 0;

void dfs (int u) {
    in[u] = ++timeDFS;
    for (int& v : adj[u]) {
        d[v] ^= d[u];
        dfs(v);
    }
    out[u] = timeDFS;
}

struct BinaryTrie {
    struct Node {
        Node* child[2]; set<int> ins;
        Node () { child[0] = child[1] = nullptr; }
    };
    Node* root;
    int BIT;
    BinaryTrie (int _BIT = 31) {
        BIT = _BIT;
        root = new Node();
    }
    void insert (int u) {
        Node* cur = root;
        for (int j = BIT; j >= 0; --j) {
            int b = bit(d[u], j);
            if (cur->child[b] == nullptr)
                cur->child[b] = new Node();
            cur = cur->child[b];
            cur->ins.insert(in[u]);
        }
    }
    int query (int A, int B) {
        Node* cur = root;
        int res = 0;
        for (int j = BIT; j >= 0; --j) {
            int b = bit(d[A], j);
            bool ok = false;
            Node* c = cur->child[b ^ 1];
            if (c != nullptr) {
                auto it = c->ins.lower_bound(in[B]);
                if (it != end(c->ins) && *it <= out[B])
                    ok = true;
            }
            if (ok) {
                res += (1 << j);
                cur = c;
            }
            else cur = cur->child[b];
        }
        return res;
    }
};

struct Query {
    char type; int fi, sc;
    Query (char type, int fi, int sc) : 
    type(type), fi(fi), sc(sc) {}
};

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

    vt<Query> queries;
    int q, n = 1; cin >> q;
    while (q--) {
        sti cmd; int fi, sc;
        cin >> cmd >> fi >> sc;
        queries.emplace_back(cmd[0], fi, sc);
        if (cmd[0] == 'A') {
            adj[fi].emplace_back(++n);
            d[n] = sc;
        }
    }
    n = 1; dfs(1);
    BinaryTrie BT;
    BT.insert(1);
    for (auto& [type, fi, sc] : queries) {
        if (type == 'A') BT.insert(++n);
        else cout << BT.query(fi, sc) << '\n';
    }

    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...