Submission #1285064

#TimeUsernameProblemLanguageResultExecution timeMemory
1285064vache_kocharyanKlasika (COCI20_klasika)C++20
33 / 110
1082 ms524640 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <unordered_set>

using namespace std;

typedef long long ll;

#define UNIQUE_SORT(vec) do { \
	sort((vec).begin(), (vec).end()); \
	(vec).erase(unique((vec).begin(), (vec).end()), (vec).end()); \
} while(0)

#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ss second
#define ff first
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define cinall(X) for(auto &i:X)cin >> i
#define printall(X) for(auto &i:X)cout << i

const int N = 2e5 + 5;
vector<int>G[N];
int xxor[N];

int t[N], tin[N], tout[N], u[N], v[N], a[N], b[N];
int y[N];

int timer = 0;

void dfs(int node, int parent)
{
	timer++;
	tin[node] = timer;
	for (auto i : G[node])
	{
		if (i == parent)continue;
		dfs(i, node);
	}
	timer++;
	tout[node] = timer;
}

int g[40 * N][2], cnt[40 * N], root = 1, new_g = 1;
set<int>tins[40 * N];

struct Trie {
	inline void add(int x, int new_tin)
	{
		int cur_node = root;
		for (int i = 30; i >= 0; i--)
		{
			int bit = (x >> i) & 1;
			if (g[cur_node][bit] == 0)
			{
				g[cur_node][bit] = ++new_g;
			}
			tins[cur_node].insert(new_tin);
			cur_node = g[cur_node][bit];
			cnt[cur_node]++;
		}
		tins[cur_node].insert(new_tin);
	}

	inline int query(long long x, int new_tin, int new_tout)
	{
		int cur_node = root, ans = 0;
		for (int i = 30; i >= 0; i--)
		{
			int bit = (x >> i) & 1;

			if (g[cur_node][1 - bit] && cnt[g[cur_node][1 - bit]])
			{
				auto it = tins[g[cur_node][1 - bit]].lower_bound(new_tin);
				if (it != tins[g[cur_node][1 - bit]].end() && *it <= new_tout)
				{
					ans += (1 << i);
					cur_node = g[cur_node][1 - bit];
				}
				else
				{
					cur_node = g[cur_node][bit];
				}
			}
			else
			{
				cur_node = g[cur_node][bit];
			}
		}
		return ans;
	}
}trie;


void solve()
{
	int q;
	cin >> q;
	int ccnt = 1;
	for (int i = 1; i <= q; i++)
	{
		string s;
		cin >> s;

		if (s == "Add")
		{
			t[i] = 1;
			int x;
			long long y;
			cin >> x >> y;

			ccnt++;
			
			u[i] = x;
			v[i] = ccnt;

			G[x].push_back(ccnt);
			xxor[ccnt] = xxor[x] ^ y;
		}
		else
		{
			cin >> a[i] >> b[i];
		}
	}

	dfs(1, 0);
	trie.add(0, tin[1]);

	for (int i = 1; i <= q; i++)
	{
		if (t[i] == 1)
		{
			trie.add(xxor[v[i]], tin[v[i]]);
		}
		else
		{
			cout << trie.query(xxor[a[i]], tin[b[i]], tout[b[i]]) << "\n";
		}
	}
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
		cout << endl;
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...