Submission #482241

# Submission time Handle Problem Language Result Execution time Memory
482241 2021-10-23T20:42:42 Z danielliu04 Klasika (COCI20_klasika) C++17
110 / 110
2495 ms 431828 KB
// Link: https://judge.yosupo.jp/problem/set_xor_min

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define lc (node<<1)+1
#define rc (node<<1)+2
#define endl '\n'

const int MAX = 2e5+2;
int q;

struct Edge{
	int next, weight;
};

vector<Edge> adj[MAX];

struct Query{
	char type;
	int a, b;
} queries[MAX];


int t = 0;
int enter[MAX];
int out[MAX];
int value[MAX];

void dfs(int node, int curr){
	enter[node] = t++;
	value[node] = curr;
	for(Edge e : adj[node]){
		dfs(e.next, curr ^ e.weight);
	}
	out[node] = t++;
}


struct Vertex{
	Vertex* children[2];
	set<int> entries;
	Vertex(){
		children[0] = nullptr;
		children[1] = nullptr;
	}
};

Vertex* trie;

void insert(Vertex* root, int x, int time){
	Vertex* v = root;
	for(int i = 30; i >= 0; i --){
		bool c = (1 << i) & x;
		if(!v->children[c]) v->children[c] = new Vertex;
		v = v->children[c];
		v->entries.insert(time);
	}
}

int query(Vertex* root, int x, int min_entry, int max_entry){
	Vertex* v = root;
	int ans = 0;
	for(int i = 30; i >= 0; i --){
		bool c = (1 << i) & x;
		c = c^1;
		if(v->children[c]){
			auto a = v->children[c]->entries.lower_bound(min_entry);
			if(a != v->children[c]->entries.end() && *a <= max_entry){
				ans |= (1 << i);
				v = v->children[c];
				continue;
			}
		}

		v = v->children[c^1];
	}
	return ans;
}

int main() {
	
	// cin.tie(0);
	// ios::sync_with_stdio(0);

	cin >> q;

	int counter = 2;
	for(int i = 0; i < q; i ++){
		string s; int a, b;
		cin >> s >> a >> b;
		char type = s[0];
		if(type == 'A'){
			adj[a].push_back({counter ++, b});
		}
		queries[i] = {type, a, b};
	}

	dfs(1, 0);

	// use trie data structure to maintain current values of xor(1, node)
	trie = new Vertex;
	insert(trie, 0, 0);
	counter = 2;
	for(int i = 0; i < q; i ++){
		Query Q = queries[i];
		if(Q.type == 'A'){
			insert(trie, value[counter], enter[counter]);
			counter ++;
		}
		else{
			int ans = query(trie, value[Q.a], enter[Q.b], out[Q.b]);
			cout << ans << endl;
		}
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 3 ms 5216 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 3 ms 5128 KB Output is correct
6 Correct 3 ms 5196 KB Output is correct
7 Correct 3 ms 5324 KB Output is correct
8 Correct 3 ms 5452 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 3 ms 5452 KB Output is correct
12 Correct 3 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 3 ms 5216 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 3 ms 5128 KB Output is correct
6 Correct 3 ms 5196 KB Output is correct
7 Correct 3 ms 5324 KB Output is correct
8 Correct 3 ms 5452 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 3 ms 5452 KB Output is correct
12 Correct 3 ms 5452 KB Output is correct
13 Correct 7 ms 6416 KB Output is correct
14 Correct 8 ms 7628 KB Output is correct
15 Correct 9 ms 8908 KB Output is correct
16 Correct 11 ms 10132 KB Output is correct
17 Correct 7 ms 6348 KB Output is correct
18 Correct 10 ms 7604 KB Output is correct
19 Correct 9 ms 8780 KB Output is correct
20 Correct 10 ms 9932 KB Output is correct
21 Correct 6 ms 6312 KB Output is correct
22 Correct 9 ms 7612 KB Output is correct
23 Correct 9 ms 8876 KB Output is correct
24 Correct 10 ms 9932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 678 ms 122756 KB Output is correct
2 Correct 1156 ms 228292 KB Output is correct
3 Correct 1656 ms 329408 KB Output is correct
4 Correct 2082 ms 431260 KB Output is correct
5 Correct 709 ms 120692 KB Output is correct
6 Correct 1206 ms 224024 KB Output is correct
7 Correct 1664 ms 323200 KB Output is correct
8 Correct 2144 ms 422352 KB Output is correct
9 Correct 762 ms 120228 KB Output is correct
10 Correct 1201 ms 224996 KB Output is correct
11 Correct 1603 ms 325728 KB Output is correct
12 Correct 2033 ms 424520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 3 ms 5216 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 3 ms 5128 KB Output is correct
6 Correct 3 ms 5196 KB Output is correct
7 Correct 3 ms 5324 KB Output is correct
8 Correct 3 ms 5452 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 3 ms 5452 KB Output is correct
12 Correct 3 ms 5452 KB Output is correct
13 Correct 7 ms 6416 KB Output is correct
14 Correct 8 ms 7628 KB Output is correct
15 Correct 9 ms 8908 KB Output is correct
16 Correct 11 ms 10132 KB Output is correct
17 Correct 7 ms 6348 KB Output is correct
18 Correct 10 ms 7604 KB Output is correct
19 Correct 9 ms 8780 KB Output is correct
20 Correct 10 ms 9932 KB Output is correct
21 Correct 6 ms 6312 KB Output is correct
22 Correct 9 ms 7612 KB Output is correct
23 Correct 9 ms 8876 KB Output is correct
24 Correct 10 ms 9932 KB Output is correct
25 Correct 678 ms 122756 KB Output is correct
26 Correct 1156 ms 228292 KB Output is correct
27 Correct 1656 ms 329408 KB Output is correct
28 Correct 2082 ms 431260 KB Output is correct
29 Correct 709 ms 120692 KB Output is correct
30 Correct 1206 ms 224024 KB Output is correct
31 Correct 1664 ms 323200 KB Output is correct
32 Correct 2144 ms 422352 KB Output is correct
33 Correct 762 ms 120228 KB Output is correct
34 Correct 1201 ms 224996 KB Output is correct
35 Correct 1603 ms 325728 KB Output is correct
36 Correct 2033 ms 424520 KB Output is correct
37 Correct 1224 ms 123768 KB Output is correct
38 Correct 1706 ms 228280 KB Output is correct
39 Correct 2052 ms 332156 KB Output is correct
40 Correct 2322 ms 431828 KB Output is correct
41 Correct 1274 ms 121084 KB Output is correct
42 Correct 1757 ms 223940 KB Output is correct
43 Correct 2099 ms 323380 KB Output is correct
44 Correct 2373 ms 421700 KB Output is correct
45 Correct 1424 ms 120688 KB Output is correct
46 Correct 1909 ms 224992 KB Output is correct
47 Correct 2193 ms 324392 KB Output is correct
48 Correct 2495 ms 424436 KB Output is correct