#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<pair<int, int>>G[N];
int par[N];
int xxor[N];
long long bb, answer = 0;
bool is[N];
void dfs(int node, int parent, long long ans)
{
if (is[node])
{
answer = max(answer, ans);
}
for (auto& i : G[node])
{
if (i.first == parent)continue;
dfs(i.first, node, (ans ^ i.second));
}
}
void dfs1(int node, int parent)
{
is[node] = true;
for (auto i : G[node])
{
if (i.first != parent)
dfs1(i.first, node);
}
}
struct Trie {
int g[30 * N][2], cnt[30 * N], root = 1, new_g = 1;
void add(int x)
{
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;
}
cur_node = g[cur_node][bit];
cnt[cur_node]++;
}
}
void del(int x)
{
int cur_node = root;
for (int i = 30; i >= 0; i--)
{
int bit = (x >> i) & 1;
cur_node = g[cur_node][bit];
cnt[cur_node]--;
}
}
int query(long long x)
{
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]])
{
ans += (1 << i);
cur_node = g[cur_node][1 - bit];
}
else
{
cur_node = g[cur_node][bit];
}
}
return ans;
}
}trie;
void solve()
{
trie.add(0);
int q;
cin >> q;
int ccnt = 1;
while (q--)
{
string s;
cin >> s;
if (s == "Add")
{
int x;
long long y;
cin >> x >> y;
ccnt++;
G[x].push_back({ ccnt, y });
G[ccnt].push_back({ x, y });
par[ccnt] = x;
xxor[ccnt] = (xxor[x] ^ y);
trie.add(xxor[ccnt]);
}
else
{
int a, b;
cin >> a >> b;
if (b == 1)
{
cout << trie.query(xxor[a]) << "\n";
}
else
{
dfs1(b, par[b]);
dfs(a, -1, 0);
cout << answer << "\n";
answer = 0;
for (int i = 1; i <= ccnt; i++)
is[i] = false;
}
}
}
}
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 time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |