제출 #447634

#제출 시각아이디문제언어결과실행 시간메모리
447634EsquireKlasika (COCI20_klasika)C++17
110 / 110
2859 ms524288 KiB
#include<bits/stdc++.h>
using namespace std ; 
typedef long long ll ; 
typedef pair <int ,int> pii ; 
const int MAXN = 4e5 + 5; 
const int MOD = 1e9 + 7 ; 

#define F first 
#define S second 
#define debug(x) cerr << #x << " :" << x << "\n" 

int q ,n ,st[MAXN] ,ed[MAXN] ,child[MAXN * 30][2] ,newint ,a[MAXN] ,b[MAXN] ,dist[MAXN] ,tme ,idx[MAXN] ,len ,root ,x;  
vector <pii> G[MAXN] ; 
set <int> *s[MAXN * 30] ; 
string sq[MAXN] ; 

void dfs(int &u){
	st[u] = tme++ ; 
	for (auto [v ,w] : G[u]){
		dist[v] = dist[u] ^ w ; 
		dfs(v) ; 
	}
	ed[u] = tme++ ; 
}

void add(int &p ,int &id){
	root = 0 ; 
	for (int i = 30; i >= 0; i--){
		x = (p >> i) & 1 ;
		if (child[root][x] == -1){
			child[root][x] = ++newint ; 
			s[newint] = new set <int> () ; 
		}
		root = child[root][x] ; 
		(*s[root]).insert(st[id]) ;
	}
}

bool check(int &id ,int &v){
	if ((*s[id]).lower_bound(st[v]) == (*s[id]).end()){
		return false ; 
	}
	return (*(*s[id]).lower_bound(st[v])) <= ed[v] ; 
}

void ans(int &u ,int &v){ 
	len = dist[u] ; 
	root = 0 ; 
	for (int i = 30; i >= 0; i--){
		x = (len >> i) & 1 ; 
		if (child[root][1 - x] != -1 && check(child[root][1 - x] ,v)){
			root = child[root][1 - x] ; 
			if (!x){
				len += (1 << i) ; 
			}
		}
		else {
			if (x == 1){
				len -= (1 << i) ; 
			}
			root = child[root][x] ; 
		}
	}
	cout << len << "\n" ; 
}

int main (){
	ios::sync_with_stdio(false); cin.tie(0) ;cout.tie(0) ; 
	memset(child ,-1 ,sizeof child) ; 
	cin >> q ; 
	n = 1 ;
	for (int i = 1; i <= q; i++){
		cin >> sq[i] >> a[i] >> b[i] ; 
		if (sq[i][0] == 'A'){
			n++ ; 
			G[a[i]].push_back({n ,b[i]}) ; 
			idx[i] = n ; 
		}
	}
	int tmp = 1;
	dfs(tmp) ;
	add(dist[1] ,tmp) ; 
	for (int i = 1; i <= q; i++){
		if (sq[i][0] == 'Q'){
			ans(a[i] ,b[i]) ; 
		}
		else {
			add(dist[idx[i]] ,idx[i]) ;
		}
	}
	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...