This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** I can do this all day **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl "\n"
const int N = 2e5 + 10;
const int M = 16 * N;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 30;
char C[N];
int q, n = 1, ptr, Cnt = 1, w[N], St[N], Fi[N], yal[N], child[N][2];
set < int > st[M];
vector < int > G[N];
pair < string, pii > Q[N];
void dfs(int v)
{
St[v] = ++ptr;
for(auto u : G[v])
{
w[u] = w[v] ^ yal[u];
dfs(u);
}
Fi[v] = ptr;
}
/// add trie -> x = w ash ta root, v = rasi ke mikhai add bokoni
inline void add_trie(int x, int v)
{
int node = 1;
for(int T = 30; ~T; T --)
{
int rem = x >> T & 1;
if(child[node][rem] == 0) /// set nashode
{
child[node][rem] = ++Cnt;
}
node = child[node][rem];
st[node].insert(St[v]);
}
}
/// check v, node -> aya toye set trie v rasi hast ke toye sub node bashe ya na
inline bool check(int v, int node)
{
auto it = st[v].lower_bound(St[node]);
if(it == st[v].end() || *it > Fi[node]) return 0;
return 1;
}
/// ask trie x, b -> maximum xor ke adadmoon = x, va toye sub v bashe
inline int ask_trie(int x, int b)
{
int node = 1, tot = 0;
for(int T = 30; ~T; T --)
{
int rem = x >> T & 1;
int other = child[node][1 ^ rem];
if(check(other, b))
{
tot += (1 << T);
node = other;
}
else
{
node = child[node][rem];
}
}
return tot;
}
int main()
{
scanf("%d", &q);
for(int i = 1; i <= q; i ++)
{
string s;
int a, b;
scanf("%s%d%d", C, &a, &b);
s = C;
Q[i] = Mp(s, Mp(a, b));
if(s == "Add")
{
++n;
G[a].push_back(n);
yal[n] = b;
}
}
dfs(1);
int _n = 1;
for(int i = 1; i <= q; i ++)
{
if(Q[i].F == "Add")
{
_n ++;
add_trie(w[_n], _n);
}
else
{
printf("%d\n", ask_trie(w[Q[i].S.F], Q[i].S.S));
}
}
return 0;
}
/*!
stay away from negative people they have a problem for every solution !
*/
/*
4
Add 1 5
Query 1 1
Add 1 7
Query 1 1
*/
Compilation message (stderr)
klasika.cpp: In function 'int main()':
klasika.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
97 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
klasika.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%s%d%d", C, &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |