#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;
}
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);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
61272 KB |
Output is correct |
2 |
Correct |
11 ms |
61276 KB |
Output is correct |
3 |
Correct |
9 ms |
61276 KB |
Output is correct |
4 |
Correct |
9 ms |
61168 KB |
Output is correct |
5 |
Correct |
10 ms |
61276 KB |
Output is correct |
6 |
Correct |
10 ms |
61168 KB |
Output is correct |
7 |
Correct |
10 ms |
61276 KB |
Output is correct |
8 |
Correct |
10 ms |
61276 KB |
Output is correct |
9 |
Correct |
11 ms |
61160 KB |
Output is correct |
10 |
Correct |
10 ms |
61276 KB |
Output is correct |
11 |
Correct |
11 ms |
61348 KB |
Output is correct |
12 |
Correct |
11 ms |
61276 KB |
Output is correct |
13 |
Correct |
10 ms |
61188 KB |
Output is correct |
14 |
Correct |
10 ms |
61344 KB |
Output is correct |
15 |
Correct |
11 ms |
61336 KB |
Output is correct |
16 |
Correct |
11 ms |
61276 KB |
Output is correct |
17 |
Correct |
10 ms |
61396 KB |
Output is correct |
18 |
Correct |
11 ms |
61276 KB |
Output is correct |
19 |
Correct |
10 ms |
61152 KB |
Output is correct |
20 |
Correct |
11 ms |
61276 KB |
Output is correct |
21 |
Correct |
10 ms |
61216 KB |
Output is correct |
22 |
Correct |
11 ms |
61276 KB |
Output is correct |
23 |
Correct |
10 ms |
61528 KB |
Output is correct |
24 |
Correct |
10 ms |
61276 KB |
Output is correct |
25 |
Correct |
10 ms |
61276 KB |
Output is correct |
26 |
Correct |
10 ms |
61152 KB |
Output is correct |
27 |
Correct |
10 ms |
61388 KB |
Output is correct |
28 |
Correct |
11 ms |
61276 KB |
Output is correct |
29 |
Correct |
10 ms |
61156 KB |
Output is correct |
30 |
Correct |
11 ms |
61272 KB |
Output is correct |
31 |
Correct |
11 ms |
61204 KB |
Output is correct |
32 |
Correct |
11 ms |
61396 KB |
Output is correct |
33 |
Correct |
11 ms |
61276 KB |
Output is correct |
34 |
Correct |
10 ms |
61276 KB |
Output is correct |
35 |
Correct |
11 ms |
61188 KB |
Output is correct |
36 |
Correct |
10 ms |
61276 KB |
Output is correct |
37 |
Correct |
11 ms |
61276 KB |
Output is correct |
38 |
Correct |
10 ms |
61276 KB |
Output is correct |
39 |
Correct |
11 ms |
61276 KB |
Output is correct |
40 |
Correct |
10 ms |
61476 KB |
Output is correct |
41 |
Runtime error |
339 ms |
524288 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
81228 KB |
Output is correct |
2 |
Correct |
349 ms |
80804 KB |
Output is correct |
3 |
Correct |
346 ms |
80976 KB |
Output is correct |
4 |
Correct |
365 ms |
80676 KB |
Output is correct |
5 |
Correct |
371 ms |
81500 KB |
Output is correct |
6 |
Correct |
340 ms |
81076 KB |
Output is correct |
7 |
Correct |
404 ms |
81268 KB |
Output is correct |
8 |
Correct |
390 ms |
81700 KB |
Output is correct |
9 |
Correct |
355 ms |
81056 KB |
Output is correct |
10 |
Correct |
363 ms |
81960 KB |
Output is correct |
11 |
Correct |
369 ms |
81232 KB |
Output is correct |
12 |
Correct |
374 ms |
81584 KB |
Output is correct |
13 |
Correct |
390 ms |
81656 KB |
Output is correct |
14 |
Runtime error |
294 ms |
524288 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
61272 KB |
Output is correct |
2 |
Correct |
11 ms |
61276 KB |
Output is correct |
3 |
Correct |
9 ms |
61276 KB |
Output is correct |
4 |
Correct |
9 ms |
61168 KB |
Output is correct |
5 |
Correct |
10 ms |
61276 KB |
Output is correct |
6 |
Correct |
10 ms |
61168 KB |
Output is correct |
7 |
Correct |
10 ms |
61276 KB |
Output is correct |
8 |
Correct |
10 ms |
61276 KB |
Output is correct |
9 |
Correct |
11 ms |
61160 KB |
Output is correct |
10 |
Correct |
10 ms |
61276 KB |
Output is correct |
11 |
Correct |
11 ms |
61348 KB |
Output is correct |
12 |
Correct |
11 ms |
61276 KB |
Output is correct |
13 |
Correct |
10 ms |
61188 KB |
Output is correct |
14 |
Correct |
10 ms |
61344 KB |
Output is correct |
15 |
Correct |
11 ms |
61336 KB |
Output is correct |
16 |
Correct |
11 ms |
61276 KB |
Output is correct |
17 |
Correct |
10 ms |
61396 KB |
Output is correct |
18 |
Correct |
11 ms |
61276 KB |
Output is correct |
19 |
Correct |
10 ms |
61152 KB |
Output is correct |
20 |
Correct |
11 ms |
61276 KB |
Output is correct |
21 |
Correct |
10 ms |
61216 KB |
Output is correct |
22 |
Correct |
11 ms |
61276 KB |
Output is correct |
23 |
Correct |
10 ms |
61528 KB |
Output is correct |
24 |
Correct |
10 ms |
61276 KB |
Output is correct |
25 |
Correct |
10 ms |
61276 KB |
Output is correct |
26 |
Correct |
10 ms |
61152 KB |
Output is correct |
27 |
Correct |
10 ms |
61388 KB |
Output is correct |
28 |
Correct |
11 ms |
61276 KB |
Output is correct |
29 |
Correct |
10 ms |
61156 KB |
Output is correct |
30 |
Correct |
11 ms |
61272 KB |
Output is correct |
31 |
Correct |
11 ms |
61204 KB |
Output is correct |
32 |
Correct |
11 ms |
61396 KB |
Output is correct |
33 |
Correct |
11 ms |
61276 KB |
Output is correct |
34 |
Correct |
10 ms |
61276 KB |
Output is correct |
35 |
Correct |
11 ms |
61188 KB |
Output is correct |
36 |
Correct |
10 ms |
61276 KB |
Output is correct |
37 |
Correct |
11 ms |
61276 KB |
Output is correct |
38 |
Correct |
10 ms |
61276 KB |
Output is correct |
39 |
Correct |
11 ms |
61276 KB |
Output is correct |
40 |
Correct |
10 ms |
61476 KB |
Output is correct |
41 |
Runtime error |
339 ms |
524288 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |