Submission #844405

# Submission time Handle Problem Language Result Execution time Memory
844405 2023-09-05T12:59:44 Z vjudge1 Klasika (COCI20_klasika) C++17
110 / 110
335 ms 197976 KB
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n";
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005
#define M 30

const int modulo = 1e9 + 7;


int par[N], dist[N], sz[N], ans[N], timee[N];
vector<pii> qry[N];
vector<int> child[N];


struct Node{
	Node* nxt[2];
	int mini;
	Node(){
		nxt[0] = nxt[1] = NULL;
		mini = modulo;
	}
};

Node *trie[N];

void add(Node *root, int val, int t){
	for (int i = M; i >= 0; i--){
		int ind = 0;
		if (val & (1<<i)) ind = 1;
		if (root->nxt[ind] == NULL){
			root->nxt[ind] = new Node;
		}

		root = root->nxt[ind];
		root->mini = min(root->mini, t);
	}
}	

int query(Node *root, int val, int t){
	int ans = 0;
	for (int i = M; i >= 0; i--){
		int ind = 1;
		if (val & (1<<i)) ind = 0;
		if (root->nxt[ind] != NULL && root->nxt[ind]->mini <= t) ans |= (1<<i), root = root->nxt[ind];
		else root = root->nxt[1 ^ ind];
	}
	return ans;
}

void merge_subtree(Node *a, Node *b){
	a->mini = min(a->mini, b->mini);
	for (int i = 0; i < 2; i++){
		if (b->nxt[i] == NULL) continue;
		if (a->nxt[i] == NULL)  a->nxt[i] = b->nxt[i];
		else merge_subtree(a->nxt[i], b->nxt[i]);
	}
}

void dfs(int node, int root){
	trie[node] = new Node;
	add(trie[node], dist[node], timee[node]);
	sz[node] = 1;
	for (auto i : child[node]){
		dfs(i, node);
		if (sz[i] > sz[node]){
			swap(trie[node], trie[i]);
		}
		merge_subtree(trie[node], trie[i]);
		sz[node] += sz[i];
	}

	for (auto i : qry[node]){
		ans[i.nd] = query(trie[node], i.st, i.nd);
	}
}


int32_t main()
{
	fastio();
	memset(ans, -1, sizeof(ans));

	int q;
	cin>>q;
	int cntr = 2;
	for (int i = 1; i <= q; i++){
		string type;
		cin>>type;
		if (type == "Add"){
			int x, y;
			cin>>x>>y;
			par[cntr] = x;
			child[x].pb(cntr);
			dist[cntr] = dist[x] ^ y;
			timee[cntr] = i;
			cntr++;
		}
		else{
			int a, b;
			cin>>a>>b;
			qry[b].pb({dist[a], i});
		}
	}

	dfs(1, 0);

	for (int i = 1; i <= q; i++){
		if (ans[i] != -1) cout<<ans[i]<<endl;
	}

	cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " seconds\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 3 ms 14936 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 3 ms 15192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 3 ms 14936 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 3 ms 15192 KB Output is correct
13 Correct 4 ms 15192 KB Output is correct
14 Correct 4 ms 15704 KB Output is correct
15 Correct 4 ms 16220 KB Output is correct
16 Correct 5 ms 16472 KB Output is correct
17 Correct 4 ms 15192 KB Output is correct
18 Correct 4 ms 15704 KB Output is correct
19 Correct 5 ms 15900 KB Output is correct
20 Correct 5 ms 16472 KB Output is correct
21 Correct 4 ms 15192 KB Output is correct
22 Correct 4 ms 15704 KB Output is correct
23 Correct 4 ms 15960 KB Output is correct
24 Correct 5 ms 16472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 62008 KB Output is correct
2 Correct 230 ms 106176 KB Output is correct
3 Correct 264 ms 149252 KB Output is correct
4 Correct 313 ms 193484 KB Output is correct
5 Correct 150 ms 58596 KB Output is correct
6 Correct 221 ms 102084 KB Output is correct
7 Correct 266 ms 141788 KB Output is correct
8 Correct 335 ms 182636 KB Output is correct
9 Correct 166 ms 61716 KB Output is correct
10 Correct 209 ms 103224 KB Output is correct
11 Correct 259 ms 144476 KB Output is correct
12 Correct 296 ms 185288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 3 ms 14936 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14680 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 3 ms 14936 KB Output is correct
12 Correct 3 ms 15192 KB Output is correct
13 Correct 4 ms 15192 KB Output is correct
14 Correct 4 ms 15704 KB Output is correct
15 Correct 4 ms 16220 KB Output is correct
16 Correct 5 ms 16472 KB Output is correct
17 Correct 4 ms 15192 KB Output is correct
18 Correct 4 ms 15704 KB Output is correct
19 Correct 5 ms 15900 KB Output is correct
20 Correct 5 ms 16472 KB Output is correct
21 Correct 4 ms 15192 KB Output is correct
22 Correct 4 ms 15704 KB Output is correct
23 Correct 4 ms 15960 KB Output is correct
24 Correct 5 ms 16472 KB Output is correct
25 Correct 153 ms 62008 KB Output is correct
26 Correct 230 ms 106176 KB Output is correct
27 Correct 264 ms 149252 KB Output is correct
28 Correct 313 ms 193484 KB Output is correct
29 Correct 150 ms 58596 KB Output is correct
30 Correct 221 ms 102084 KB Output is correct
31 Correct 266 ms 141788 KB Output is correct
32 Correct 335 ms 182636 KB Output is correct
33 Correct 166 ms 61716 KB Output is correct
34 Correct 209 ms 103224 KB Output is correct
35 Correct 259 ms 144476 KB Output is correct
36 Correct 296 ms 185288 KB Output is correct
37 Correct 182 ms 66700 KB Output is correct
38 Correct 225 ms 110404 KB Output is correct
39 Correct 268 ms 154960 KB Output is correct
40 Correct 316 ms 197976 KB Output is correct
41 Correct 144 ms 62800 KB Output is correct
42 Correct 214 ms 103292 KB Output is correct
43 Correct 284 ms 142936 KB Output is correct
44 Correct 304 ms 182776 KB Output is correct
45 Correct 139 ms 63136 KB Output is correct
46 Correct 193 ms 104564 KB Output is correct
47 Correct 239 ms 144976 KB Output is correct
48 Correct 280 ms 186056 KB Output is correct