// [+]
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5 + 5;
int n, q;
struct Node {
int a, b;
bool t;
ll val;
Node() { val = -1; }
Node(int l, int r):
a(l), b(r), t(true), val(0) {}
Node(int a, int b, bool t, ll val):
a(a), b(b), t(t), val(val) {}
};
Node merge(const Node &x, const Node &y) {
if (x.t) {
if (y.t) {
// (1, 1)
if (x.b < y.a) {
return Node(x.b, y.a, 0, 0);
} else if (y.b < x.a) {
return Node(x.a, y.b, 0, x.a - y.b);
} else {
return Node(max(x.a, y.a), min(x.b, y.b), 1, 0);
}
} else {
return Node(max(x.a, min(x.b, y.a)), y.b, 0, y.val + max(0, x.a - y.a)); // (1, 0)
}
} else {
if (y.t) {
return Node(x.a, max(y.a, min(x.b, y.b)), 0, x.val + max(0, x.b - y.b)); // (0, 1)
} else {
return Node(x.a, y.b, 0, x.val + y.val + max(0, x.b - y.a)); // (0, 0)
}
}
}
struct SegmentTree {
Node IT[N << 2];
SegmentTree() {}
void update(int x, int L, int R, int id, int l, int r) {
if (l == r) return IT[id] = Node(L, R), void();
int mid = (l + r) / 2;
if (x <= mid) update(x, L, R, id << 1, l, mid);
else update(x, L, R, id << 1 | 1, mid + 1, r);
IT[id] = merge(IT[id << 1], IT[id << 1 | 1]);
}
inline void update(int x, int l, int r) {
update(x, l - x, r - x - 1, 1, 1, n - 1);
}
Node get(int x, int y, int id, int l, int r) {
if (l > y || r < x) return Node();
if (x <= l && r <= y) return IT[id];
int mid = (l + r) / 2;
auto L = get(x, y, id << 1, l, mid);
auto R = get(x, y, id << 1 | 1, mid + 1, r);
if (L.val < 0) return R;
if (R.val < 0) return L;
return merge(L, R);
}
inline Node get(int x, int y) {
return get(x, y, 1, 1, n - 1);
}
} seg, revseg;
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i < n; i++) {
int l, r; cin >> l >> r;
seg.update(i, l, r);
revseg.update(n - i, l, r);
}
while (q--) {
int T; cin >> T;
if (T == 1) {
int pos, l, r;
cin >> pos >> l >> r;
seg.update(pos, l, r);
revseg.update(n - pos, l, r);
} else {
int A, t_A, B, t_B;
cin >> A >> t_A >> B >> t_B;
if (A == B) cout << max(0, t_A - t_B);
else {
if (A < B) {
cout << merge(Node(t_A - A, t_A - A),
merge(seg.get(A, B - 1),
Node((int)-1e9, t_B - B))).val;
} else {
cout << merge(Node(t_A - (n - A + 1), t_A - (n - A + 1)),
merge(revseg.get(n - A + 1, n - B),
Node((int)-1e9, t_B - (n - B + 1)))).val;
}
}
cout << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
56664 KB |
Output is correct |
2 |
Correct |
7 ms |
56796 KB |
Output is correct |
3 |
Correct |
7 ms |
56768 KB |
Output is correct |
4 |
Correct |
7 ms |
56664 KB |
Output is correct |
5 |
Correct |
7 ms |
56744 KB |
Output is correct |
6 |
Correct |
7 ms |
56668 KB |
Output is correct |
7 |
Correct |
7 ms |
56688 KB |
Output is correct |
8 |
Correct |
7 ms |
56776 KB |
Output is correct |
9 |
Correct |
7 ms |
56668 KB |
Output is correct |
10 |
Correct |
7 ms |
56748 KB |
Output is correct |
11 |
Correct |
8 ms |
56668 KB |
Output is correct |
12 |
Correct |
8 ms |
56796 KB |
Output is correct |
13 |
Correct |
10 ms |
56668 KB |
Output is correct |
14 |
Correct |
8 ms |
56668 KB |
Output is correct |
15 |
Correct |
8 ms |
56784 KB |
Output is correct |
16 |
Correct |
8 ms |
56752 KB |
Output is correct |
17 |
Correct |
10 ms |
56664 KB |
Output is correct |
18 |
Correct |
8 ms |
56920 KB |
Output is correct |
19 |
Correct |
8 ms |
56668 KB |
Output is correct |
20 |
Correct |
8 ms |
56668 KB |
Output is correct |
21 |
Correct |
10 ms |
56760 KB |
Output is correct |
22 |
Correct |
8 ms |
56668 KB |
Output is correct |
23 |
Correct |
8 ms |
56768 KB |
Output is correct |
24 |
Correct |
8 ms |
56668 KB |
Output is correct |
25 |
Correct |
9 ms |
56668 KB |
Output is correct |
26 |
Correct |
8 ms |
56920 KB |
Output is correct |
27 |
Correct |
9 ms |
56668 KB |
Output is correct |
28 |
Correct |
8 ms |
56668 KB |
Output is correct |
29 |
Correct |
8 ms |
56764 KB |
Output is correct |
30 |
Correct |
8 ms |
56668 KB |
Output is correct |
31 |
Correct |
8 ms |
56772 KB |
Output is correct |
32 |
Correct |
8 ms |
56668 KB |
Output is correct |
33 |
Correct |
8 ms |
56668 KB |
Output is correct |
34 |
Correct |
9 ms |
56668 KB |
Output is correct |
35 |
Correct |
8 ms |
56668 KB |
Output is correct |
36 |
Correct |
8 ms |
56668 KB |
Output is correct |
37 |
Correct |
8 ms |
56668 KB |
Output is correct |
38 |
Correct |
8 ms |
56668 KB |
Output is correct |
39 |
Correct |
9 ms |
56668 KB |
Output is correct |
40 |
Correct |
8 ms |
56668 KB |
Output is correct |
41 |
Correct |
7 ms |
56668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
493 ms |
60940 KB |
Output is correct |
2 |
Correct |
476 ms |
61032 KB |
Output is correct |
3 |
Correct |
462 ms |
61012 KB |
Output is correct |
4 |
Correct |
484 ms |
61012 KB |
Output is correct |
5 |
Correct |
480 ms |
60832 KB |
Output is correct |
6 |
Correct |
479 ms |
61012 KB |
Output is correct |
7 |
Correct |
508 ms |
61012 KB |
Output is correct |
8 |
Correct |
481 ms |
61160 KB |
Output is correct |
9 |
Correct |
470 ms |
61076 KB |
Output is correct |
10 |
Correct |
493 ms |
61148 KB |
Output is correct |
11 |
Correct |
493 ms |
60904 KB |
Output is correct |
12 |
Correct |
532 ms |
61068 KB |
Output is correct |
13 |
Correct |
549 ms |
61008 KB |
Output is correct |
14 |
Correct |
8 ms |
56664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
56664 KB |
Output is correct |
2 |
Correct |
7 ms |
56796 KB |
Output is correct |
3 |
Correct |
7 ms |
56768 KB |
Output is correct |
4 |
Correct |
7 ms |
56664 KB |
Output is correct |
5 |
Correct |
7 ms |
56744 KB |
Output is correct |
6 |
Correct |
7 ms |
56668 KB |
Output is correct |
7 |
Correct |
7 ms |
56688 KB |
Output is correct |
8 |
Correct |
7 ms |
56776 KB |
Output is correct |
9 |
Correct |
7 ms |
56668 KB |
Output is correct |
10 |
Correct |
7 ms |
56748 KB |
Output is correct |
11 |
Correct |
8 ms |
56668 KB |
Output is correct |
12 |
Correct |
8 ms |
56796 KB |
Output is correct |
13 |
Correct |
10 ms |
56668 KB |
Output is correct |
14 |
Correct |
8 ms |
56668 KB |
Output is correct |
15 |
Correct |
8 ms |
56784 KB |
Output is correct |
16 |
Correct |
8 ms |
56752 KB |
Output is correct |
17 |
Correct |
10 ms |
56664 KB |
Output is correct |
18 |
Correct |
8 ms |
56920 KB |
Output is correct |
19 |
Correct |
8 ms |
56668 KB |
Output is correct |
20 |
Correct |
8 ms |
56668 KB |
Output is correct |
21 |
Correct |
10 ms |
56760 KB |
Output is correct |
22 |
Correct |
8 ms |
56668 KB |
Output is correct |
23 |
Correct |
8 ms |
56768 KB |
Output is correct |
24 |
Correct |
8 ms |
56668 KB |
Output is correct |
25 |
Correct |
9 ms |
56668 KB |
Output is correct |
26 |
Correct |
8 ms |
56920 KB |
Output is correct |
27 |
Correct |
9 ms |
56668 KB |
Output is correct |
28 |
Correct |
8 ms |
56668 KB |
Output is correct |
29 |
Correct |
8 ms |
56764 KB |
Output is correct |
30 |
Correct |
8 ms |
56668 KB |
Output is correct |
31 |
Correct |
8 ms |
56772 KB |
Output is correct |
32 |
Correct |
8 ms |
56668 KB |
Output is correct |
33 |
Correct |
8 ms |
56668 KB |
Output is correct |
34 |
Correct |
9 ms |
56668 KB |
Output is correct |
35 |
Correct |
8 ms |
56668 KB |
Output is correct |
36 |
Correct |
8 ms |
56668 KB |
Output is correct |
37 |
Correct |
8 ms |
56668 KB |
Output is correct |
38 |
Correct |
8 ms |
56668 KB |
Output is correct |
39 |
Correct |
9 ms |
56668 KB |
Output is correct |
40 |
Correct |
8 ms |
56668 KB |
Output is correct |
41 |
Correct |
7 ms |
56668 KB |
Output is correct |
42 |
Correct |
493 ms |
60940 KB |
Output is correct |
43 |
Correct |
476 ms |
61032 KB |
Output is correct |
44 |
Correct |
462 ms |
61012 KB |
Output is correct |
45 |
Correct |
484 ms |
61012 KB |
Output is correct |
46 |
Correct |
480 ms |
60832 KB |
Output is correct |
47 |
Correct |
479 ms |
61012 KB |
Output is correct |
48 |
Correct |
508 ms |
61012 KB |
Output is correct |
49 |
Correct |
481 ms |
61160 KB |
Output is correct |
50 |
Correct |
470 ms |
61076 KB |
Output is correct |
51 |
Correct |
493 ms |
61148 KB |
Output is correct |
52 |
Correct |
493 ms |
60904 KB |
Output is correct |
53 |
Correct |
532 ms |
61068 KB |
Output is correct |
54 |
Correct |
549 ms |
61008 KB |
Output is correct |
55 |
Correct |
8 ms |
56664 KB |
Output is correct |
56 |
Correct |
516 ms |
58836 KB |
Output is correct |
57 |
Correct |
454 ms |
58792 KB |
Output is correct |
58 |
Correct |
513 ms |
58960 KB |
Output is correct |
59 |
Correct |
500 ms |
59096 KB |
Output is correct |
60 |
Correct |
460 ms |
58960 KB |
Output is correct |
61 |
Correct |
512 ms |
58692 KB |
Output is correct |
62 |
Correct |
548 ms |
58908 KB |
Output is correct |
63 |
Correct |
491 ms |
58868 KB |
Output is correct |
64 |
Correct |
493 ms |
58704 KB |
Output is correct |
65 |
Correct |
501 ms |
58840 KB |
Output is correct |
66 |
Correct |
520 ms |
58932 KB |
Output is correct |
67 |
Correct |
497 ms |
58848 KB |
Output is correct |
68 |
Correct |
469 ms |
58840 KB |
Output is correct |
69 |
Correct |
529 ms |
59120 KB |
Output is correct |
70 |
Correct |
469 ms |
58860 KB |
Output is correct |
71 |
Correct |
456 ms |
58704 KB |
Output is correct |
72 |
Correct |
479 ms |
58972 KB |
Output is correct |
73 |
Correct |
515 ms |
58680 KB |
Output is correct |
74 |
Correct |
502 ms |
58844 KB |
Output is correct |
75 |
Correct |
556 ms |
59084 KB |
Output is correct |
76 |
Correct |
506 ms |
58708 KB |
Output is correct |
77 |
Correct |
8 ms |
56668 KB |
Output is correct |