제출 #1110385

#제출 시각아이디문제언어결과실행 시간메모리
1110385flyingkiteKlasika (COCI20_klasika)C++17
110 / 110
2348 ms515408 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second  
#define all(x) (x).begin(), (x).end()
 
const ll N = 3e6 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 450;
ll n = 1, q, timer = 0;
vector<pll>adj[N];
struct query{ll typ,x,y;} t[N];
ll tin[N], tout[N], dep[N];
void dfs(ll u, ll par){
    tin[u] = ++timer;
    for(auto v : adj[u]){
        if(v.F == par) continue;
        dep[v.F] = dep[u] ^ v.S;
        dfs(v.F, u);
    }
    tout[u] = timer;
}
struct Trie{
    struct ccjv{
        ll c[2]; set<ll>s;
    } t[N];
    ll cur = 3e6;
    void init(){
        for(int i = 0; i <= cur;i++) t[i].c[0] = t[i].c[1] = -1, t[i].s.clear();
        cur = 0;
    }
    void add(ll x, ll val){
        ll pos = 0;
        for(int i = 30; i >= 0;i--){
            ll f = (x >> i) & 1;
            if(t[pos].c[f] == -1) t[pos].c[f] = ++cur;
            pos = t[pos].c[f];
            t[pos].s.insert(val);
        }
    }
    bool ok(ll pos, ll l, ll r){
        auto it = t[pos].s.lower_bound(l);
        if(it == t[pos].s.end() || (*it > r)) return 0;
        return 1;
    }
    ll query(ll x, ll l, ll r){
        ll pos = 0, res = 0;
        for(int i = 30; i >= 0;i--){
            ll f = (x >> i) & 1;
            if(t[pos].c[f ^ 1] != -1 && ok(t[pos].c[f ^ 1], l, r)){
                res |= (1 << i);
                pos = t[pos].c[f ^ 1];
            }
            else if(t[pos].c[f] != -1 && ok(t[pos].c[f], l, r)) pos = t[pos].c[f];
        }
        return res;
    }
} chinsu;
void to_thic_cau(){
    cin >> q;
    for(int i = 1; i <= q;i++){
        string s; ll x,y; cin >> s >> x >> y;
        if(s == "Add") adj[x].pb({++n, y}), t[i] = {1, n, x};      
        else t[i] = {2, x, y};
    }
    dfs(1, 0);
    chinsu.init();
    chinsu.add(0, 1);
    for(int i = 1; i <= q;i++){
        if(t[i].typ == 1) {
            chinsu.add(dep[t[i].x], tin[t[i].x]);
        }
        else{
            cout << chinsu.query(dep[t[i].x], tin[t[i].y], tout[t[i].y]) << '\n';
        }
    }
}    
 
signed main()   
{  
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll tc = 1;
    //cin >> tc;
    while(tc--) to_thic_cau();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...