제출 #1360820

#제출 시각아이디문제언어결과실행 시간메모리
13608200xessamKlasika (COCI20_klasika)C++20
110 / 110
1322 ms419536 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5 , Bits = 30;

vector<array<int,2>> g[N] ;
int in[N] , out[N] , pref[N] , timer ;
void dfs0(int u) {
    in[u] = timer ++ ;
    for(auto &[v , c] : g[u]) {
        pref[v] = c ^ pref[u] ;
        dfs0(v) ;
    }
    out[u] = timer - 1 ;
}


struct BinaryTrie {
    struct Node {
        Node *nxt[2];
        int pref;
        set<int> in ;
        Node() {
            nxt[0] = nxt[1] = nullptr;
            pref = 0;
        }
    } *root = new Node();

    void insert(int x , int tm) {
        Node *cur = root;
        for (int i = 30; ~--i;) {
            int b = (x >> i) & 1;
            if (!cur->nxt[b]) cur->nxt[b] = new Node();
            cur = cur->nxt[b];
            cur->pref++;
            cur->in.insert(tm) ;
        }
    }

    int maxXOR(int x , int tin , int tout) {
        Node *cur = root;
        int ans = 0;
        for (int i = 30; ~--i;) {
            int b = (x >> i) & 1;
            if (cur->nxt[b ^ 1]) {
                auto it = cur->nxt[b ^ 1]->in.lower_bound(tin) ;
                if ( it == cur->nxt[b ^ 1]->in.end() or *it > tout) {
                    cur = cur->nxt[b];
                    continue;
                }
                ans |= (1 << i);
                cur = cur->nxt[b ^ 1];
            } else {
                cur = cur->nxt[b];
            }
        }
        return ans;
    }
};




int32_t main() {
    #ifdef ECPC
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int q ; cin >> q ;
    vector<array<int,3>> Q(q) ;
    int nodes = 1 ;
    for (int i = 0; i < q; i++) {
        string s ; cin >> s ;
        if (s == "Query") Q[i][0] = 1 ;
        else Q[i][0] = 0 ;
        cin >> Q[i][1] >> Q[i][2] ;
        if (Q[i][0] == 0) {
            int p = Q[i][1] , c = Q[i][2] ;
            g[p].push_back({++nodes, c}) ;
        }
    }

    dfs0(1);
    nodes = 1 ;
    BinaryTrie tr ;
    tr.insert(pref[1] , in[1]) ;
    for(auto [tp , a , b] : Q) {
        if (tp) {
            int path = pref[a] ;
            cout << tr.maxXOR(path , in[b] , out[b]) << "\n";
        }
        else {
            ++nodes ;
            tr.insert(pref[nodes] , in[nodes]) ;
        }
    }

}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…