#include <bits/stdc++.h>
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
using namespace std;
struct TrieNode {
TrieNode *c[2];
set<int> st;
TrieNode() {
st.clear();
c[0] = NULL;
c[1] = NULL;
}
bool checkRange(int l, int r) {
auto lb = st.lower_bound(l), ub = st.upper_bound(r);
if(ub == st.begin()) return 0;
if(lb == st.end()) return 0;
if(ub == st.end() && st.size() < 1) return 0;
ub--;
if((*lb) <= (*ub)) {
//cout<<"l:"<<l<<" r:"<<r<<"\n";
return 1;
}
return 0;
}
void insert(int num, int disc, int index) {
st.insert(disc);
if(index == -1) return;
bool f = (1 << index) & num;
if(c[f] == NULL) {
c[f] = new TrieNode();
}
c[f]->insert(num, disc, index-1);
}
int query(int num, int l, int r, int index) {
if(!checkRange(l,r)) {
return -1;
}
if(index == -1) return 0;
bool f = (1 << index) & num;
if(c[f] != NULL) {
int res = c[f]->query(num,l,r, index-1);
if(res != -1) {
//cout<<((1 << index) & num) + res<<"\n";
return ((1 << index) & num) + res;
}
}
assert(c[!f] != NULL);
int res = ((num ^ INT_MAX) & (1 << index) ) + c[!f]->query(num, l, r, index-1);
//cout<<res<<"\n";
return res;
}
};
const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 2e5+5;
const int MX = 31;
int n,m,q;
vector<array<int,3> > quer;
int timer = 0;
vector<int> tout(N),tin(N);
vector<vector<int> > adj(N);
vector<int> w(N,0), pathw(N,0);
void dfs(int node, int par) {
tin[node] = timer++;
pathw[node] = pathw[par] ^ w[node];
//cout<<"pathw[par]:"<<pathw[par]<<" w[node]:"<<w[node]<<" path[node]:"<< pathw[node]<<"\n";
for(auto c : adj[node]) {
if(c == par) continue;
dfs(c, node);
}
tout[node] = timer++;
}
void solve() {
cin>>q;
quer.resize(q);
int cnt = 2;
for(int i = 0; i<q; i++) {
string op;
cin>>op;
int a,b;
if(op == "Query") {
cin>>a>>b;
quer[i] = {0, a, b};
}
else if(op == "Add") {
cin>>a>>b;
adj[cnt].pb(a);
adj[a].pb(cnt);
//cout<<b<<"?\n";
w[cnt] = b;
quer[i] = {1, a, cnt};
cnt++;
}
}
dfs(1,0);
TrieNode *root = new TrieNode();
int ans = 0;
root->insert(0, tin[1], MX-1);
for(int i = 0; i<q; i++) {
//cout<<"patlamadı:"<<i<<"\n";
auto cur = quer[i];
if(cur[0]) {
root->insert(pathw[cur[2]], tin[cur[2]], MX-1);
}
else {
int x = pathw[cur[1]];
x = x ^ INT_MAX;
int tmp = root->query(x, tin[cur[2]], tout[cur[2]], MX-1);
ans = tmp ^ pathw[cur[1]];
cout<<ans<<"\n";
}
}
}
signed main() {
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
4 ms |
8540 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8536 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
4 ms |
8540 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8536 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8796 KB |
Output is correct |
13 |
Correct |
8 ms |
9708 KB |
Output is correct |
14 |
Correct |
8 ms |
10988 KB |
Output is correct |
15 |
Correct |
9 ms |
12124 KB |
Output is correct |
16 |
Correct |
9 ms |
13404 KB |
Output is correct |
17 |
Correct |
7 ms |
9564 KB |
Output is correct |
18 |
Correct |
8 ms |
10844 KB |
Output is correct |
19 |
Correct |
9 ms |
12060 KB |
Output is correct |
20 |
Correct |
10 ms |
13148 KB |
Output is correct |
21 |
Correct |
7 ms |
9564 KB |
Output is correct |
22 |
Correct |
9 ms |
11100 KB |
Output is correct |
23 |
Correct |
9 ms |
12124 KB |
Output is correct |
24 |
Correct |
10 ms |
13160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
809 ms |
124560 KB |
Output is correct |
2 |
Correct |
1200 ms |
231280 KB |
Output is correct |
3 |
Correct |
1478 ms |
333768 KB |
Output is correct |
4 |
Correct |
1795 ms |
436320 KB |
Output is correct |
5 |
Correct |
741 ms |
123052 KB |
Output is correct |
6 |
Correct |
1189 ms |
228176 KB |
Output is correct |
7 |
Correct |
1629 ms |
329016 KB |
Output is correct |
8 |
Correct |
2039 ms |
430116 KB |
Output is correct |
9 |
Correct |
733 ms |
122624 KB |
Output is correct |
10 |
Correct |
1126 ms |
228612 KB |
Output is correct |
11 |
Correct |
1530 ms |
331028 KB |
Output is correct |
12 |
Correct |
1855 ms |
431332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8540 KB |
Output is correct |
7 |
Correct |
4 ms |
8540 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8536 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
8796 KB |
Output is correct |
13 |
Correct |
8 ms |
9708 KB |
Output is correct |
14 |
Correct |
8 ms |
10988 KB |
Output is correct |
15 |
Correct |
9 ms |
12124 KB |
Output is correct |
16 |
Correct |
9 ms |
13404 KB |
Output is correct |
17 |
Correct |
7 ms |
9564 KB |
Output is correct |
18 |
Correct |
8 ms |
10844 KB |
Output is correct |
19 |
Correct |
9 ms |
12060 KB |
Output is correct |
20 |
Correct |
10 ms |
13148 KB |
Output is correct |
21 |
Correct |
7 ms |
9564 KB |
Output is correct |
22 |
Correct |
9 ms |
11100 KB |
Output is correct |
23 |
Correct |
9 ms |
12124 KB |
Output is correct |
24 |
Correct |
10 ms |
13160 KB |
Output is correct |
25 |
Correct |
809 ms |
124560 KB |
Output is correct |
26 |
Correct |
1200 ms |
231280 KB |
Output is correct |
27 |
Correct |
1478 ms |
333768 KB |
Output is correct |
28 |
Correct |
1795 ms |
436320 KB |
Output is correct |
29 |
Correct |
741 ms |
123052 KB |
Output is correct |
30 |
Correct |
1189 ms |
228176 KB |
Output is correct |
31 |
Correct |
1629 ms |
329016 KB |
Output is correct |
32 |
Correct |
2039 ms |
430116 KB |
Output is correct |
33 |
Correct |
733 ms |
122624 KB |
Output is correct |
34 |
Correct |
1126 ms |
228612 KB |
Output is correct |
35 |
Correct |
1530 ms |
331028 KB |
Output is correct |
36 |
Correct |
1855 ms |
431332 KB |
Output is correct |
37 |
Correct |
1285 ms |
125148 KB |
Output is correct |
38 |
Correct |
1785 ms |
230876 KB |
Output is correct |
39 |
Correct |
1977 ms |
335904 KB |
Output is correct |
40 |
Correct |
2147 ms |
436924 KB |
Output is correct |
41 |
Correct |
1437 ms |
123140 KB |
Output is correct |
42 |
Correct |
1923 ms |
227704 KB |
Output is correct |
43 |
Correct |
2178 ms |
328956 KB |
Output is correct |
44 |
Correct |
2366 ms |
428820 KB |
Output is correct |
45 |
Correct |
1588 ms |
122528 KB |
Output is correct |
46 |
Correct |
2134 ms |
228424 KB |
Output is correct |
47 |
Correct |
2360 ms |
329364 KB |
Output is correct |
48 |
Correct |
2357 ms |
431064 KB |
Output is correct |