답안 #340058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
340058 2020-12-26T17:29:04 Z sinamhdv Klasika (COCI20_klasika) C++11
110 / 110
3171 ms 430700 KB
// oj.uz COCI20_klasika
// solution using trie & set with static memory for trie
// reduced static memory size from 31 * MAXN to 16 * MAXN to avoid memory limit error
#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
#define MAXL (16 * MAXN)

int n, q;
int dp[MAXN];
vector<pii> cld[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 : cld[v])
	{
		dfs(p.fr, x ^ p.sc);
	}
	fntime[v] = timer;
}

int trie[MAXL][2];
set<int> ts[MAXL];
int tn = 1;

inline bool hasrng(int i, int l, int r)
{
	auto it = ts[i].lower_bound(l);
	if (it == ts[i].end() || *it > r)
		return false;
	return true;
}

void add(int i, int x)
{
	int v = 1;
	for (int bpos = LOGN - 1; bpos >= 0; bpos--)
	{
		int b = (x & (1 << bpos)) >> bpos;
		if (!trie[v][b])
		{
			tn++;
			trie[v][b] = tn;
		}
		ts[v].insert(i);
		v = trie[v][b];
	}
	ts[v].insert(i);
}

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

int32_t main(void)
{
	fast_io;
	n = 1;
	cin >> q;
	FOR(i, 0, q)
	{
		string op;
		int x, y;
		cin >> op >> x >> y;
		qr[i] = {op[0], x, y};
		if (op[0] == 'A')
		{
			n++;
			cld[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 97 ms 155500 KB Output is correct
2 Correct 96 ms 155628 KB Output is correct
3 Correct 98 ms 155628 KB Output is correct
4 Correct 100 ms 155684 KB Output is correct
5 Correct 97 ms 155500 KB Output is correct
6 Correct 97 ms 155628 KB Output is correct
7 Correct 96 ms 155756 KB Output is correct
8 Correct 98 ms 155884 KB Output is correct
9 Correct 100 ms 155628 KB Output is correct
10 Correct 96 ms 155628 KB Output is correct
11 Correct 97 ms 155628 KB Output is correct
12 Correct 97 ms 155756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 155500 KB Output is correct
2 Correct 96 ms 155628 KB Output is correct
3 Correct 98 ms 155628 KB Output is correct
4 Correct 100 ms 155684 KB Output is correct
5 Correct 97 ms 155500 KB Output is correct
6 Correct 97 ms 155628 KB Output is correct
7 Correct 96 ms 155756 KB Output is correct
8 Correct 98 ms 155884 KB Output is correct
9 Correct 100 ms 155628 KB Output is correct
10 Correct 96 ms 155628 KB Output is correct
11 Correct 97 ms 155628 KB Output is correct
12 Correct 97 ms 155756 KB Output is correct
13 Correct 101 ms 156396 KB Output is correct
14 Correct 104 ms 156908 KB Output is correct
15 Correct 103 ms 157676 KB Output is correct
16 Correct 103 ms 158316 KB Output is correct
17 Correct 100 ms 156140 KB Output is correct
18 Correct 106 ms 156908 KB Output is correct
19 Correct 102 ms 157548 KB Output is correct
20 Correct 104 ms 158188 KB Output is correct
21 Correct 99 ms 156140 KB Output is correct
22 Correct 102 ms 157036 KB Output is correct
23 Correct 103 ms 157548 KB Output is correct
24 Correct 105 ms 158316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 913 ms 228716 KB Output is correct
2 Correct 1498 ms 296420 KB Output is correct
3 Correct 2092 ms 362732 KB Output is correct
4 Correct 2692 ms 430060 KB Output is correct
5 Correct 951 ms 225772 KB Output is correct
6 Correct 1564 ms 291692 KB Output is correct
7 Correct 2212 ms 356076 KB Output is correct
8 Correct 2864 ms 421100 KB Output is correct
9 Correct 929 ms 225804 KB Output is correct
10 Correct 1559 ms 292552 KB Output is correct
11 Correct 2195 ms 358124 KB Output is correct
12 Correct 2868 ms 423276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 155500 KB Output is correct
2 Correct 96 ms 155628 KB Output is correct
3 Correct 98 ms 155628 KB Output is correct
4 Correct 100 ms 155684 KB Output is correct
5 Correct 97 ms 155500 KB Output is correct
6 Correct 97 ms 155628 KB Output is correct
7 Correct 96 ms 155756 KB Output is correct
8 Correct 98 ms 155884 KB Output is correct
9 Correct 100 ms 155628 KB Output is correct
10 Correct 96 ms 155628 KB Output is correct
11 Correct 97 ms 155628 KB Output is correct
12 Correct 97 ms 155756 KB Output is correct
13 Correct 101 ms 156396 KB Output is correct
14 Correct 104 ms 156908 KB Output is correct
15 Correct 103 ms 157676 KB Output is correct
16 Correct 103 ms 158316 KB Output is correct
17 Correct 100 ms 156140 KB Output is correct
18 Correct 106 ms 156908 KB Output is correct
19 Correct 102 ms 157548 KB Output is correct
20 Correct 104 ms 158188 KB Output is correct
21 Correct 99 ms 156140 KB Output is correct
22 Correct 102 ms 157036 KB Output is correct
23 Correct 103 ms 157548 KB Output is correct
24 Correct 105 ms 158316 KB Output is correct
25 Correct 913 ms 228716 KB Output is correct
26 Correct 1498 ms 296420 KB Output is correct
27 Correct 2092 ms 362732 KB Output is correct
28 Correct 2692 ms 430060 KB Output is correct
29 Correct 951 ms 225772 KB Output is correct
30 Correct 1564 ms 291692 KB Output is correct
31 Correct 2212 ms 356076 KB Output is correct
32 Correct 2864 ms 421100 KB Output is correct
33 Correct 929 ms 225804 KB Output is correct
34 Correct 1559 ms 292552 KB Output is correct
35 Correct 2195 ms 358124 KB Output is correct
36 Correct 2868 ms 423276 KB Output is correct
37 Correct 1509 ms 228460 KB Output is correct
38 Correct 2189 ms 295788 KB Output is correct
39 Correct 2652 ms 364012 KB Output is correct
40 Correct 3020 ms 430700 KB Output is correct
41 Correct 1674 ms 225644 KB Output is correct
42 Correct 2285 ms 291436 KB Output is correct
43 Correct 2741 ms 355920 KB Output is correct
44 Correct 3171 ms 420844 KB Output is correct
45 Correct 1703 ms 225900 KB Output is correct
46 Correct 2373 ms 292332 KB Output is correct
47 Correct 2740 ms 356792 KB Output is correct
48 Correct 3075 ms 423020 KB Output is correct