답안 #340040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
340040 2020-12-26T15:55:28 Z sinamhdv Klasika (COCI20_klasika) C++11
110 / 110
3054 ms 439760 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)INF * INF;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define msub(a, b) ((mod + ((a) - (b)) % mod) % mod)
#define mdiv(a, b) ((a) * poww((b), mod - 2) % mod)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'

struct QR
{
	char op;
	int x, y;
};

#define MAXN 200100
#define LOGN 31

int n, q;
int dp[MAXN];
vector<pii> adj[MAXN];
QR qr[MAXN];
int sttime[MAXN], fntime[MAXN], timer;

void dfs(int v, int x)
{
	sttime[v] = timer++;
	dp[v] = x;
	for (pii p : adj[v])
	{
		dfs(p.fr, x ^ p.sc);
	}
	fntime[v] = timer;
}

inline bool hasrng(const set<int> &ts, int l, int r)
{
	return ts.lower_bound(l) != ts.upper_bound(r);
}

struct node
{
	node *cld[2];
	set<int> ts;
};

node trie;

void add(int i, int x)
{
	node *v = &trie;
	for (int bpos = LOGN - 1; bpos >= 0; bpos--)
	{
		int c = (x & (1 << bpos)) >> bpos;
		if (!v -> cld[c])
		{
			v -> cld[c] = new node;
			memset(v -> cld[c] -> cld, 0, 2 * sizeof(node*));
		}
		(v -> ts).insert(i);
		v = v -> cld[c];
	}
	(v -> ts).insert(i);
}

int query(int l, int r, int x)
{
	int res = 0;
	node *v = &trie;
	for (int bpos = LOGN - 1; bpos >= 0; bpos--)
	{
		int b = (x & (1 << bpos)) >> bpos;
		if (v -> cld[1 - b] && hasrng(v -> cld[1 - b] -> ts, l, r))
		{
			res |= (1 << bpos);
			v = v -> cld[1 - b];
		}
		else
		{
			v = v -> cld[b];
		}
	}
	return res;
}

int32_t main(void)
{
	fast_io;
	n = 1;
	cin >> q;
	FOR(i, 0, q)
	{
		char op[10];
		int x, y;
		cin >> op >> x >> y;
		qr[i] = {op[0], x, y};
		if (op[0] == 'A')
		{
			n++;
			adj[x].pb(mp(n, y));
		}
	}
	dfs(1, 0);
	
	int cnt = 1;
	add(sttime[1], dp[1]);
	FOR(i, 0, q)
	{
		if (qr[i].op == 'A')
		{
			cnt++;
			add(sttime[cnt], dp[cnt]);
		}
		else
		{
			cout << query(sttime[qr[i].y], fntime[qr[i].y] - 1, dp[qr[i].x]) << endl;
		}
	}
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5368 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 4 ms 5612 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5356 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5612 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5356 KB Output is correct
11 Correct 4 ms 5484 KB Output is correct
12 Correct 4 ms 5632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5368 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 4 ms 5612 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5356 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5612 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5356 KB Output is correct
11 Correct 4 ms 5484 KB Output is correct
12 Correct 4 ms 5632 KB Output is correct
13 Correct 8 ms 6508 KB Output is correct
14 Correct 9 ms 7788 KB Output is correct
15 Correct 11 ms 9068 KB Output is correct
16 Correct 12 ms 10220 KB Output is correct
17 Correct 8 ms 6380 KB Output is correct
18 Correct 10 ms 7660 KB Output is correct
19 Correct 13 ms 9068 KB Output is correct
20 Correct 12 ms 10124 KB Output is correct
21 Correct 8 ms 6508 KB Output is correct
22 Correct 9 ms 7788 KB Output is correct
23 Correct 11 ms 8940 KB Output is correct
24 Correct 13 ms 10092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1122 ms 123116 KB Output is correct
2 Correct 1688 ms 229484 KB Output is correct
3 Correct 2229 ms 332200 KB Output is correct
4 Correct 2640 ms 439020 KB Output is correct
5 Correct 1031 ms 122860 KB Output is correct
6 Correct 1674 ms 228076 KB Output is correct
7 Correct 2185 ms 329196 KB Output is correct
8 Correct 2730 ms 430176 KB Output is correct
9 Correct 1040 ms 122476 KB Output is correct
10 Correct 1695 ms 229208 KB Output is correct
11 Correct 2241 ms 331756 KB Output is correct
12 Correct 2689 ms 432356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5368 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 4 ms 5612 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5356 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5612 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5356 KB Output is correct
11 Correct 4 ms 5484 KB Output is correct
12 Correct 4 ms 5632 KB Output is correct
13 Correct 8 ms 6508 KB Output is correct
14 Correct 9 ms 7788 KB Output is correct
15 Correct 11 ms 9068 KB Output is correct
16 Correct 12 ms 10220 KB Output is correct
17 Correct 8 ms 6380 KB Output is correct
18 Correct 10 ms 7660 KB Output is correct
19 Correct 13 ms 9068 KB Output is correct
20 Correct 12 ms 10124 KB Output is correct
21 Correct 8 ms 6508 KB Output is correct
22 Correct 9 ms 7788 KB Output is correct
23 Correct 11 ms 8940 KB Output is correct
24 Correct 13 ms 10092 KB Output is correct
25 Correct 1122 ms 123116 KB Output is correct
26 Correct 1688 ms 229484 KB Output is correct
27 Correct 2229 ms 332200 KB Output is correct
28 Correct 2640 ms 439020 KB Output is correct
29 Correct 1031 ms 122860 KB Output is correct
30 Correct 1674 ms 228076 KB Output is correct
31 Correct 2185 ms 329196 KB Output is correct
32 Correct 2730 ms 430176 KB Output is correct
33 Correct 1040 ms 122476 KB Output is correct
34 Correct 1695 ms 229208 KB Output is correct
35 Correct 2241 ms 331756 KB Output is correct
36 Correct 2689 ms 432356 KB Output is correct
37 Correct 1716 ms 126120 KB Output is correct
38 Correct 2295 ms 232512 KB Output is correct
39 Correct 2598 ms 338168 KB Output is correct
40 Correct 2798 ms 439760 KB Output is correct
41 Correct 1774 ms 123500 KB Output is correct
42 Correct 2308 ms 228076 KB Output is correct
43 Correct 2692 ms 329248 KB Output is correct
44 Correct 3026 ms 429504 KB Output is correct
45 Correct 1952 ms 122920 KB Output is correct
46 Correct 2613 ms 228972 KB Output is correct
47 Correct 2862 ms 330348 KB Output is correct
48 Correct 3054 ms 432224 KB Output is correct