#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--;
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 |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
688 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
668 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
664 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
668 KB |
Output is correct |
38 |
Correct |
2 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Runtime error |
426 ms |
524288 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
666 ms |
70856 KB |
Output is correct |
2 |
Correct |
597 ms |
68384 KB |
Output is correct |
3 |
Correct |
649 ms |
69200 KB |
Output is correct |
4 |
Correct |
659 ms |
67160 KB |
Output is correct |
5 |
Correct |
621 ms |
71508 KB |
Output is correct |
6 |
Correct |
613 ms |
70312 KB |
Output is correct |
7 |
Correct |
657 ms |
72328 KB |
Output is correct |
8 |
Correct |
657 ms |
75328 KB |
Output is correct |
9 |
Correct |
632 ms |
67868 KB |
Output is correct |
10 |
Correct |
709 ms |
72140 KB |
Output is correct |
11 |
Correct |
637 ms |
71616 KB |
Output is correct |
12 |
Correct |
655 ms |
76128 KB |
Output is correct |
13 |
Correct |
671 ms |
77140 KB |
Output is correct |
14 |
Runtime error |
376 ms |
524288 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
688 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
668 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
664 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
668 KB |
Output is correct |
38 |
Correct |
2 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Runtime error |
426 ms |
524288 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |