제출 #1329587

#제출 시각아이디문제언어결과실행 시간메모리
1329587arianshabaaniKlasika (COCI20_klasika)C++20
0 / 110
5090 ms36984 KiB
// #inclue <iostream>
#include <bits/stdc++.h>
using namespace std;
//ifstream fin("input.txt");
//ofstream fout("output.txt");

// #CONFIG
string outone = "YES";
string outtwo = "NO";

const int inf=1e9;
const int mod=1e9+7;
const int modd = 998244353;
const int maxn25=2*1e5+10;
const int maxn55=5*1e5+10;
const int maxn5=1e5+10;
const int maxn7=1e7+10;
const int maxn9=1e9+10;

#define ll long long int
#define ull unsigned long long int
#define pass cerr << "Pass shod!" << endl
#define testc ll tt; cin >> tt; while(tt--)
#define pb push_back
#define mp(i, j) make_pair(i, j)
#define moew cin.tie(0); cout.tie(0); ios::sync_with_stdio(false)
#define one first
#define two second
#define enld endl
#define neld endl
#define ednl endl
#define vectoR vector
#define cosnt const
#define ppq priority_queue
#define t1 __builtin_popcount
#define rip return 0
#define biset bitset
#define Endl endl
#define moz int m = (l+r)/2; int lc = 2*in; int rn = lc+1
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef priority_queue<pll, vector<pll>, greater<pll>> pq;
typedef int itn;

void yes() { cout << "YES" << endl; }
void no() { cout << "NO" << endl; }
void out1() { cout << outone << endl; }
void out2() { cout << outtwo << endl; }
ll max3 (ll a, ll b, ll c) { return max(a, max(b, c)); }
ll min3 (ll a, ll b, ll c) { return min(a, min(b, c)); }
ll ceill (ll a, ll b) { return (a+b-1)/b; }
long double flor(long double a){ return floor(a+0.5); }
ll logg (ll a, ll b) { return log(a)/log(b); }

cosnt int maxn = maxn25;

vector<pair<int, pii>> qq;
int pxor[maxn];
int st[maxn];
int fn[maxn];
vector<int> a[maxn];
int timee[maxn];
int t =1;
int ans[maxn];
itn pw[50];

void dfs(int v, int p){
    st[v]=t;
    t++;
    for (auto i : a[v]){
        if (i==p) continue;
        dfs(i, v);
    }
    fn[v]=t;
}

bool check(int s, int i){
    return bool (s & (1<<i));
}

struct node{
    int ch[3];
    set<pii> s;
    int par;
    node(){
        ch[0]=-1;
        ch[1]=-1;
    }
};

struct trie{
    vector<node> o;
    trie(){
        node root;
        o.pb(root);
    }
    void add(int s, int stm, int tm){
        int v = 0;
        for (int i=30; i>=0; i--){
            int c = check(s, i);
            if (o[v].ch[c]==(-1)){
                o[v].ch[c]=o.size();
                node nw;
                nw.par=v;
                o.pb(nw);
            }
            v=o[v].ch[c];
            if (i==0){
                o[v].s.insert({stm, tm});
            }
        }
        while (v!=0){
            for (auto [b, c] : o[v].s){
                o[o[v].par].s.insert({b, c});
            }
            v=o[v].par;
        }
    }
    void shit(){
        int cntt=0;
        for (auto i : o){
            o[cntt].s.insert({inf, inf});
            cntt++;
        }
    }
    int get(int s, int stm, int fnm, int tm){
        int v = 0;
        itn ans = 0;
        for (int i=30; i>=0; i--){
            int c = check(s, i);
            if (o[v].ch[1-c]!=(-1)){
                pii pp = {stm, 0};
                pii p = *(o[o[v].ch[1-c]].s.lower_bound(pp));
                if (p.one<fnm and p.two<=tm){
                    ans+=pw[i];
                    v=o[v].ch[1-c];
                    continue;
                }
            }
            v=o[v].ch[c];
        }
        return ans;
    }
};

int main(){
    moew;
    pw[0]=1;
    for (int i=1; i<=30; i++){
        pw[i]=pw[i-1]*2;
    }
    int q;
    cin >> q;
    int n=1;
    itn cnt = 1;
    pxor[1]=0;
    for (int i=1; i<=q; i++){
        ans[i]=-1;
        string s;
        cin >> s;
        if (s=="Add"){
            int b, c;
            cin >> b >> c;
            n++;
            cnt++;
            timee[cnt]=i;
            a[b].pb(cnt);
            pxor[cnt]=c^pxor[b];
        }else {
            int b, c;
            cin >> b >> c;
            qq.pb({i, {b, c}});
        }
    }
    dfs(1, 0);
    trie tr;
    for (int i=1; i<=n; i++){
        tr.add(pxor[i], st[i], timee[i]);
    }
    tr.shit();
    for (auto [in, k] : qq){
        auto [b, c]=k;
        ans[in]=tr.get(pxor[b], st[c], fn[c], in);
    }
    for (int i=1; i<=q; i++){
        if (ans[i]!=(-1)){
            cout << ans[i] << endl;
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...