Submission #671266

# Submission time Handle Problem Language Result Execution time Memory
671266 2022-12-12T15:15:30 Z Nimbostratus Klasika (COCI20_klasika) C++17
110 / 110
2832 ms 490956 KB
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using lint = long long;
const int maxn = 2e5 + 5;
const int inf = 1e9 + 5;
const int mod = 1e9 + 7;

struct node {
	int ch[2] = {0, 0};
	set<pair<int, int>> s;
};

int n, q;
int tin[maxn], tout[maxn];
vector<int> adj[maxn];
int val[maxn];
array<int, 3> qry[maxn];
vector<node> trie;

void dfs(int u, int p) {
	static int timer = 0;
	tin[u] = ++timer;
	for(int v : adj[u]) {
		if(v == p)
			continue;
		dfs(v, u);
	}
	tout[u] = ++timer;
}

//k at most 31
void add(int idx, int k, int num, int u) {
	trie[idx].s.insert({tin[u], tout[u]});
	if(k == 0)
		return;
	if(!trie[idx].ch[0]) {
		trie.push_back(node());
		trie[idx].ch[0] = trie.size() - 1;
	}
	if(!trie[idx].ch[1]) {
		trie.push_back(node());
		trie[idx].ch[1] = trie.size() - 1;
	}
	int newidx = trie[idx].ch[!!(num & (1 << (k - 1)))];
	add(newidx, k - 1, num, u);
}

//k at most 31
int query(int idx, int k, int num, int u) {
	if(k == 0)
		return 0;
	if(!trie[idx].ch[0]) {
		trie.push_back(node());
		trie[idx].ch[0] = trie.size() - 1;
	}
	if(!trie[idx].ch[1]) {
		trie.push_back(node());
		trie[idx].ch[1] = trie.size() - 1;
	}
	int curbit = !!(num & (1 << (k - 1)));
	auto& s = trie[trie[idx].ch[1 ^ curbit]].s;
	auto it = s.lower_bound({tin[u], tout[u]});
	if(it == s.end() || it->first > tout[u])
		return query(trie[idx].ch[curbit], k - 1, num, u);
	return (1 << (k - 1)) | query(trie[idx].ch[1 ^ curbit], k - 1, num, u); 
}


signed main() {
	#ifdef Local
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> q;
	n = 1;
	for(int i = 1; i <= q; i++) {
		int x, y;
		string op;
		cin >> op >> x >> y;
		if(op == "Add") {
			qry[i][0] = 1;
			n++;
			qry[i][1] = x;
			qry[i][2] = n;
			adj[x].push_back(n);
			val[n] = val[x] ^ y;
		}
		else {
			qry[i][0] = 2;
			qry[i][1] = x;
			qry[i][2] = y;
		}
	}
	dfs(1, 0);
	trie.push_back(node());
	add(0, 31, 0, 1);
	for(int i = 1; i <= q; i++) {
		if(qry[i][0] == 1)
			add(0, 31, val[qry[i][2]], qry[i][2]);
		else
			cout << query(0, 31, val[qry[i][1]], qry[i][2]) << endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 3 ms 5428 KB Output is correct
3 Correct 3 ms 5684 KB Output is correct
4 Correct 4 ms 5704 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5424 KB Output is correct
7 Correct 4 ms 5680 KB Output is correct
8 Correct 3 ms 5680 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5640 KB Output is correct
11 Correct 3 ms 5680 KB Output is correct
12 Correct 4 ms 5684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 3 ms 5428 KB Output is correct
3 Correct 3 ms 5684 KB Output is correct
4 Correct 4 ms 5704 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5424 KB Output is correct
7 Correct 4 ms 5680 KB Output is correct
8 Correct 3 ms 5680 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5640 KB Output is correct
11 Correct 3 ms 5680 KB Output is correct
12 Correct 4 ms 5684 KB Output is correct
13 Correct 7 ms 7420 KB Output is correct
14 Correct 9 ms 9988 KB Output is correct
15 Correct 10 ms 10000 KB Output is correct
16 Correct 12 ms 11040 KB Output is correct
17 Correct 7 ms 7428 KB Output is correct
18 Correct 9 ms 9864 KB Output is correct
19 Correct 11 ms 9864 KB Output is correct
20 Correct 12 ms 10884 KB Output is correct
21 Correct 8 ms 7408 KB Output is correct
22 Correct 10 ms 9956 KB Output is correct
23 Correct 11 ms 9864 KB Output is correct
24 Correct 12 ms 10884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 788 ms 180092 KB Output is correct
2 Correct 1417 ms 362268 KB Output is correct
3 Correct 1908 ms 375444 KB Output is correct
4 Correct 2488 ms 490608 KB Output is correct
5 Correct 920 ms 180308 KB Output is correct
6 Correct 1522 ms 357204 KB Output is correct
7 Correct 2097 ms 368824 KB Output is correct
8 Correct 2618 ms 481392 KB Output is correct
9 Correct 867 ms 180640 KB Output is correct
10 Correct 1505 ms 358088 KB Output is correct
11 Correct 2012 ms 371708 KB Output is correct
12 Correct 2483 ms 483828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 3 ms 5428 KB Output is correct
3 Correct 3 ms 5684 KB Output is correct
4 Correct 4 ms 5704 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5424 KB Output is correct
7 Correct 4 ms 5680 KB Output is correct
8 Correct 3 ms 5680 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5640 KB Output is correct
11 Correct 3 ms 5680 KB Output is correct
12 Correct 4 ms 5684 KB Output is correct
13 Correct 7 ms 7420 KB Output is correct
14 Correct 9 ms 9988 KB Output is correct
15 Correct 10 ms 10000 KB Output is correct
16 Correct 12 ms 11040 KB Output is correct
17 Correct 7 ms 7428 KB Output is correct
18 Correct 9 ms 9864 KB Output is correct
19 Correct 11 ms 9864 KB Output is correct
20 Correct 12 ms 10884 KB Output is correct
21 Correct 8 ms 7408 KB Output is correct
22 Correct 10 ms 9956 KB Output is correct
23 Correct 11 ms 9864 KB Output is correct
24 Correct 12 ms 10884 KB Output is correct
25 Correct 788 ms 180092 KB Output is correct
26 Correct 1417 ms 362268 KB Output is correct
27 Correct 1908 ms 375444 KB Output is correct
28 Correct 2488 ms 490608 KB Output is correct
29 Correct 920 ms 180308 KB Output is correct
30 Correct 1522 ms 357204 KB Output is correct
31 Correct 2097 ms 368824 KB Output is correct
32 Correct 2618 ms 481392 KB Output is correct
33 Correct 867 ms 180640 KB Output is correct
34 Correct 1505 ms 358088 KB Output is correct
35 Correct 2012 ms 371708 KB Output is correct
36 Correct 2483 ms 483828 KB Output is correct
37 Correct 1369 ms 183344 KB Output is correct
38 Correct 1998 ms 362568 KB Output is correct
39 Correct 2308 ms 378232 KB Output is correct
40 Correct 2585 ms 490956 KB Output is correct
41 Correct 1426 ms 180812 KB Output is correct
42 Correct 2031 ms 357600 KB Output is correct
43 Correct 2534 ms 369076 KB Output is correct
44 Correct 2832 ms 480536 KB Output is correct
45 Correct 1487 ms 181112 KB Output is correct
46 Correct 2115 ms 358420 KB Output is correct
47 Correct 2440 ms 370200 KB Output is correct
48 Correct 2692 ms 483772 KB Output is correct