답안 #256834

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256834 2020-08-03T09:20:35 Z MrRobot_28 Klasika (COCI20_klasika) C++17
110 / 110
3066 ms 439048 KB
#include<bits/stdc++.h>
 
using namespace std;
int n = 1;
vector <vector <pair <int, int> > > g;
vector <int> h, tin, tout;
int timer = 0;
void dfs(int v, int p = -1)
{
	tin[v] = timer++;
	for(int i = 0; i < g[v].size(); i++)
	{
		int to = g[v][i].first;
		int w = g[v][i].second;
		if(to != p)
		{
			h[to] = h[v] ^ w;
			dfs(to, v);
		}
	}
	tout[v] = timer - 1;
}
struct node{
	node* left;
	node* right;
	set <pair <int, int> > s;
	node(){
	left = NULL;
	right = NULL;};
};
void update(node* &w, int i, int val, int v)
{
	if(i < 0)
	{
		return;
	}
	if(val & (1 << i))
	{
		if(!w -> right)
		{
			w -> right = new node();
		}
		node* &t = w -> right;
		t -> s.insert({tin[v], v});
		update(t, i - 1, val, v);
	}
	else
	{
		if(!w -> left)
		{
			w -> left = new node();
		}
		node* &t = w -> left;
		t -> s.insert({tin[v], v});
		update(t, i - 1, val, v);
	}
}
int ans(node* w, int i, int val, int l, int r)
{
	if(i < 0)
	{
		return 0;
	}
	int val1 = (1 << 31) - 1 - val;
	if(val1 & (1 << i))
	{
		if(!w -> right)
		{
			return ans(w -> left, i - 1, val, l, r);
		}
		else
		{
			set <pair <int, int> > :: iterator it;
			it = w -> right -> s.lower_bound({l, 0});
			if(it != w -> right -> s.end() && it -> first <= r)
			{
				return ans(w -> right, i - 1, val, l, r) + (1 << i);
			}
			else
			{
				return ans(w -> left, i - 1, val, l, r);
			}
		}
	}
	else
	{
		if(!w -> left)
		{
			return ans(w -> right, i - 1, val, l, r) + (1 << i);
		}
		else
		{
			set <pair <int, int> > :: iterator it;
			it = w -> left -> s.lower_bound({l, 0});
			if(it != w -> left -> s.end() && it -> first <= r)
			{
				return ans(w -> left, i - 1, val, l, r);
			}
			else
			{
				return ans(w -> right, i - 1, val, l, r) + (1 << i);
			}
		}
	}
}
signed main(){
//	ios_base::sync_with_stdio(false);
//	cin.tie(NULL);
//	cout.tie(NULL);
	int q;
	cin >> q;
	vector <string> type(q);
	vector <pair <int, int> > mass(q);
	for(int i = 0; i < q; i++){
		cin >> type[i];
		cin >> mass[i].first >> mass[i].second;
		if(type[i] == "Add"){
			n++;
		}
	}
	g.resize(n);
	h.resize(n);
	tin.resize(n);
	tout.resize(n);
	int cnt = 1;
	for(int i = 0; i < q; i++)
	{
		if(type[i] == "Add")
		{
			mass[i].first--;
			g[mass[i].first].push_back({cnt, mass[i].second});
			cnt++;
		}
	}
	dfs(0);
	node* tree = new node();
	cnt = 1;
	update(tree, 30, 0, 0);
	for(int i = 0; i < q; i++)
	{
		if(type[i] == "Add")
		{
			update(tree, 30, h[cnt], cnt);
			cnt++;
		}
		else
		{
			mass[i].first--;
			mass[i].second--;
			int val = h[mass[i].first];
			int val1 = ans(tree, 30, val, tin[mass[i].second], tout[mass[i].second]);
			cout << (val ^ val1) << "\n";
		}
	}
	return 0;
}

Compilation message

