답안 #446491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446491 2021-07-22T06:57:16 Z Saboon Klasika (COCI20_klasika) C++17
110 / 110
3131 ms 435916 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 2e5 + 5;

int typ[maxn], q1[maxn], q2[maxn];
int par[maxn];
vector<int> t[maxn];
int Time = 1, st[maxn], ft[maxn];

struct Trie{
	struct node{
		node* lc;
		node* rc;
		set<int> s;
		bool find(int l, int r){
			auto it = s.lower_bound(l);
			if (it == s.end() or r <= *it)
				return 0;
			return 1;
		}
	};
	node* root;
	Trie(){
		root = new node;
	}
	void add(int x, int idx){
		node* now = root;
		now->s.insert(idx);
		for (int i = 30; i >= 0; i--){
			if (x & (1 << i)){
				if (now->rc == NULL)
					now->rc = new node;
				now = now->rc;
			}
			else{
				if (now->lc == NULL)
					now->lc = new node;
				now = now->lc;
			}
			now->s.insert(idx);
		}
	}
	int get(int x, int l, int r){
		node* now = root;
		int ret = 0;
		for (int i = 30; i >= 0; i--){
			if (x & (1 << i)){
				if (now->lc != NULL and now->lc->find(l, r)){
					ret |= (1 << i);
					now = now->lc;
				}
				else
					now = now->rc;
			}
			else{
				if (now->rc != NULL and now->rc->find(l, r)){
					ret |= (1 << i);
					now = now->rc;
				}
				else
					now = now->lc;
			}
		}
		return ret;
	}
};

void dfs(int v){
	st[v] = Time ++;
	for (auto u : t[v])
		dfs(u);
	ft[v] = Time;
}

int main(){
	ios_base::sync_with_stdio(false);
	int q;
	cin >> q;
	int n = 1;
	for (int i = 1; i <= q; i++){
		string type;
		int x, y;
		cin >> type >> x >> y;
		typ[i] = 1, q1[i] = x, q2[i] = y;
		if (type == "Add"){
			t[x].push_back(++n);
			typ[i] = 0;
			par[n] = (par[x] ^ y);
		}
	}
	dfs(1);
	Trie T;
	T.add(par[1], st[1]);
	int m = 2;
	for (int i = 1; i <= q; i++){
		if (typ[i] == 0){
			int x = q1[i], y = q2[i];
			T.add(par[m], st[m]);
			m ++;
		}
		else{
			int x = q1[i], y = q2[i];
			cout << T.get(par[x], st[y], ft[y]) << '\n';
		}
	}
}

Compilation message

klasika.cpp: In function 'int main()':
klasika.cpp:100:8: warning: unused variable 'x' [-Wunused-variable]
  100 |    int x = q1[i], y = q2[i];
      |        ^
klasika.cpp:100:19: warning: unused variable 'y' [-Wunused-variable]
  100 |    int x = q1[i], y = q2[i];
      |                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 4 ms 5276 KB Output is correct
3 Correct 4 ms 5452 KB Output is correct
4 Correct 4 ms 5580 KB Output is correct
5 Correct 4 ms 5148 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 5 ms 5580 KB Output is correct
9 Correct 4 ms 5196 KB Output is correct
10 Correct 4 ms 5348 KB Output is correct
11 Correct 5 ms 5452 KB Output is correct
12 Correct 4 ms 5580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 4 ms 5276 KB Output is correct
3 Correct 4 ms 5452 KB Output is correct
4 Correct 4 ms 5580 KB Output is correct
5 Correct 4 ms 5148 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 5 ms 5580 KB Output is correct
9 Correct 4 ms 5196 KB Output is correct
10 Correct 4 ms 5348 KB Output is correct
11 Correct 5 ms 5452 KB Output is correct
12 Correct 4 ms 5580 KB Output is correct
13 Correct 7 ms 6476 KB Output is correct
14 Correct 9 ms 7756 KB Output is correct
15 Correct 12 ms 8996 KB Output is correct
16 Correct 16 ms 10124 KB Output is correct
17 Correct 9 ms 6344 KB Output is correct
18 Correct 9 ms 7628 KB Output is correct
19 Correct 11 ms 8872 KB Output is correct
20 Correct 13 ms 10032 KB Output is correct
21 Correct 7 ms 6440 KB Output is correct
22 Correct 9 ms 7628 KB Output is correct
23 Correct 11 ms 8872 KB Output is correct
24 Correct 12 ms 10060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 820 ms 123860 KB Output is correct
2 Correct 1340 ms 230388 KB Output is correct
3 Correct 1933 ms 332604 KB Output is correct
4 Correct 2500 ms 435500 KB Output is correct
5 Correct 811 ms 122440 KB Output is correct
6 Correct 1401 ms 227780 KB Output is correct
7 Correct 1928 ms 328516 KB Output is correct
8 Correct 2521 ms 429692 KB Output is correct
9 Correct 786 ms 121908 KB Output is correct
10 Correct 1360 ms 228488 KB Output is correct
11 Correct 2029 ms 330744 KB Output is correct
12 Correct 2613 ms 431384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 4 ms 5276 KB Output is correct
3 Correct 4 ms 5452 KB Output is correct
4 Correct 4 ms 5580 KB Output is correct
5 Correct 4 ms 5148 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 5 ms 5580 KB Output is correct
9 Correct 4 ms 5196 KB Output is correct
10 Correct 4 ms 5348 KB Output is correct
11 Correct 5 ms 5452 KB Output is correct
12 Correct 4 ms 5580 KB Output is correct
13 Correct 7 ms 6476 KB Output is correct
14 Correct 9 ms 7756 KB Output is correct
15 Correct 12 ms 8996 KB Output is correct
16 Correct 16 ms 10124 KB Output is correct
17 Correct 9 ms 6344 KB Output is correct
18 Correct 9 ms 7628 KB Output is correct
19 Correct 11 ms 8872 KB Output is correct
20 Correct 13 ms 10032 KB Output is correct
21 Correct 7 ms 6440 KB Output is correct
22 Correct 9 ms 7628 KB Output is correct
23 Correct 11 ms 8872 KB Output is correct
24 Correct 12 ms 10060 KB Output is correct
25 Correct 820 ms 123860 KB Output is correct
26 Correct 1340 ms 230388 KB Output is correct
27 Correct 1933 ms 332604 KB Output is correct
28 Correct 2500 ms 435500 KB Output is correct
29 Correct 811 ms 122440 KB Output is correct
30 Correct 1401 ms 227780 KB Output is correct
31 Correct 1928 ms 328516 KB Output is correct
32 Correct 2521 ms 429692 KB Output is correct
33 Correct 786 ms 121908 KB Output is correct
34 Correct 1360 ms 228488 KB Output is correct
35 Correct 2029 ms 330744 KB Output is correct
36 Correct 2613 ms 431384 KB Output is correct
37 Correct 1405 ms 124764 KB Output is correct
38 Correct 1936 ms 230436 KB Output is correct
39 Correct 2354 ms 335248 KB Output is correct
40 Correct 2784 ms 435916 KB Output is correct
41 Correct 1474 ms 122924 KB Output is correct
42 Correct 1998 ms 227532 KB Output is correct
43 Correct 2512 ms 328692 KB Output is correct
44 Correct 2913 ms 428732 KB Output is correct
45 Correct 1584 ms 122360 KB Output is correct
46 Correct 2345 ms 228488 KB Output is correct
47 Correct 2782 ms 329440 KB Output is correct
48 Correct 3131 ms 431228 KB Output is correct