-
Notifications
You must be signed in to change notification settings - Fork 3
/
F.cpp
431 lines (350 loc) · 10.2 KB
/
F.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include<bits/stdc++.h>
#pragma GCC optimize("03")
using namespace std;
#define ll long long
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(a) a.begin(),a.end()
#define endl "\n"
#define sp " "
#define pb push_back
#define mp make_pair
#define vecvec(type, name, n, m, value) vector<vector<type>> name(n + 1, vector<type> (m + 1, value))
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) {cerr << "[" << #x << "] = ["; _print(x);}
#define reach cerr << "reached" << endl
#else
#define debug(x...)
#define reach
#endif
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7;
const int64_t inf = 0x3f3f3f3f, INF = 1e18, BIG_MOD = 489133282872437279;
/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// #define int int64_t
const int N = 2e5+100, L = 30;
int bit[2 * N];
struct Fenwick //one indexed
{
int n;
Fenwick(int n)
{
this->n = n + 10;
}
int sum(int idx)
{
int ret = 0;
for (++idx; idx > 0; idx -= idx & -idx)
ret += bit[idx];
return ret;
}
int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
void add(int idx, int delta)
{
for (++idx; idx < n; idx += idx & -idx) bit[idx] += delta;
}
};
Fenwick rq(N << 1);
struct node
{
pair<int, int> mn_l = mp(inf, inf), mn_r = mp(inf, inf);
node() : mn_l(mp(inf, inf)), mn_r(mp(inf, inf)) {};
};
bool cLazy[8 * N + 5];
int lazy[8 * N + 5];
vector<node> st(8 * N + 5);
struct SegTree
{
int N;
SegTree(int n)
{
N = n;
}
void merge(node &cur, node &l, node &r)
{
cur.mn_l = min(l.mn_l, r.mn_l);
cur.mn_r = min(l.mn_r, r.mn_r);
}
// be careful about that one lifting case in range add, range max qr for ex
void propagate(int ver, int L, int R)
{
if (L != R) // passing on lazy shit to children
{
cLazy[ver * 2] = cLazy[ver * 2 + 1] = 1;
lazy[ver * 2] = lazy[ver * 2 + 1] = lazy[ver];
}
st[ver].mn_l = mp(lazy[ver], L), st[ver].mn_r = mp(lazy[ver], -R);
cLazy[ver] = lazy[ver] = 0;
}
node Query(int ver, int L, int R, int i, int j)
{
if (cLazy[ver])
propagate(ver, L, R);
if (j < L || i > R)
return node();
if (i <= L && R <= j)
return st[ver];
int M = (L + R) / 2;
node left = Query(ver * 2, L, M, i, j), right = Query(ver * 2 + 1, M + 1, R, i, j), cur;
merge(cur, left, right);
return cur;
}
node pQuery(int ver, int L, int R, int pos)
{
if (cLazy[ver])
propagate(ver, L, R);
if (L == R)
return st[ver];
int M = (L + R) / 2;
if (pos <= M)
return pQuery(ver * 2, L, M, pos);
else
return pQuery(ver * 2 + 1, M + 1, R, pos);
}
void Update(int ver, int L, int R, int i, int j, int val)
{
if (cLazy[ver])
propagate(ver, L, R);
if (j < L || i > R)
return;
if (i <= L && R <= j)
{
cLazy[ver] = 1, lazy[ver] = val;
propagate(ver, L, R);
return;
}
int M = (L + R) / 2;
Update(ver * 2, L, M, i, j, val), Update(ver * 2 + 1, M + 1, R, i, j, val);
merge(st[ver], st[ver * 2], st[ver * 2 + 1]);
}
void pUpdate(int ver, int L, int R, int pos, int val)
{
if (cLazy[ver])
propagate(ver, L, R);
if (L == R)
{
st[ver].mn_l = mp(val, pos), st[ver].mn_r = mp(val, -pos);
return;
}
int M = (L + R) / 2;
if (pos <= M)
pUpdate(ver * 2, L, M, pos, val);
else
pUpdate(ver * 2 + 1, M + 1, R, pos, val);
if(cLazy[ver*2]) propagate(ver*2, L, M);
if(cLazy[ver*2 + 1]) propagate(ver*2 + 1, M + 1, R);
merge(st[ver], st[ver * 2], st[ver * 2 + 1]);
}
node query(int pos)
{
return pQuery(1, 1, N, pos);
}
node query(int l, int r)
{
return Query(1, 1, N, l, r);
}
void update(int pos, int val)
{
pUpdate(1, 1, N, pos, val);
}
void update(int l, int r, int val)
{
Update(1, 1, N, l, r, val);
}
};
SegTree cq(N << 1);
int n, q, root;
vector<int> adj[N];
//paths
int mx[N], p[N], rev[N << 1], dep[N];
//lca
int timer = 0;
int tin[N], tout[N], jump[N][L];
void dfs(int u, int p)
{
tin[u] = ++ timer;
jump[u][0] = p;
for(int i = 1; i < L; i ++) jump[u][i] = jump[jump[u][i - 1]][i - 1];
mx[u] = u;
for(auto v : adj[u])
if(v != p)
dep[v] = dep[u] + 1, dfs(v, u), mx[u] = max(mx[u], mx[v]);
rq.add(mx[u], 1);
tout[u] = ++ timer;
}
bool is_ancestor(int par, int node)
{
return (tin[par] <= tin[node] and tout[node] <= tout[par]);
}
int lca(int u, int v)
{
if(is_ancestor(u, v)) return u;
if(is_ancestor(v, u)) return v;
for(int i = L - 1; i >= 0; i --)
if(!is_ancestor(jump[u][i], v))
u = jump[u][i];
return jump[u][0];
}
int dis(int u, int v)
{
return (dep[u] + dep[v] - 2 * dep[lca(u, v)]);
}
int sub[N], par[N], val[N], head[N], sid[N];
bool heavy[N];
struct HLD
{
int n;
// for heavy paths
int timer = 0;
void init(int N)
{
n = N;
}
void build(int root)
{
vector<pair<int, int>> heavy_child(n + 5, mp(0, -1));
function<void(int)> structure = [&](int u)
{
sub[u] = 1;
for(auto v : adj[u])
if(v != par[u])
par[v] = u, structure(v), sub[u] += sub[v], heavy_child[u] = max(heavy_child[u], mp(sub[v], v));
if(heavy_child[u].second != -1) heavy[heavy_child[u].second] = 1;
};
structure(root);
for(int h = 1; h <= n; h ++)
{
if(heavy[h])
{
if(heavy[par[h]]) continue;
int u = h, v = h;
while(v != -1)
{
head[v] = h, sid[v] = ++ timer;
cq.update(sid[v], mx[v]);
v = heavy_child[v].second;
}
}
else
{
val[h] = mx[h];
}
}
}
void set_on_segment(int l, int r, int x)
{
auto qr = cq.query(l, r);
while(qr.mn_l.first < x)
{
int y = qr.mn_l.first, a = qr.mn_l.second, b = -qr.mn_r.second;
rq.add(y, -(b - a + 1));
cq.update(a, b, x);
rq.add(x, b - a + 1);
qr = cq.query(l, r);
}
}
void update(int u, int v, int x)
{
int l = lca(u, v);
vector<int> b = {u, v};
for(auto w : b)
{
while(w != par[l])
{
if(heavy[w])
{
if(is_ancestor(head[w], l))
{
set_on_segment(sid[l], sid[w], x);
w = par[l];
}
else
{
set_on_segment(sid[head[w]], sid[w], x);
w = par[head[w]];
}
}
else
{
rq.add(val[w], -1);
val[w] = x;
rq.add(val[w], +1);
w = par[w];
}
}
}
}
int query(int u)
{
if(!heavy[u]) return val[u];
return cq.query(sid[u]).mn_l.first;
}
}hld;
void up(int v, int x)
{
hld.update(root, v, p[root]);
root = v, p[v] = x, rev[x] = v;
hld.update(root, root, p[root]);
}
int when(int v)
{
int color = hld.query(v);
if(color == p[root])
return rq.sum(1, color) - dis(v, root);
return (color > 1 ? rq.sum(1, color - 1) : 0) + 1 + dis(v, rev[color]);
}
int32_t main()
{
fastio();
cin >> n >> q;
for(int i = 1, u, v; i < n; i ++) cin >> u >> v, adj[u].pb(v), adj[v].pb(u);
tin[0] = 0;
dep[n] = 1, dfs(n, 0);
tout[0] = ++ timer;
for(int i = 1; i <= n; i ++) p[i] = rev[i] = i;
root = n;
hld.init(n);
hld.build(n);
for(int i = 1, u, v; i <= q; i ++)
{
string s;
cin >> s;
if(s == "up")
{
cin >> v;
up(v, n + i);
}
else if(s == "when")
{
cin >> v;
cout << when(v) << endl;
}
else if(s == "compare")
{
cin >> v >> u;
cout << (when(v) < when(u) ? v : u) << endl;
}
}
}