// 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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9308 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9348 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
3 ms |
9308 KB |
Output is correct |
9 |
Correct |
3 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9308 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9348 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
3 ms |
9308 KB |
Output is correct |
9 |
Correct |
3 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9308 KB |
Output is correct |
13 |
Correct |
5 ms |
10076 KB |
Output is correct |
14 |
Correct |
5 ms |
11356 KB |
Output is correct |
15 |
Correct |
6 ms |
12636 KB |
Output is correct |
16 |
Correct |
7 ms |
13660 KB |
Output is correct |
17 |
Correct |
4 ms |
10076 KB |
Output is correct |
18 |
Correct |
5 ms |
11408 KB |
Output is correct |
19 |
Correct |
7 ms |
12636 KB |
Output is correct |
20 |
Correct |
7 ms |
13660 KB |
Output is correct |
21 |
Correct |
4 ms |
10076 KB |
Output is correct |
22 |
Correct |
8 ms |
11356 KB |
Output is correct |
23 |
Correct |
7 ms |
12636 KB |
Output is correct |
24 |
Correct |
7 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
455 ms |
124184 KB |
Output is correct |
2 |
Correct |
766 ms |
228624 KB |
Output is correct |
3 |
Correct |
1088 ms |
328628 KB |
Output is correct |
4 |
Correct |
1406 ms |
429312 KB |
Output is correct |
5 |
Correct |
494 ms |
123344 KB |
Output is correct |
6 |
Correct |
901 ms |
226584 KB |
Output is correct |
7 |
Correct |
1300 ms |
326228 KB |
Output is correct |
8 |
Correct |
1767 ms |
425176 KB |
Output is correct |
9 |
Correct |
464 ms |
122468 KB |
Output is correct |
10 |
Correct |
812 ms |
226988 KB |
Output is correct |
11 |
Correct |
1201 ms |
327656 KB |
Output is correct |
12 |
Correct |
1530 ms |
426412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9308 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9348 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
3 ms |
9308 KB |
Output is correct |
9 |
Correct |
3 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9308 KB |
Output is correct |
13 |
Correct |
5 ms |
10076 KB |
Output is correct |
14 |
Correct |
5 ms |
11356 KB |
Output is correct |
15 |
Correct |
6 ms |
12636 KB |
Output is correct |
16 |
Correct |
7 ms |
13660 KB |
Output is correct |
17 |
Correct |
4 ms |
10076 KB |
Output is correct |
18 |
Correct |
5 ms |
11408 KB |
Output is correct |
19 |
Correct |
7 ms |
12636 KB |
Output is correct |
20 |
Correct |
7 ms |
13660 KB |
Output is correct |
21 |
Correct |
4 ms |
10076 KB |
Output is correct |
22 |
Correct |
8 ms |
11356 KB |
Output is correct |
23 |
Correct |
7 ms |
12636 KB |
Output is correct |
24 |
Correct |
7 ms |
13660 KB |
Output is correct |
25 |
Correct |
455 ms |
124184 KB |
Output is correct |
26 |
Correct |
766 ms |
228624 KB |
Output is correct |
27 |
Correct |
1088 ms |
328628 KB |
Output is correct |
28 |
Correct |
1406 ms |
429312 KB |
Output is correct |
29 |
Correct |
494 ms |
123344 KB |
Output is correct |
30 |
Correct |
901 ms |
226584 KB |
Output is correct |
31 |
Correct |
1300 ms |
326228 KB |
Output is correct |
32 |
Correct |
1767 ms |
425176 KB |
Output is correct |
33 |
Correct |
464 ms |
122468 KB |
Output is correct |
34 |
Correct |
812 ms |
226988 KB |
Output is correct |
35 |
Correct |
1201 ms |
327656 KB |
Output is correct |
36 |
Correct |
1530 ms |
426412 KB |
Output is correct |
37 |
Correct |
790 ms |
125132 KB |
Output is correct |
38 |
Correct |
1246 ms |
228736 KB |
Output is correct |
39 |
Correct |
1501 ms |
331276 KB |
Output is correct |
40 |
Correct |
1737 ms |
429708 KB |
Output is correct |
41 |
Correct |
910 ms |
123680 KB |
Output is correct |
42 |
Correct |
1391 ms |
226460 KB |
Output is correct |
43 |
Correct |
1750 ms |
326060 KB |
Output is correct |
44 |
Correct |
2029 ms |
424300 KB |
Output is correct |
45 |
Correct |
996 ms |
122888 KB |
Output is correct |
46 |
Correct |
1450 ms |
227148 KB |
Output is correct |
47 |
Correct |
1726 ms |
326284 KB |
Output is correct |
48 |
Correct |
1902 ms |
426572 KB |
Output is correct |