#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
int N, Q;
struct info {
// z = -1 => intersection between all intervals is positive, stored in [x, y]
// z >= 0 => z stores cost, x is maximum starting point, y is minimum ending point (under minimum cost)
int x, y;
i64 z;
void pr() const {
cout << "{x=" << x << ", y=" << y << ", z=" << z << "}";
}
};
info operator+(const info one, const info two) {
if (~one.z && ~two.z) return {one.x, two.y, one.z + two.z + max(0, one.y - two.x)};
if (~one.z) return {one.x, one.y < two.x ? two.x : min(one.y, two.y), one.z + max(0, one.y - two.y)};
if (~two.z) return {two.x > one.y ? one.y : max(one.x, two.x), two.y, two.z + max(0, one.x - two.x)};
// cout << "\t merging "; one.pr(); cout << " and "; two.pr(); cout << '\n';
// cout << "last case" << endl;
if (max(one.x, two.x) <= min(one.y, two.y)) return {max(one.x, two.x), min(one.y, two.y), -1};
return one.y < two.x ? info{one.y, two.x, 0} : info{one.x, two.y, one.x - two.y};
}
struct segt {
int l, r;
segt *lc = NULL, *rc = NULL;
info x;
segt(int l, int r) : l(l), r(r) {
if (l == r) x = info{-1, -1, -1};
else {
int m = (l + r) / 2;
lc = new segt(l, m), rc = new segt(m + 1, r);
x = lc->x + rc->x;
}
}
void upd(int i, int ql, int qr) {
if (l == r) x = {ql - l, qr - l - 1, -1};
else {
i <= lc->r ? lc->upd(i, ql, qr) : rc->upd(i, ql, qr);
x = lc->x + rc->x;
}
// cout << "GOT " << l << ' ' << r << ": ";
// x.pr();
// cout << '\n';
}
info qry(int ql, int qr) {
if (ql <= l && qr >= r) return x;
if (qr <= lc->r) return lc->qry(ql, qr);
if (ql >= rc->l) return rc->qry(ql, qr);
return lc->qry(ql, qr) + rc->qry(ql, qr);
}
} *tree, *tree_rev;
void upd(int i, int l, int r) {
tree->upd(i, l, r);
tree_rev->upd(N - i - 1, l, r);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> Q, N--;
if (!N) {
while (Q--) {
int t;
cin >> t;
assert(t == 2);
int i, t1, j, t2;
cin >> i >> t1 >> j >> t2, i--, j--;
assert(i == j);
cout << max(0, t1 - t2) << '\n';
}
return 0;
}
tree = new segt(0, N - 1);
tree_rev = new segt(0, N - 1);
for (int i = 0; i < N; i++) {
int l, r;
cin >> l >> r;
upd(i, l, r);
}
while (Q--) {
int t;
cin >> t;
if (t == 1) {
int i, l, r;
cin >> i >> l >> r, i--;
upd(i, l, r);
} else {
int i, t1, j, t2;
cin >> i >> t1 >> j >> t2, i--, j--;
if (i == j)
cout << max(0, t1 - t2) << '\n';
else if (i < j) {
info one{t1 - i, t1 - i, -1}, two{t2 - j, t2 - j, -1};
// info mid{tree->qry(i, j - 1)};
// cout << "one: "; one.pr(); cout << '\n';
// cout << "two: "; two.pr(); cout << '\n';
// cout << "mid: "; mid.pr(); cout << '\n';
// cout << "one+mid: "; (one+mid).pr(); cout << '\n';
cout << max(i64(0), (one + tree->qry(i, j - 1) + two).z) << '\n';
} else {
i = N - i;
j = N - j;
info one{t1 - i, t1 - i, -1}, two{t2 - j, t2 - j, -1};
cout << max(i64(0), (one + tree_rev->qry(i, j - 1) + two).z) << '\n';
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
608 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
680 ms |
64516 KB |
Output is correct |
2 |
Correct |
650 ms |
61052 KB |
Output is correct |
3 |
Correct |
649 ms |
61644 KB |
Output is correct |
4 |
Correct |
645 ms |
60044 KB |
Output is correct |
5 |
Correct |
650 ms |
63976 KB |
Output is correct |
6 |
Correct |
638 ms |
62884 KB |
Output is correct |
7 |
Correct |
660 ms |
64848 KB |
Output is correct |
8 |
Correct |
673 ms |
67428 KB |
Output is correct |
9 |
Correct |
663 ms |
60468 KB |
Output is correct |
10 |
Correct |
684 ms |
64448 KB |
Output is correct |
11 |
Correct |
648 ms |
64008 KB |
Output is correct |
12 |
Correct |
695 ms |
68164 KB |
Output is correct |
13 |
Correct |
671 ms |
69096 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
608 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
680 ms |
64516 KB |
Output is correct |
43 |
Correct |
650 ms |
61052 KB |
Output is correct |
44 |
Correct |
649 ms |
61644 KB |
Output is correct |
45 |
Correct |
645 ms |
60044 KB |
Output is correct |
46 |
Correct |
650 ms |
63976 KB |
Output is correct |
47 |
Correct |
638 ms |
62884 KB |
Output is correct |
48 |
Correct |
660 ms |
64848 KB |
Output is correct |
49 |
Correct |
673 ms |
67428 KB |
Output is correct |
50 |
Correct |
663 ms |
60468 KB |
Output is correct |
51 |
Correct |
684 ms |
64448 KB |
Output is correct |
52 |
Correct |
648 ms |
64008 KB |
Output is correct |
53 |
Correct |
695 ms |
68164 KB |
Output is correct |
54 |
Correct |
671 ms |
69096 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
638 ms |
68012 KB |
Output is correct |
57 |
Correct |
617 ms |
63932 KB |
Output is correct |
58 |
Correct |
650 ms |
69580 KB |
Output is correct |
59 |
Correct |
648 ms |
70140 KB |
Output is correct |
60 |
Correct |
612 ms |
65612 KB |
Output is correct |
61 |
Correct |
685 ms |
71932 KB |
Output is correct |
62 |
Correct |
701 ms |
71732 KB |
Output is correct |
63 |
Correct |
657 ms |
71472 KB |
Output is correct |
64 |
Correct |
686 ms |
72052 KB |
Output is correct |
65 |
Correct |
674 ms |
68792 KB |
Output is correct |
66 |
Correct |
671 ms |
69160 KB |
Output is correct |
67 |
Correct |
641 ms |
71700 KB |
Output is correct |
68 |
Correct |
609 ms |
66116 KB |
Output is correct |
69 |
Correct |
668 ms |
73424 KB |
Output is correct |
70 |
Correct |
638 ms |
66168 KB |
Output is correct |
71 |
Correct |
613 ms |
63568 KB |
Output is correct |
72 |
Correct |
639 ms |
65908 KB |
Output is correct |
73 |
Correct |
694 ms |
71796 KB |
Output is correct |
74 |
Correct |
657 ms |
72020 KB |
Output is correct |
75 |
Correct |
659 ms |
73116 KB |
Output is correct |
76 |
Correct |
645 ms |
73808 KB |
Output is correct |
77 |
Correct |
0 ms |
344 KB |
Output is correct |