#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define MOD 1000000007
#define fi first
#define se second
#define ll long long
#define ii pair<int,int>
#define heap_max(a) priority_queue<a>
#define heap_min(a) priority_queue<a, vector<a>, greater <a>>
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define el cout << '\n'
#define rep(i, n) for(int i = 0; i < (n); i++)
#define For(i, a, b) for(int i = (a); i <= (b); i++)
#define Fod(i, a, b) for(int i = (a); i >= (b); i--)
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b){
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b){
if (a < b) return a = b, true;
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) {return l + rng() % (r - l + 1);}
const int N = 2e5 + 10;
int n, q, a[N], tin[N], tout[N], timer = 0;
struct Query{
string t;
int u, v;
} qry[N];
vector <int> ke[N];
void DFS(int u) {
tin[u] = ++timer;
for(int &v : ke[u]) DFS(v);
tout[u] = timer;
}
struct Trie {
struct node{
node *child[2];
set <int> s;
node() {
memset(child, 0, sizeof child);
}
};
node *root;
Trie() {
root = new node();
}
void add(int u) {
node *p = root;
Fod(i, 30, 0) {
int k = ((a[u] >> i) & 1);
if(p -> child[k] == nullptr) p -> child[k] = new node();
p = p -> child[k];
p -> s.insert(tin[u]);
}
}
int get(int x, int l, int r) {
node *p = root;
int res = 0;
Fod(i, 30, 0) {
int k = ((x >> i) & 1);
if(p -> child[k ^ 1] == nullptr) {
p = p -> child[k];
continue;
}
auto it = p -> child[k ^ 1] -> s.lower_bound(l);
if(it != p -> child[k ^ 1] -> s.end() && *it <= r) {
p = p -> child[k ^ 1];
res |= 1 << i;
} else p = p -> child[k];
}
return res;
}
}T;
void solve(){
cin >> q;
n = 1;
For(i, 1, q) {
cin >> qry[i].t >> qry[i].u >> qry[i].v;
if(qry[i].t[0] == 'A') {
ke[qry[i].u].eb(++n);
a[n] = a[qry[i].u] ^ qry[i].v;
qry[i].u = n;
}
}
DFS(1);
For(i, 1, q)
if(qry[i].t[0] == 'Q')
cout << T.get(a[qry[i].u], tin[qry[i].v], tout[qry[i].v]) << '\n';
else
T.add(qry[i].u);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int bla = uniform_int_distribution<int>(1, 100)(rng);
#define TASK ""
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int T = 1;
//cin >> T;
while(T--){
solve();
}
return 0;
}
Compilation message
klasika.cpp: In function 'int main()':
klasika.cpp:104:9: warning: unused variable 'bla' [-Wunused-variable]
104 | int bla = uniform_int_distribution<int>(1, 100)(rng);
| ^~~
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(TASK".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(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
25924 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
25924 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
61 ms |
34284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
25924 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |