#include <bits/stdc++.h>
using namespace std;
#define fod(i,a,b) for(int i = (int) (a); i <= (int) (b); i++)
#define fok(i,a,b) for(int i = (int) (a); i >= (int) (b); i--)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define el '\n'
#define ve vector
#define vi ve<int>
#define vll ve<ll>
#define mask(a) (1LL<<(a))
#define BIT(msk,i) (msk>>(i)&1LL)
using namespace std;
template <class T> bool mini(T &a, T b){ return (a > (b)) ? a = (b), 1 : 0; }
template <class T> bool maxi(T &a, T b){ return (a < (b)) ? a = (b), 1 : 0; }
#define name "klasika"
int n = 1, q;
const int N = 2e5;
struct Query{
bool new_node;
int x, y;
}query[N + 5];
ve <ve<pii>> g;
int in[N + 5], out[N + 5], timedfs = 0;
int f[N + 5];
void dfs(int u, int p){
in[u] = ++timedfs;
for(pii who : g[u]) if(who.fi != p){
int v, w; tie(v, w) = who;
f[v] = f[u] xor w;
dfs(v, u);
}
out[u] = timedfs;
}
int num_node = 1, trie[31 * N + 5][2];
vi ids[31 * N + 5];
bool sorted[31 * N + 5];
#define all(a) a.begin(), a.end()
inline void add(int val, int id){
int i = 1;
fok(j,30,0){
int w = BIT(val, j);
if(!trie[i][w]) trie[i][w] = ++num_node;
i = trie[i][w];
ids[i].pb(id);
sorted[i] = 0;
}
}
int get(int val, int l, int r){
int i = 1;
int res = 0;
fok(j,30,0){
int w = BIT(val, j);
if(!trie[i][w^1]) i = trie[i][w];
else{
if(!sorted[trie[i][w^1]]) sort(all(ids[trie[i][w^1]])), sorted[trie[i][w^1]] = 1;
if(lower_bound(all(ids[trie[i][w^1]]), l) == upper_bound(all(ids[trie[i][w^1]]), r)){
i = trie[i][w];
continue;
}
res += mask(j);
i = trie[i][w^1];
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> q;
g.resize(q + 1);
fod(i,1,q){
string s; cin >> s;
int x, y; cin >> x >> y;
if(s == "Add"){
n++;
g[x].pb(mp(n, y));
g[n].pb(mp(x, y));
query[i] = Query{1, n, y};
}
else query[i] = Query{0, x, y};
}
dfs(1, 0);
add(f[1], in[1]);
fod(i,1,q){
if(query[i].new_node){
add(f[query[i].x], in[query[i].x]);
}
else{
cout << get(f[query[i].x], in[query[i].y], out[query[i].y]) << el;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
146000 KB |
Output is correct |
2 |
Correct |
73 ms |
146004 KB |
Output is correct |
3 |
Correct |
76 ms |
146160 KB |
Output is correct |
4 |
Correct |
61 ms |
146004 KB |
Output is correct |
5 |
Correct |
58 ms |
146008 KB |
Output is correct |
6 |
Correct |
68 ms |
146012 KB |
Output is correct |
7 |
Correct |
75 ms |
146000 KB |
Output is correct |
8 |
Correct |
54 ms |
146004 KB |
Output is correct |
9 |
Correct |
55 ms |
146000 KB |
Output is correct |
10 |
Correct |
57 ms |
145956 KB |
Output is correct |
11 |
Correct |
62 ms |
146216 KB |
Output is correct |
12 |
Correct |
54 ms |
146236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
146000 KB |
Output is correct |
2 |
Correct |
73 ms |
146004 KB |
Output is correct |
3 |
Correct |
76 ms |
146160 KB |
Output is correct |
4 |
Correct |
61 ms |
146004 KB |
Output is correct |
5 |
Correct |
58 ms |
146008 KB |
Output is correct |
6 |
Correct |
68 ms |
146012 KB |
Output is correct |
7 |
Correct |
75 ms |
146000 KB |
Output is correct |
8 |
Correct |
54 ms |
146004 KB |
Output is correct |
9 |
Correct |
55 ms |
146000 KB |
Output is correct |
10 |
Correct |
57 ms |
145956 KB |
Output is correct |
11 |
Correct |
62 ms |
146216 KB |
Output is correct |
12 |
Correct |
54 ms |
146236 KB |
Output is correct |
13 |
Correct |
76 ms |
146512 KB |
Output is correct |
14 |
Correct |
56 ms |
147008 KB |
Output is correct |
15 |
Correct |
67 ms |
147280 KB |
Output is correct |
16 |
Correct |
64 ms |
147688 KB |
Output is correct |
17 |
Correct |
70 ms |
146512 KB |
Output is correct |
18 |
Correct |
58 ms |
146768 KB |
Output is correct |
19 |
Correct |
69 ms |
147316 KB |
Output is correct |
20 |
Correct |
84 ms |
147540 KB |
Output is correct |
21 |
Correct |
63 ms |
146448 KB |
Output is correct |
22 |
Correct |
57 ms |
146940 KB |
Output is correct |
23 |
Correct |
61 ms |
147292 KB |
Output is correct |
24 |
Correct |
58 ms |
147608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5021 ms |
186828 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
146000 KB |
Output is correct |
2 |
Correct |
73 ms |
146004 KB |
Output is correct |
3 |
Correct |
76 ms |
146160 KB |
Output is correct |
4 |
Correct |
61 ms |
146004 KB |
Output is correct |
5 |
Correct |
58 ms |
146008 KB |
Output is correct |
6 |
Correct |
68 ms |
146012 KB |
Output is correct |
7 |
Correct |
75 ms |
146000 KB |
Output is correct |
8 |
Correct |
54 ms |
146004 KB |
Output is correct |
9 |
Correct |
55 ms |
146000 KB |
Output is correct |
10 |
Correct |
57 ms |
145956 KB |
Output is correct |
11 |
Correct |
62 ms |
146216 KB |
Output is correct |
12 |
Correct |
54 ms |
146236 KB |
Output is correct |
13 |
Correct |
76 ms |
146512 KB |
Output is correct |
14 |
Correct |
56 ms |
147008 KB |
Output is correct |
15 |
Correct |
67 ms |
147280 KB |
Output is correct |
16 |
Correct |
64 ms |
147688 KB |
Output is correct |
17 |
Correct |
70 ms |
146512 KB |
Output is correct |
18 |
Correct |
58 ms |
146768 KB |
Output is correct |
19 |
Correct |
69 ms |
147316 KB |
Output is correct |
20 |
Correct |
84 ms |
147540 KB |
Output is correct |
21 |
Correct |
63 ms |
146448 KB |
Output is correct |
22 |
Correct |
57 ms |
146940 KB |
Output is correct |
23 |
Correct |
61 ms |
147292 KB |
Output is correct |
24 |
Correct |
58 ms |
147608 KB |
Output is correct |
25 |
Execution timed out |
5021 ms |
186828 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |