#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
const int MAXN = 300005;
const llint INF = 1000000000000000000LL;
llint n, q, ofs = 1;
llint lef[MAXN], rig[MAXN];
llint tip[MAXN], A[MAXN], B[MAXN], C[MAXN], D[MAXN], sol[MAXN];
struct node {
bool konst;
llint lo, hi, c;
llint w, h;
node () {}
} t[MAXN * 4], e;
void ispis (node a) {
cout << "node" << endl;
cout << "konst " << a.konst << " ";
if (a.konst) cout << a.c; else cout << a.lo << " " << a.hi;
cout << endl;
cout << a.w << " " << a.h << endl;
}
llint f (node a, llint x) {
if (a.konst) return a.c;
return max(a.lo, min(a.hi, x));
}
llint cost (node a, llint x) {
return max(x - a.w, 0LL) + a.h;
}
node spoji (node a, node b) {
if (b.c == -INF) return a;
if (a.c == -INF) return b;
node res;
if (a.konst) {
res.konst = 1;
res.c = f(b, a.c);
res.w = a.w;
res.h = a.h + cost(b, a.c);
} else {
if (b.konst) {
res.konst = 1;
res.c = b.c;
} else if (a.hi < b.lo || b.hi < a.lo) {
res.konst = 1;
if (a.hi < b.lo) res.c = b.lo; else res.c = b.hi;
} else {
res.konst = 0;
res.lo = max(a.lo, b.lo);
res.hi = min(a.hi, b.hi);
}
res.h = b.h;
if (a.lo <= b.w && b.w <= a.hi) {
res.w = b.w;
} else if (a.hi < b.w) {
res.w = a.hi;
} else {
res.w = a.lo;
res.h = cost(b, a.lo);
}
}
return res;
}
void tour_init () {
e.c = -INF;
for (int i = 0; i < n-1; i++) {
t[i + ofs].konst = 0;
t[i + ofs].lo = lef[i]; t[i + ofs].hi = rig[i];
t[i + ofs].h = 0;
t[i + ofs].w = rig[i];
}
for (int i = ofs - 1; i > 0; i--) {
t[i] = spoji(t[2 * i], t[2 * i + 1]);
}
}
void update (int pos, int lo, int hi) {
//cout << "update " << pos << " " << lo << " " << hi << endl;
pos += ofs;
t[pos].lo = lo; t[pos].hi = hi; t[pos].w = hi;
pos /= 2;
while (pos > 0) {
t[pos] = spoji(t[pos * 2], t[pos * 2 + 1]);
pos /= 2;
}
}
node upit (int x, int from, int to, int lo, int hi) {
if (from <= lo && hi <= to) return t[x];
if (to < lo || hi < from) return e;
return spoji(upit(2 * x, from, to, lo, (lo + hi) / 2), upit(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi));
}
void okreni () {
reverse(lef, lef + n - 1);
reverse(rig, rig + n - 1);
for (int i = 0; i < q; i++) {
if (tip[i] == 1) {
A[i] = n - 2 - A[i];
} else {
A[i] = n - 1 - A[i];
C[i] = n - 1 - C[i];
}
}
}
void solve_lef_to_rig () {
for (int i = 0; i < n - 1; i++) lef[i] -= i, rig[i] -= i;
tour_init();
for (int i = 0; i < q; i++) {
if (tip[i] == 1) {
B[i] -= A[i]; C[i] -= A[i];
update(A[i], B[i], C[i] - 1);
B[i] += A[i]; C[i] += A[i];
} else if (A[i] < C[i]) {
B[i] -= A[i]; D[i] -= C[i];
//cout << "upit " << A[i] << " " << C[i] - 1 << endl;
node res = upit(1, A[i], C[i] - 1, 0, ofs - 1);
//ispis(res);
llint val = cost(res, B[i]) + max(f(res, B[i]) - D[i], 0LL);
sol[i] = val;
B[i] += A[i]; D[i] += C[i];
}
}
for (int i = 0; i < n - 1; i++) lef[i] += i, rig[i] += i;
}
void solve_const () {
for (int i = 0; i < q; i++) {
if (tip[i] == 2 && A[i] == C[i]) {
sol[i] = max(B[i] - D[i], 0LL);
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
while (ofs < n) ofs *= 2;
for (int i = 0; i < n - 1; i++) {
cin >> lef[i] >> rig[i];
rig[i]--;
}
for (int i = 0; i < q; i++) {
cin >> tip[i];
if (tip[i] == 1) {
cin >> A[i] >> B[i] >> C[i];
A[i]--;
} else {
cin >> A[i] >> B[i] >> C[i] >> D[i];
A[i]--; C[i]--;
}
}
solve_lef_to_rig();
okreni();
solve_lef_to_rig();
solve_const();
for (int i = 0; i < q; i++) {
if (tip[i] == 2) cout << sol[i] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
4 ms |
768 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
668 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
640 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
640 KB |
Output is correct |
31 |
Correct |
3 ms |
640 KB |
Output is correct |
32 |
Correct |
3 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
640 KB |
Output is correct |
37 |
Correct |
3 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
640 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1576 ms |
76220 KB |
Output is correct |
2 |
Correct |
1544 ms |
62512 KB |
Output is correct |
3 |
Correct |
1487 ms |
62744 KB |
Output is correct |
4 |
Correct |
1463 ms |
62328 KB |
Output is correct |
5 |
Correct |
1543 ms |
76156 KB |
Output is correct |
6 |
Correct |
1504 ms |
75768 KB |
Output is correct |
7 |
Correct |
1582 ms |
76536 KB |
Output is correct |
8 |
Correct |
1564 ms |
77816 KB |
Output is correct |
9 |
Correct |
1509 ms |
62588 KB |
Output is correct |
10 |
Correct |
1539 ms |
76332 KB |
Output is correct |
11 |
Correct |
1530 ms |
76280 KB |
Output is correct |
12 |
Correct |
1534 ms |
77816 KB |
Output is correct |
13 |
Correct |
1529 ms |
78344 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
4 ms |
768 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
668 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
640 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
3 ms |
640 KB |
Output is correct |
31 |
Correct |
3 ms |
640 KB |
Output is correct |
32 |
Correct |
3 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
640 KB |
Output is correct |
37 |
Correct |
3 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
640 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1576 ms |
76220 KB |
Output is correct |
43 |
Correct |
1544 ms |
62512 KB |
Output is correct |
44 |
Correct |
1487 ms |
62744 KB |
Output is correct |
45 |
Correct |
1463 ms |
62328 KB |
Output is correct |
46 |
Correct |
1543 ms |
76156 KB |
Output is correct |
47 |
Correct |
1504 ms |
75768 KB |
Output is correct |
48 |
Correct |
1582 ms |
76536 KB |
Output is correct |
49 |
Correct |
1564 ms |
77816 KB |
Output is correct |
50 |
Correct |
1509 ms |
62588 KB |
Output is correct |
51 |
Correct |
1539 ms |
76332 KB |
Output is correct |
52 |
Correct |
1530 ms |
76280 KB |
Output is correct |
53 |
Correct |
1534 ms |
77816 KB |
Output is correct |
54 |
Correct |
1529 ms |
78344 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1350 ms |
72836 KB |
Output is correct |
57 |
Correct |
1266 ms |
59048 KB |
Output is correct |
58 |
Correct |
1253 ms |
73632 KB |
Output is correct |
59 |
Correct |
1246 ms |
73720 KB |
Output is correct |
60 |
Correct |
1183 ms |
59640 KB |
Output is correct |
61 |
Correct |
1246 ms |
74396 KB |
Output is correct |
62 |
Correct |
1283 ms |
74200 KB |
Output is correct |
63 |
Correct |
1296 ms |
74104 KB |
Output is correct |
64 |
Correct |
1270 ms |
74488 KB |
Output is correct |
65 |
Correct |
1256 ms |
73172 KB |
Output is correct |
66 |
Correct |
1259 ms |
73304 KB |
Output is correct |
67 |
Correct |
1309 ms |
74244 KB |
Output is correct |
68 |
Correct |
1208 ms |
71948 KB |
Output is correct |
69 |
Correct |
1280 ms |
75080 KB |
Output is correct |
70 |
Correct |
1200 ms |
59832 KB |
Output is correct |
71 |
Correct |
1222 ms |
58292 KB |
Output is correct |
72 |
Correct |
1309 ms |
71780 KB |
Output is correct |
73 |
Correct |
1307 ms |
74040 KB |
Output is correct |
74 |
Correct |
1267 ms |
74268 KB |
Output is correct |
75 |
Correct |
1288 ms |
74488 KB |
Output is correct |
76 |
Correct |
1272 ms |
75256 KB |
Output is correct |
77 |
Correct |
1 ms |
384 KB |
Output is correct |