This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Author : SamPV - HaiHoang
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define MASK(x) (1LL << (x))
#define BIT(x, k) (((x) >> (k)) & 1LL)
#define For(i, a, b) for (int i = (a); i <= (b); ++i)
#define Ford(i, a, b) for (int i = (a); i >= (b); --i)
#define Rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
const int mod = 1e9 + 7;
template <class X, class Y> bool maximize(X &a, Y b) {
if(a < b) return a = b,true;
return false;
}
template <class X, class Y> bool minimize(X &a, Y b) {
if(a > b) return a = b,true;
return false;
}
void file() {
#define TASK "TASK"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {return l + rng() % (r - l + 1);}
const int MAXN = 2e5 + 5;
struct QUERY{
int u, v;
bool type;
}q[MAXN];
int time_in[MAXN], time_out[MAXN], N = 1, Q, XOR[MAXN], run;
vector <int> ke[MAXN];
struct Tree{
struct node{
node *child[2];
set <int> s;
node(){
child[0] = child[1] = nullptr;
s = set <int> ();
}
}*root;
Tree(){
root = new node();
}
void add(int x, int pos){
node *tmp = root;
Ford(i, 30, 0){
int k = BIT(x, i);
if(tmp -> child[k] == nullptr) tmp -> child[k] = new node();
tmp = tmp -> child[k];
tmp -> s.insert(pos);
}
}
int MaxXor(int x, int l, int r){
node *tmp = root;
int res = 0;
Ford(i, 30, 0){
int k = BIT(x, i);
if(tmp ->child[k ^ 1] != nullptr){
set <int> ::iterator it = tmp ->child[1 ^ k] ->s.lower_bound(l);
if(*it <= r && it != tmp -> child[1 ^ k] -> s.end()){
tmp = tmp ->child[1 ^ k];
res |= MASK(i);
} else tmp = tmp -> child[k];
} else tmp = tmp ->child[k];
}
return res;
}
}trie;
void dfs(int u, int par = -1){
time_in[u] = ++run;
// cout << u << " " << XOR[u] << '\n';
for (auto v : ke[u]) if(v != par){
dfs(v, u);
}
time_out[u] = run;
}
void process(void) {
cin >> Q;
For(i, 1, Q){
string s;
cin >> s >> q[i].u >> q[i].v;
if(s == "Add"){
q[i].type = 1; ++N;
ke[q[i].u].push_back(N);
ke[N].push_back(q[i].u);
XOR[N] = XOR[q[i].u] ^ q[i].v;
q[i].u = N;
}
else q[i].type = 0;
}
dfs(1, 0);
trie.add(XOR[1], time_in[1]);
For(i, 1, Q){
if(q[i].type){
trie.add(XOR[q[i].u], time_in[q[i].u]);
} else cout << trie.MaxXor(XOR[q[i].u], time_in[q[i].v], time_out[q[i].v]) << '\n';
}
}
signed main() {
file();
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
process();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message (stderr)
klasika.cpp: In function 'void file()':
klasika.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
klasika.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | 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... |