// ~~ icebear love attttt ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebearat"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
const int BLOCK_SIZE = 2709;
int q, n;
vector<ii> G[N];
int f[N], tin[N], tout[N], tour[N], timer = -1;
struct Query {
int type, u, v;
} Q[N];
struct Trie {
struct Node {
Node *child[2];
bool exist;
Node() {
child[0] = child[1] = NULL;
}
};
Node *root;
Trie() {
root = new Node();
}
void add(int x) {
Node *p = root;
RED(j, 31) {
int pos = BIT(x, j);
if (p -> child[pos] == NULL) p -> child[pos] = new Node();
p = p -> child[pos];
}
}
int maxXor(int x) {
Node *p = root;
int ans = 0;
RED(j, 31) {
int pos = BIT(x, j);
if (p -> child[pos ^ 1] != NULL) {
ans |= MASK(j);
p = p -> child[pos ^ 1];
}
else {
if (p -> child[pos] == NULL) return 0;
p = p -> child[pos];
}
}
return ans;
}
} trie[N / BLOCK_SIZE];
void dfs(int u, int par) {
tin[u] = ++timer;
tour[timer] = u;
for(ii x : G[u]) {
int v, w; tie(v, w) = x;
if (v == par) continue;
f[v] = f[u] ^ w;
dfs(v, u);
}
tout[u] = timer;
}
void init(void) {
cin >> q;
string type;
n = 1;
FOR(i, 1, q) {
cin >> type >> Q[i].u >> Q[i].v;
if (type == "Add") {
n++;
Q[i].type = 1;
G[Q[i].u].pb(mp(n, Q[i].v));
} else Q[i].type = 2;
}
dfs(1, -1);
}
void process(void) {
int node = 1;
trie[0].add(0);
FOR(i, 1, q) {
if (Q[i].type == 1) {
node++;
int pos = tin[node];
trie[pos / BLOCK_SIZE].add(f[node]);
} else {
int ans = 0;
int a = Q[i].u;
int l = tin[Q[i].v], r = tout[Q[i].v];
int blockL = (l + BLOCK_SIZE - 1) / BLOCK_SIZE;
int blockR = r / BLOCK_SIZE;
if (blockL >= blockR) {
FOR(j, l, r) {
int b = tour[j];
if (b <= node) maximize(ans, f[a] ^ f[b]);
}
cout << ans << '\n';
continue;
}
FOR(j, l, blockL * BLOCK_SIZE - 1) {
int b = tour[j];
if (0 < b && b <= node) maximize(ans, f[a] ^ f[b]);
}
FOR(j, blockR * BLOCK_SIZE, r) {
int b = tour[j];
if (0 < b && b <= node) maximize(ans, f[a] ^ f[b]);
}
FOR(j, blockL, blockR - 1) maximize(ans, trie[j].maxXor(f[a]));
cout << ans << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
klasika.cpp: In function 'int main()':
klasika.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
klasika.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | freopen(task".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... |