답안 #446545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446545 2021-07-22T11:22:51 Z Arinoor Klasika (COCI20_klasika) C++17
110 / 110
1113 ms 105484 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int inf = 1e9 + 10;
const int maxlg = 31;

vector<pair<int, int>> query[maxn];
int ans_query[maxn];
vector<int> G[maxn];
int val[maxn], sz[maxn], adding_time[maxn];

struct Trie{
	struct node{
		node *child[2];
		int mini;
		int sz;

		node(){
			child[0] = nullptr;
			child[1] = nullptr;
			mini = inf;
			sz = 0;
		}

		node *getChild(bool bit){
			if(child[bit] == nullptr)
				child[bit] = new node();
			else if(child[bit]->sz == 0)
				child[bit]->mini = inf;
			return child[bit];
		}

		bool check(bool bit, int ind){
			if(child[bit] == nullptr or child[bit]->sz == 0)
				return false;
			return (child[bit]->mini) < ind;
		}

	};
	
	node *root;

	Trie(){
		root = new node();
	}

	void add(int v){
		node *cur = root;
		for(int i = maxlg - 1; i >= 0; i --){
			cur = cur->getChild(val[v] & (1 << i));
			cur->mini = min(cur->mini, adding_time[v]);
			cur->sz ++;
		}
	}

	void erase(int v){
		node *cur = root;
		for(int i = maxlg - 1; i >= 0; i --){
			cur = cur->getChild(val[v] & (1 << i));
			cur->sz --;
		}
	}

	int get(int a, int ind){
		node *cur = root;
		int ans = 0;
		for(int i = maxlg - 1; i >= 0; i --){
			bool bit = (a & (1 << i));
			if(cur->check(!bit, ind)){
				if(!bit)
					ans += (1 << i);
				cur = cur->child[!bit];
			}
			else{
				if(bit)
					ans += (1 << i);
				cur = cur->child[bit];
			}
		}
		return ans ^ a;
	}

} T;

void dfs_sz(int v){
	sz[v] = 1;
	for(int u : G[v]){
		dfs_sz(u);
		sz[v] += sz[u];
	}
}

void dfs_add(int v){
	T.add(v);
	for(int u : G[v])
		dfs_add(u);
}

void dfs_erase(int v){
	T.erase(v);
	for(int u : G[v]){
		dfs_erase(u);
	}
}

void dsu(int v, bool keep){
	int big_child = -1;
	int maxi = 0;
	for(int u : G[v]){
		if(sz[u] > maxi){
			maxi = sz[u];
			big_child = u;
		}
	}
	if(big_child == -1){
		for(auto q : query[v]){
			ans_query[q.second] = q.first ^ val[v];
		}
		if(keep)
			T.add(v);
		return;
	}
	for(int u : G[v]){
		if(u != big_child)
			dsu(u, false);
	}
	dsu(big_child, true);
	T.add(v);
	for(int u : G[v]){
		if(u != big_child){
			dfs_add(u);
		}
	}
	for(auto q : query[v]){
		ans_query[q.second] = T.get(q.first, q.second);
	}
	if(!keep){
		T.erase(v);
		for(int u : G[v]){
			dfs_erase(u);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int Q;
	cin >> Q;
	memset(ans_query, -1, sizeof ans_query);
	int vertex_cnt = 1;
	for(int i = 0; i < Q; i ++){
		string type;
		int x, y;
		cin >> type >> x >> y;
		if(type == "Add"){
			G[--x].push_back(vertex_cnt);
			val[vertex_cnt] = (y ^ val[x]);
			adding_time[vertex_cnt++] = i;
		}
		else{
			query[--y].push_back({val[--x], i});
		}
	}
	dfs_sz(0);
	dsu(0, 0);
	for(int i = 0; i < Q; i ++){
		if(ans_query[i] != -1){
			cout << ans_query[i] << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10444 KB Output is correct
2 Correct 6 ms 10572 KB Output is correct
3 Correct 6 ms 10600 KB Output is correct
4 Correct 7 ms 10540 KB Output is correct
5 Correct 6 ms 10444 KB Output is correct
6 Correct 6 ms 10444 KB Output is correct
7 Correct 7 ms 10572 KB Output is correct
8 Correct 7 ms 10572 KB Output is correct
9 Correct 7 ms 10548 KB Output is correct
10 Correct 7 ms 10572 KB Output is correct
11 Correct 8 ms 10572 KB Output is correct
12 Correct 7 ms 10572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10444 KB Output is correct
2 Correct 6 ms 10572 KB Output is correct
3 Correct 6 ms 10600 KB Output is correct
4 Correct 7 ms 10540 KB Output is correct
5 Correct 6 ms 10444 KB Output is correct
6 Correct 6 ms 10444 KB Output is correct
7 Correct 7 ms 10572 KB Output is correct
8 Correct 7 ms 10572 KB Output is correct
9 Correct 7 ms 10548 KB Output is correct
10 Correct 7 ms 10572 KB Output is correct
11 Correct 8 ms 10572 KB Output is correct
12 Correct 7 ms 10572 KB Output is correct
13 Correct 8 ms 10828 KB Output is correct
14 Correct 8 ms 11212 KB Output is correct
15 Correct 9 ms 11468 KB Output is correct
16 Correct 9 ms 11724 KB Output is correct
17 Correct 8 ms 10736 KB Output is correct
18 Correct 9 ms 11084 KB Output is correct
19 Correct 12 ms 11340 KB Output is correct
20 Correct 11 ms 11540 KB Output is correct
21 Correct 8 ms 10740 KB Output is correct
22 Correct 9 ms 11084 KB Output is correct
23 Correct 10 ms 11320 KB Output is correct
24 Correct 10 ms 11596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 228 ms 39232 KB Output is correct
2 Correct 295 ms 62012 KB Output is correct
3 Correct 346 ms 83472 KB Output is correct
4 Correct 406 ms 104720 KB Output is correct
5 Correct 330 ms 34396 KB Output is correct
6 Correct 582 ms 52216 KB Output is correct
7 Correct 816 ms 68668 KB Output is correct
8 Correct 1090 ms 84940 KB Output is correct
9 Correct 247 ms 35052 KB Output is correct
10 Correct 343 ms 54056 KB Output is correct
11 Correct 430 ms 71600 KB Output is correct
12 Correct 513 ms 88676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10444 KB Output is correct
2 Correct 6 ms 10572 KB Output is correct
3 Correct 6 ms 10600 KB Output is correct
4 Correct 7 ms 10540 KB Output is correct
5 Correct 6 ms 10444 KB Output is correct
6 Correct 6 ms 10444 KB Output is correct
7 Correct 7 ms 10572 KB Output is correct
8 Correct 7 ms 10572 KB Output is correct
9 Correct 7 ms 10548 KB Output is correct
10 Correct 7 ms 10572 KB Output is correct
11 Correct 8 ms 10572 KB Output is correct
12 Correct 7 ms 10572 KB Output is correct
13 Correct 8 ms 10828 KB Output is correct
14 Correct 8 ms 11212 KB Output is correct
15 Correct 9 ms 11468 KB Output is correct
16 Correct 9 ms 11724 KB Output is correct
17 Correct 8 ms 10736 KB Output is correct
18 Correct 9 ms 11084 KB Output is correct
19 Correct 12 ms 11340 KB Output is correct
20 Correct 11 ms 11540 KB Output is correct
21 Correct 8 ms 10740 KB Output is correct
22 Correct 9 ms 11084 KB Output is correct
23 Correct 10 ms 11320 KB Output is correct
24 Correct 10 ms 11596 KB Output is correct
25 Correct 228 ms 39232 KB Output is correct
26 Correct 295 ms 62012 KB Output is correct
27 Correct 346 ms 83472 KB Output is correct
28 Correct 406 ms 104720 KB Output is correct
29 Correct 330 ms 34396 KB Output is correct
30 Correct 582 ms 52216 KB Output is correct
31 Correct 816 ms 68668 KB Output is correct
32 Correct 1090 ms 84940 KB Output is correct
33 Correct 247 ms 35052 KB Output is correct
34 Correct 343 ms 54056 KB Output is correct
35 Correct 430 ms 71600 KB Output is correct
36 Correct 513 ms 88676 KB Output is correct
37 Correct 254 ms 40336 KB Output is correct
38 Correct 360 ms 62988 KB Output is correct
39 Correct 370 ms 85032 KB Output is correct
40 Correct 455 ms 105484 KB Output is correct
41 Correct 311 ms 35316 KB Output is correct
42 Correct 651 ms 53076 KB Output is correct
43 Correct 852 ms 69692 KB Output is correct
44 Correct 1113 ms 85612 KB Output is correct
45 Correct 234 ms 35932 KB Output is correct
46 Correct 348 ms 54936 KB Output is correct
47 Correct 477 ms 72368 KB Output is correct
48 Correct 518 ms 89196 KB Output is correct