#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ff first
#define ss second
const ll K = 2;
struct Vertex {
int to[K];
set<int>inds;
Vertex() {
for (int i = 0; i < K; i++) {
to[i] = -1;
}
}
};
ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 200007;
const ll LG = 30;
ll n, m, k;
vector<Vertex>p(1);
vector<pair<int, int>>q;
vector<pair<int, int>>g[N];
int vl[N];
int ind[N];
int sz[N];
int timer = 0;
void dfs(int v, int val = 0, int par = -1) {
sz[v] = 1;
ind[v] = timer++;
if (par != -1) {
vl[v] = vl[par] ^ val;
}
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i].ff;
int w = g[v][i].ss;
if (to != par) {
dfs(to, w, v);
sz[v] += sz[to];
}
}
}
void add(int x, int i) {
int v = 0;
p[v].inds.insert(i);
for (int j = LG; j >= 0; j--) {
int c = 0;
if (x & (1 << j)) {
c = 1;
}
if (p[v].to[c] == -1) {
p[v].to[c] = p.size();
p.emplace_back();
}
p[p[v].to[c]].inds.insert(i);
//cout << v << ": " << p[v].to[0] << " " << p[v].to[1] << endl;
v = p[v].to[c];
}
}
int get_ans(int x, int i) {
int v = 0;
int ans = 0;
for (int j = LG; j >= 0; j--) {
if (x & (1 << j)) {
if (p[v].to[0] != -1) {
auto it = p[p[v].to[0]].inds.lower_bound(ind[i]);
//cout << res << " 1 ";
if ((it != p[p[v].to[0]].inds.end()) && (*it < ind[i] + sz[i])) {
v = p[v].to[0];
ans += (1 << j);
}
else {
v = p[v].to[1];
}
}
else {
v = p[v].to[1];
}
}
else {
if (p[v].to[1] != -1) {
auto it = p[p[v].to[1]].inds.lower_bound(ind[i]);
//cout << res << " 2 ";
if ((it != p[p[v].to[1]].inds.end()) && (*it < ind[i] + sz[i])) {
v = p[v].to[1];
ans += (1 << j);
}
else {
v = p[v].to[0];
}
}
else {
v = p[v].to[0];
}
}
}
//cout << endl;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int cur = 2;
vl[1] = 0;
for (int i = 0; i < n; i++) {
int x, y;
string tp;
cin >> tp >> x >> y;
if (tp == "Add") {
g[x].push_back({ cur,y });
q.push_back({ -1,cur });
cur++;
}
else {
q.push_back({ x,y });
}
}
dfs(1);
/*for (int i = 1; i <= n; i++) {
cout << i << ": " << vl[i] << endl;
}
cout << endl;*/
for (int i = 0; i < n; i++) {
if (q[i].ff == -1) {
int v = q[i].ss;
//cout << "ADDING: " << v << endl;
add(vl[v], ind[v]);
//cout << "HASA STE\n";
}
else {
cout << get_ans(vl[q[i].ff], q[i].ss) << endl;
}
}
return 0;
}
/// ---- - -------- ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - -------- ------ -------- -- - - -
Compilation message
klasika.cpp: In function 'void dfs(int, int, int)':
klasika.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < g[v].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
881 ms |
122052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |