#include <bits/stdc++.h>
using namespace std;
const int N = 300000;
const int inf = (int) 1e9;
struct node {
int L, R, to;
long long cost;
node(int L = 0, int R = inf, int to = 0, long long cost = 0) : L(L), R(R), to(to), cost(cost) {}
} st[2][4 * N];
node merge(node l, node r) {
if (l.to != inf) {
return node(l.L, l.R, r.to != inf ? r.to : min(max(l.to, r.L), r.R), l.cost + r.cost + max(0, l.to - r.R));
}
if (l.R < r.L) {
return node(l.R, l.R, r.to != inf ? r.to : r.L, l.cost + r.cost);
}
if (l.L > r.R) {
return node(l.L, l.L, r.to != inf ? r.to : r.R, l.cost + r.cost + l.L - r.R);
}
return node(max(l.L, r.L), min(l.R, r.R), r.to != inf ? r.to : inf, l.cost + r.cost);
}
int n, q, from[2][N], to[2][N];
void Build(int t, int id, int l, int r) {
if (l == r) {
st[t][id] = node(from[t][l], to[t][l], inf, 0);
return;
}
int mid = (l + r) / 2;
Build(t, id << 1, l, mid);
Build(t, id << 1 | 1, mid + 1, r);
st[t][id] = merge(st[t][id << 1], st[t][id << 1 | 1]);
}
void Update(int t, int id, int l, int r, int p) {
if (l == r) {
st[t][id] = node(from[t][l], to[t][l], inf, 0);
return;
}
int mid = (l + r) / 2;
if (p <= mid) {
Update(t, id << 1, l, mid, p);
} else {
Update(t, id << 1 | 1, mid + 1, r, p);
}
st[t][id] = merge(st[t][id << 1], st[t][id << 1 | 1]);
}
node Query(int t, int id, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return st[t][id];
}
int mid = (l + r) / 2;
if (qr <= mid) {
return Query(t, id << 1, l, mid, ql, qr);
} else if (ql > mid) {
return Query(t, id << 1 | 1, mid + 1, r, ql, qr);
} else {
return merge(Query(t, id << 1, l, mid, ql, qr), Query(t, id << 1 | 1, mid + 1, r, ql, qr));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> from[0][i] >> to[0][i];
--to[0][i];
from[1][n - i] = from[0][i];
to[1][n - i] = to[0][i];
}
for (int i = 1; i < n; i++) {
from[0][i] -= i;
to[0][i] -= i;
from[1][i] -= i;
to[1][i] -= i;
}
if (n != 1) {
Build(0, 1, 1, n - 1);
Build(1, 1, 1, n - 1);
}
while (q--) {
int op;
cin >> op;
if (op == 1) {
int p, s, e;
cin >> p >> s >> e;
--e;
from[0][p] = s - p;
to[0][p] = e - p;
from[1][n - p] = s - (n - p);
to[1][n - p] = e - (n - p);
if (n != 1) {
Update(0, 1, 1, n - 1, p);
Update(1, 1, 1, n - 1, n - p);
}
} else {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a == c) {
cout << max(b - d, 0) << '\n';
continue;
}
int t = (a < c ? 0 : 1);
if (a > c) {
a = n - a + 1;
c = n - c + 1;
}
b -= a;
d -= c;
node L = node(b, b, b, 0);
node R = node(d, d, d, 0);
node seg = Query(t, 1, 1, n - 1, a, c - 1);
cout << merge(merge(L, seg), R).cost << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
61272 KB |
Output is correct |
2 |
Correct |
9 ms |
61276 KB |
Output is correct |
3 |
Correct |
10 ms |
61180 KB |
Output is correct |
4 |
Correct |
10 ms |
61276 KB |
Output is correct |
5 |
Correct |
10 ms |
61276 KB |
Output is correct |
6 |
Correct |
10 ms |
61272 KB |
Output is correct |
7 |
Correct |
10 ms |
61276 KB |
Output is correct |
8 |
Correct |
9 ms |
61276 KB |
Output is correct |
9 |
Correct |
10 ms |
61276 KB |
Output is correct |
10 |
Correct |
10 ms |
61276 KB |
Output is correct |
11 |
Correct |
10 ms |
61272 KB |
Output is correct |
12 |
Correct |
10 ms |
61168 KB |
Output is correct |
13 |
Correct |
11 ms |
61168 KB |
Output is correct |
14 |
Correct |
10 ms |
61276 KB |
Output is correct |
15 |
Correct |
10 ms |
61276 KB |
Output is correct |
16 |
Correct |
10 ms |
61276 KB |
Output is correct |
17 |
Correct |
10 ms |
61276 KB |
Output is correct |
18 |
Correct |
10 ms |
61352 KB |
Output is correct |
19 |
Correct |
10 ms |
61272 KB |
Output is correct |
20 |
Correct |
11 ms |
61276 KB |
Output is correct |
21 |
Correct |
10 ms |
61276 KB |
Output is correct |
22 |
Correct |
12 ms |
61348 KB |
Output is correct |
23 |
Correct |
10 ms |
61276 KB |
Output is correct |
24 |
Correct |
10 ms |
61276 KB |
Output is correct |
25 |
Correct |
11 ms |
61276 KB |
Output is correct |
26 |
Correct |
10 ms |
61352 KB |
Output is correct |
27 |
Correct |
10 ms |
61220 KB |
Output is correct |
28 |
Correct |
10 ms |
61276 KB |
Output is correct |
29 |
Correct |
10 ms |
61392 KB |
Output is correct |
30 |
Correct |
10 ms |
61368 KB |
Output is correct |
31 |
Correct |
10 ms |
61276 KB |
Output is correct |
32 |
Correct |
10 ms |
61276 KB |
Output is correct |
33 |
Correct |
10 ms |
61272 KB |
Output is correct |
34 |
Correct |
11 ms |
61388 KB |
Output is correct |
35 |
Correct |
10 ms |
61348 KB |
Output is correct |
36 |
Correct |
11 ms |
61276 KB |
Output is correct |
37 |
Correct |
10 ms |
61276 KB |
Output is correct |
38 |
Correct |
11 ms |
61276 KB |
Output is correct |
39 |
Correct |
10 ms |
61276 KB |
Output is correct |
40 |
Correct |
10 ms |
61276 KB |
Output is correct |
41 |
Correct |
9 ms |
57176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
73648 KB |
Output is correct |
2 |
Correct |
341 ms |
73556 KB |
Output is correct |
3 |
Correct |
351 ms |
73512 KB |
Output is correct |
4 |
Correct |
350 ms |
73816 KB |
Output is correct |
5 |
Correct |
361 ms |
73552 KB |
Output is correct |
6 |
Correct |
349 ms |
73604 KB |
Output is correct |
7 |
Correct |
351 ms |
73524 KB |
Output is correct |
8 |
Correct |
373 ms |
74024 KB |
Output is correct |
9 |
Correct |
341 ms |
73704 KB |
Output is correct |
10 |
Correct |
353 ms |
73500 KB |
Output is correct |
11 |
Correct |
351 ms |
73620 KB |
Output is correct |
12 |
Correct |
353 ms |
73812 KB |
Output is correct |
13 |
Correct |
368 ms |
74000 KB |
Output is correct |
14 |
Correct |
9 ms |
57432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
61272 KB |
Output is correct |
2 |
Correct |
9 ms |
61276 KB |
Output is correct |
3 |
Correct |
10 ms |
61180 KB |
Output is correct |
4 |
Correct |
10 ms |
61276 KB |
Output is correct |
5 |
Correct |
10 ms |
61276 KB |
Output is correct |
6 |
Correct |
10 ms |
61272 KB |
Output is correct |
7 |
Correct |
10 ms |
61276 KB |
Output is correct |
8 |
Correct |
9 ms |
61276 KB |
Output is correct |
9 |
Correct |
10 ms |
61276 KB |
Output is correct |
10 |
Correct |
10 ms |
61276 KB |
Output is correct |
11 |
Correct |
10 ms |
61272 KB |
Output is correct |
12 |
Correct |
10 ms |
61168 KB |
Output is correct |
13 |
Correct |
11 ms |
61168 KB |
Output is correct |
14 |
Correct |
10 ms |
61276 KB |
Output is correct |
15 |
Correct |
10 ms |
61276 KB |
Output is correct |
16 |
Correct |
10 ms |
61276 KB |
Output is correct |
17 |
Correct |
10 ms |
61276 KB |
Output is correct |
18 |
Correct |
10 ms |
61352 KB |
Output is correct |
19 |
Correct |
10 ms |
61272 KB |
Output is correct |
20 |
Correct |
11 ms |
61276 KB |
Output is correct |
21 |
Correct |
10 ms |
61276 KB |
Output is correct |
22 |
Correct |
12 ms |
61348 KB |
Output is correct |
23 |
Correct |
10 ms |
61276 KB |
Output is correct |
24 |
Correct |
10 ms |
61276 KB |
Output is correct |
25 |
Correct |
11 ms |
61276 KB |
Output is correct |
26 |
Correct |
10 ms |
61352 KB |
Output is correct |
27 |
Correct |
10 ms |
61220 KB |
Output is correct |
28 |
Correct |
10 ms |
61276 KB |
Output is correct |
29 |
Correct |
10 ms |
61392 KB |
Output is correct |
30 |
Correct |
10 ms |
61368 KB |
Output is correct |
31 |
Correct |
10 ms |
61276 KB |
Output is correct |
32 |
Correct |
10 ms |
61276 KB |
Output is correct |
33 |
Correct |
10 ms |
61272 KB |
Output is correct |
34 |
Correct |
11 ms |
61388 KB |
Output is correct |
35 |
Correct |
10 ms |
61348 KB |
Output is correct |
36 |
Correct |
11 ms |
61276 KB |
Output is correct |
37 |
Correct |
10 ms |
61276 KB |
Output is correct |
38 |
Correct |
11 ms |
61276 KB |
Output is correct |
39 |
Correct |
10 ms |
61276 KB |
Output is correct |
40 |
Correct |
10 ms |
61276 KB |
Output is correct |
41 |
Correct |
9 ms |
57176 KB |
Output is correct |
42 |
Correct |
361 ms |
73648 KB |
Output is correct |
43 |
Correct |
341 ms |
73556 KB |
Output is correct |
44 |
Correct |
351 ms |
73512 KB |
Output is correct |
45 |
Correct |
350 ms |
73816 KB |
Output is correct |
46 |
Correct |
361 ms |
73552 KB |
Output is correct |
47 |
Correct |
349 ms |
73604 KB |
Output is correct |
48 |
Correct |
351 ms |
73524 KB |
Output is correct |
49 |
Correct |
373 ms |
74024 KB |
Output is correct |
50 |
Correct |
341 ms |
73704 KB |
Output is correct |
51 |
Correct |
353 ms |
73500 KB |
Output is correct |
52 |
Correct |
351 ms |
73620 KB |
Output is correct |
53 |
Correct |
353 ms |
73812 KB |
Output is correct |
54 |
Correct |
368 ms |
74000 KB |
Output is correct |
55 |
Correct |
9 ms |
57432 KB |
Output is correct |
56 |
Correct |
399 ms |
78116 KB |
Output is correct |
57 |
Correct |
385 ms |
77904 KB |
Output is correct |
58 |
Correct |
402 ms |
78340 KB |
Output is correct |
59 |
Correct |
401 ms |
78168 KB |
Output is correct |
60 |
Correct |
380 ms |
77648 KB |
Output is correct |
61 |
Correct |
423 ms |
78476 KB |
Output is correct |
62 |
Correct |
401 ms |
78416 KB |
Output is correct |
63 |
Correct |
426 ms |
78564 KB |
Output is correct |
64 |
Correct |
403 ms |
78416 KB |
Output is correct |
65 |
Correct |
398 ms |
78116 KB |
Output is correct |
66 |
Correct |
406 ms |
78316 KB |
Output is correct |
67 |
Correct |
409 ms |
78300 KB |
Output is correct |
68 |
Correct |
394 ms |
77740 KB |
Output is correct |
69 |
Correct |
427 ms |
78424 KB |
Output is correct |
70 |
Correct |
398 ms |
77660 KB |
Output is correct |
71 |
Correct |
387 ms |
77476 KB |
Output is correct |
72 |
Correct |
382 ms |
77260 KB |
Output is correct |
73 |
Correct |
423 ms |
77848 KB |
Output is correct |
74 |
Correct |
402 ms |
77908 KB |
Output is correct |
75 |
Correct |
402 ms |
77956 KB |
Output is correct |
76 |
Correct |
409 ms |
78672 KB |
Output is correct |
77 |
Correct |
9 ms |
57180 KB |
Output is correct |