klasika.cpp: In function 'void dfs(int, int)':
klasika.cpp:11:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < g[v].size(); i++)
                 ~~^~~~~~~~~~~~~
klasika.cpp: In function 'int ans(node*, int, int, int, int)':
klasika.cpp:64:23: warning: integer overflow in expression [-Woverflow]
  int val1 = (1 << 31) - 1 - val;
             ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 1 ms 896 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 768 KB Output is correct
12 Correct 1 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 1 ms 896 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 768 KB Output is correct
12 Correct 1 ms 896 KB Output is correct
13 Correct 6 ms 1792 KB Output is correct
14 Correct 8 ms 3072 KB Output is correct
15 Correct 9 ms 4352 KB Output is correct
16 Correct 10 ms 5504 KB Output is correct
17 Correct 6 ms 1792 KB Output is correct
18 Correct 9 ms 2944 KB Output is correct
19 Correct 10 ms 4224 KB Output is correct
20 Correct 12 ms 5376 KB Output is correct
21 Correct 6 ms 1792 KB Output is correct
22 Correct 9 ms 3072 KB Output is correct
23 Correct 11 ms 4224 KB Output is correct
24 Correct 12 ms 5376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1024 ms 123356 KB Output is correct
2 Correct 1576 ms 232536 KB Output is correct
3 Correct 2092 ms 335224 KB Output is correct
4 Correct 2591 ms 438764 KB Output is correct
5 Correct 1016 ms 122616 KB Output is correct
6 Correct 1546 ms 226952 KB Output is correct
7 Correct 2083 ms 326896 KB Output is correct
8 Correct 2674 ms 427132 KB Output is correct
9 Correct 979 ms 122332 KB Output is correct
10 Correct 1776 ms 228088 KB Output is correct
11 Correct 2116 ms 329992 KB Output is correct
12 Correct 2633 ms 429560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 1 ms 896 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 768 KB Output is correct
12 Correct 1 ms 896 KB Output is correct
13 Correct 6 ms 1792 KB Output is correct
14 Correct 8 ms 3072 KB Output is correct
15 Correct 9 ms 4352 KB Output is correct
16 Correct 10 ms 5504 KB Output is correct
17 Correct 6 ms 1792 KB Output is correct
18 Correct 9 ms 2944 KB Output is correct
19 Correct 10 ms 4224 KB Output is correct
20 Correct 12 ms 5376 KB Output is correct
21 Correct 6 ms 1792 KB Output is correct
22 Correct 9 ms 3072 KB Output is correct
23 Correct 11 ms 4224 KB Output is correct
24 Correct 12 ms 5376 KB Output is correct
25 Correct 1024 ms 123356 KB Output is correct
26 Correct 1576 ms 232536 KB Output is correct
27 Correct 2092 ms 335224 KB Output is correct
28 Correct 2591 ms 438764 KB Output is correct
29 Correct 1016 ms 122616 KB Output is correct
30 Correct 1546 ms 226952 KB Output is correct
31 Correct 2083 ms 326896 KB Output is correct
32 Correct 2674 ms 427132 KB Output is correct
33 Correct 979 ms 122332 KB Output is correct
34 Correct 1776 ms 228088 KB Output is correct
35 Correct 2116 ms 329992 KB Output is correct
36 Correct 2633 ms 429560 KB Output is correct
37 Correct 1611 ms 126316 KB Output is correct
38 Correct 2253 ms 232360 KB Output is correct
39 Correct 2779 ms 337932 KB Output is correct
40 Correct 2912 ms 439048 KB Output is correct
41 Correct 1795 ms 123316 KB Output is correct
42 Correct 2183 ms 226808 KB Output is correct
43 Correct 2610 ms 327288 KB Output is correct
44 Correct 3066 ms 426340 KB Output is correct
45 Correct 1781 ms 122616 KB Output is correct
46 Correct 2364 ms 227960 KB Output is correct
47 Correct 2778 ms 328464 KB Output is correct
48 Correct 3037 ms 429744 KB Output is correct