#include <bits/stdc++.h>
#define ll long long
#define el cout << '\n'
#define BIT(n) (1ll << n)
#define bit(mask, i) ((mask >> i) & 1)
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
const int maxn = 2e5;
const int maxbit = 30;
const int INF = 1e9;
struct Trie
{
struct Node
{
int child[2];
int minId;
Node()
{
memset(child, -1, sizeof child);
minId = INF;
}
};
vector<Node> nodes;
Trie()
{
nodes.push_back(Node());
}
void addNum(int u, int id)
{
int pos = 0;
for (int i = maxbit; i >= 0; i--)
{
int e = bit(u, i);
if (nodes[pos].child[e] == -1)
{
nodes.push_back(Node());
nodes[pos].child[e] = nodes.size() - 1;
}
pos = nodes[pos].child[e];
nodes[pos].minId = min(nodes[pos].minId, id);
}
}
int getMaxXor(int u, int id)
{
int pos = 0, ans = 0;
for (int i = maxbit; i >= 0; i--)
{
int needBit = bit(u, i) ^ 1;
int c1 = nodes[pos].child[needBit];
int c0 = nodes[pos].child[needBit ^ 1];
if (c1 != -1 && nodes[c1].minId <= id)
{
ans |= BIT(i);
pos = c1;
}
else pos = c0;
}
return ans;
}
};
int q, ans[maxn + 10], dp[maxn + 10], n = 1, id[maxn + 10];
vector<ii> query[maxn + 10];
vector<int> adj[maxn + 10], v[maxn + 10];
Trie trie[maxn + 10];
void dfs(int top)
{
v[top].push_back(top);
trie[top].addNum(dp[top], id[top]);
for (int next_top : adj[top])
{
dfs(next_top);
if (v[top].size() < v[next_top].size())
{
swap(v[top], v[next_top]);
swap(trie[top], trie[next_top]);
}
for (auto x : v[next_top])
{
v[top].push_back(x);
trie[top].addNum(dp[x], id[x]);
}
}
for (ii p : query[top])
{
int dist = p.fi;
int idx = p.se;
ans[idx] = trie[top].getMaxXor(dist, idx);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("KLASIKA.INP", "r"))
{
freopen("KLASIKA.INP", "r", stdin);
freopen("KLASIKA.OUT", "w", stdout);
}
cin >> q;
for (int i = 1; i <= q; i++)
{
string t;
int a, b;
cin >> t >> a >> b;
if (t == "Add")
{
adj[a].push_back(++n);
dp[n] = dp[a] ^ b;
id[n] = i;
}
else
{
query[b].push_back({dp[a], i});
}
}
memset(ans, -1, sizeof ans);
dfs(1);
for (int i = 1; i <= q; i++)
if (ans[i] != -1)
cout << ans[i], el;
}
Compilation message (stderr)
klasika.cpp: In function 'int main()':
klasika.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen("KLASIKA.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
klasika.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen("KLASIKA.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |