답안 #198490

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198490 2020-01-26T11:30:04 Z ZwariowanyMarcin Klasika (COCI20_klasika) C++14
33 / 110
5000 ms 483588 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int) x.size()
#define pb push_back
#define LL long long
#define ld long double
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, j, n) for(int i = j; i <= n; ++i)
#define boost cin.tie(0), ios_base::sync_with_stdio(0);


using namespace std;		

const int nax = 200100;
const int pod = (1 << 18);

int q;
int t[nax];
int a[nax];
int b[nax];
int e[nax];

char s[10];

vector <int> v[nax];
int path[nax];

int tim;
int in[nax];
int out[nax];

void dfs(int u) {
	in[u] = tim++;
	for (auto it : v[u])
		dfs(it);
	out[u] = tim;
}

struct trie {
	int fre = 1;
	vector <int> go[2];
	vector <int> z;
	
	void init(int N) {
		go[0].resize(1, 0);
		go[1].resize(1, 0);
	}
	
	void add(int x) {
		int u = 0;
		for (int i = 29; 0 <= i; --i) {
			int c = ((x >> i) & 1);
			if (!go[c][u]) {
				go[c][u] = fre++;
				go[0].pb(0);
				go[1].pb(0);
			}
			u = go[c][u];
		}
	}
	
	int query(int x) {
		if (!z.empty()) {
			int best = 0;
			for (auto it : z)
				best = max(best, (it ^ x));
			return best;
		}
		int res = 0;
		int u = 0;
		for (int i = 29; 0 <= i; --i) {
			int c = !((x >> i) & 1);
			if (go[c][u]) {
				res += 1 << i;
				u = go[c][u];
			}
			else if(go[!c][u]) 
				u = go[!c][u];
			else return 0;
		}
		return res;
	}
};

trie d[2 * pod];
				
void add(int u, int x) {
	u += pod;
	int dl = 1;
	while (u) {
		if (dl <= 32) d[u].z.pb(x);
		else d[u].add(x);
		u /= 2;
		dl *= 2;
	}
}	

void make(int u = 1, int z = pod) {
	d[u].init(z);
	if (pod <= u) return;
	make(2 * u, z / 2);
	make(2 * u + 1, z / 2);
}		

int query(int x, int y, int z, int u = 1, int l = 0, int r = pod - 1) {
	if (x <= l && r <= y) 
		return d[u].query(z);
	int res = 0;
	int m = (l + r) / 2;
	if (x <= m) res = query(x, y, z, 2 * u, l, m);
	if (m < y) res = max(res, query(x, y, z, 2 * u + 1, m + 1, r));
	return res;
}

int main() {
	scanf ("%d", &q);
	int node = 2;
	for (int i = 1; i <= q; ++i) {
		scanf ("%s%d%d", s, a + i, b + i);
		t[i] = (s[0] == 'A');
		if (t[i]) {
			e[i] = node;
			path[node] = (path[a[i]] ^ b[i]);
			v[a[i]].pb(node++);
		}
	}
	dfs(1);
	
	make();
	
	add(in[1], 0);
	for (int i = 1; i <= q; ++i) {
		if (t[i] == 1) add(in[e[i]], path[e[i]]);
		else printf ("%d\n", query(in[b[i]], out[b[i]] - 1, path[a[i]]));
	} 
		
	return 0;
}

Compilation message

klasika.cpp: In function 'int main()':
klasika.cpp:118:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d", &q);
  ~~~~~~^~~~~~~~~~
klasika.cpp:121:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%s%d%d", s, a + i, b + i);
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 79352 KB Output is correct
2 Correct 147 ms 79096 KB Output is correct
3 Correct 139 ms 79544 KB Output is correct
4 Correct 164 ms 79352 KB Output is correct
5 Correct 144 ms 79224 KB Output is correct
6 Correct 153 ms 79224 KB Output is correct
7 Correct 143 ms 79352 KB Output is correct
8 Correct 143 ms 79352 KB Output is correct
9 Correct 144 ms 79120 KB Output is correct
10 Correct 145 ms 79480 KB Output is correct
11 Correct 148 ms 79328 KB Output is correct
12 Correct 152 ms 79352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 79352 KB Output is correct
2 Correct 147 ms 79096 KB Output is correct
3 Correct 139 ms 79544 KB Output is correct
4 Correct 164 ms 79352 KB Output is correct
5 Correct 144 ms 79224 KB Output is correct
6 Correct 153 ms 79224 KB Output is correct
7 Correct 143 ms 79352 KB Output is correct
8 Correct 143 ms 79352 KB Output is correct
9 Correct 144 ms 79120 KB Output is correct
10 Correct 145 ms 79480 KB Output is correct
11 Correct 148 ms 79328 KB Output is correct
12 Correct 152 ms 79352 KB Output is correct
13 Correct 159 ms 80508 KB Output is correct
14 Correct 155 ms 82040 KB Output is correct
15 Correct 166 ms 82680 KB Output is correct
16 Correct 154 ms 83804 KB Output is correct
17 Correct 142 ms 80504 KB Output is correct
18 Correct 156 ms 81776 KB Output is correct
19 Correct 161 ms 82552 KB Output is correct
20 Correct 166 ms 83192 KB Output is correct
21 Correct 160 ms 80376 KB Output is correct
22 Correct 161 ms 81912 KB Output is correct
23 Correct 188 ms 82296 KB Output is correct
24 Correct 173 ms 83032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1794 ms 182932 KB Output is correct
2 Correct 3667 ms 280536 KB Output is correct
3 Correct 4253 ms 378024 KB Output is correct
4 Correct 4595 ms 475140 KB Output is correct
5 Correct 1463 ms 195404 KB Output is correct
6 Correct 2432 ms 305552 KB Output is correct
7 Correct 3401 ms 412724 KB Output is correct
8 Execution timed out 5298 ms 483588 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 79352 KB Output is correct
2 Correct 147 ms 79096 KB Output is correct
3 Correct 139 ms 79544 KB Output is correct
4 Correct 164 ms 79352 KB Output is correct
5 Correct 144 ms 79224 KB Output is correct
6 Correct 153 ms 79224 KB Output is correct
7 Correct 143 ms 79352 KB Output is correct
8 Correct 143 ms 79352 KB Output is correct
9 Correct 144 ms 79120 KB Output is correct
10 Correct 145 ms 79480 KB Output is correct
11 Correct 148 ms 79328 KB Output is correct
12 Correct 152 ms 79352 KB Output is correct
13 Correct 159 ms 80508 KB Output is correct
14 Correct 155 ms 82040 KB Output is correct
15 Correct 166 ms 82680 KB Output is correct
16 Correct 154 ms 83804 KB Output is correct
17 Correct 142 ms 80504 KB Output is correct
18 Correct 156 ms 81776 KB Output is correct
19 Correct 161 ms 82552 KB Output is correct
20 Correct 166 ms 83192 KB Output is correct
21 Correct 160 ms 80376 KB Output is correct
22 Correct 161 ms 81912 KB Output is correct
23 Correct 188 ms 82296 KB Output is correct
24 Correct 173 ms 83032 KB Output is correct
25 Correct 1794 ms 182932 KB Output is correct
26 Correct 3667 ms 280536 KB Output is correct
27 Correct 4253 ms 378024 KB Output is correct
28 Correct 4595 ms 475140 KB Output is correct
29 Correct 1463 ms 195404 KB Output is correct
30 Correct 2432 ms 305552 KB Output is correct
31 Correct 3401 ms 412724 KB Output is correct
32 Execution timed out 5298 ms 483588 KB Time limit exceeded
33 Halted 0 ms 0 KB -