-
Notifications
You must be signed in to change notification settings - Fork 7
/
persistent_segtree.cpp
225 lines (200 loc) · 5.07 KB
/
persistent_segtree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// persistent segment trees
// Codechef MAY17 Long Challenge
// Problem Name: Gotham PD
// Problem Code : GPD
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define pp pair<ll,ll>
#define mp make_pair
#define PI acos(-1.0)
#define all(v) v.begin(),v.end()
#define pb push_back
#define FOR(i,a,b) for(i=a;i<b;i++)
#define FREV(i,a,b) for(i=a;i>=b;i--)
#define INF 1e18
#ifndef ONLINE_JUDGE
#define gc getchar
#define pc putchar
#else
#define gc getchar_unlocked
#define pc putchar_unlocked
#endif
using namespace std;
int read_int() {
char c = gc();
while((c < '0' || c > '9') && c != '-') c = gc();
int ret = 0, neg = 0;
if (c == '-') neg = 1, c = gc();
while(c >= '0' && c <= '9') {
ret = 10 * ret + c - 48;
c = gc();
}
return neg ? -ret : ret;
}
ll read_ll() {
char c = gc();
while((c < '0' || c > '9') && c != '-') c = gc();
ll ret = 0;
int neg = 0;
if (c == '-') neg = 1, c = gc();
while(c >= '0' && c <= '9') {
ret = 10 * ret + c - 48;
c = gc();
}
return neg ? -ret : ret;
}
struct node {
node* left;
node* right;
node() {
this->left = NULL;
this->right = NULL;
}
};
vi parent(300005);
string bit_string;
vector<node*> tree_roots;
map<ll,ll> get_index_from_id;
void create_tree(node* &root, ll key, ll parent_index) {
node* temp = root;
bit_string = bitset<32>(key).to_string();
int i;
if (parent_index == -1) {
FOR(i,0,32) {
if (bit_string[i] == '0') {
root->left = new node();
root = root->left;
}
else {
root->right = new node();
root = root->right;
}
}
}
else {
node* parent_root = tree_roots[parent_index];
FOR(i,0,32) {
if (bit_string[i] == '0') {
root->left = new node();
if (parent_root != NULL) {
root->right = parent_root->right;
parent_root = parent_root->left;
}
root = root->left;
}
else {
root->right = new node();
if (parent_root != NULL) {
root->left = parent_root->left;
parent_root = parent_root->right;
}
root = root->right;
}
}
}
root = temp;
}
void solve(ll v, ll k, ll &min_answer, ll &max_answer) {
bit_string = bitset<32>(k).to_string();
node* root = tree_roots[v];
ll minim = 0, maxim = 0;
int i;
// find max
FREV(i,31,0) {
if (bit_string[31-i] == '0') {
if (root->right != NULL) {
maxim += (1<<i);
root = root->right;
}
else {
root = root->left;
}
}
else {
if (root->left != NULL) {
maxim += (1<<i);
root = root->left;
}
else {
root = root->right;
}
}
}
root = tree_roots[v];
// find min
FREV(i,31,0) {
if (bit_string[31-i] == '0') {
if (root->left != NULL) {
root = root->left;
}
else {
minim += (1<<i);
root = root->right;
}
}
else {
if (root->right != NULL) {
root = root->right;
}
else {
minim += (1<<i);
root = root->left;
}
}
}
min_answer = minim;
max_answer = maxim;
}
int main() {
ll i,u,v,r,r_key,k,q,n,min_answer,max_answer,index=0;
int t;
n = read_ll();
q = read_ll();
r = read_ll();
r_key = read_ll();
tree_roots.pb(new node());
get_index_from_id[r] = index;
parent[index] = -1;
create_tree(tree_roots[index], r_key, -1);
index++;
FOR(i,0,n-1) {
u = read_ll();
v = read_ll();
k = read_ll();
tree_roots.pb(new node());
get_index_from_id[u] = index;
parent[index] = get_index_from_id[v];
create_tree(tree_roots[index], k, parent[index]);
index++;
}
ll last_answer = 0;
FOR(i,0,q) {
t = read_int();
t ^= last_answer;
if(t == 0) {
v = read_ll();
u = read_ll();
k = read_ll();
u ^= last_answer;
v ^= last_answer;
k ^= last_answer;
tree_roots.pb(new node());
get_index_from_id[u] = index;
parent[index] = get_index_from_id[v];
create_tree(tree_roots[index], k, parent[index]);;
index++;
}
else {
v = read_ll();
k = read_ll();
v ^= last_answer;
k ^= last_answer;
solve(get_index_from_id[v],k,min_answer,max_answer);
printf("%lld %lld\n", min_answer, max_answer);
last_answer = min_answer^max_answer;
}
}
return 0;
}