// [+]
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
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) {/*fprintf(stderr,"to (%d, %d, %d, %lld)\n",a,b,t,val);*/}
};
Node merge(const Node &x, const Node &y) {
// fprintf(stderr, "from (%d, %d, %d, %lld) and (%d, %d, %d, %lld)\n", x.a,x.b,x.t,x.val,y.a,y.b,y.t,y.val);
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);
// cerr << l << ' ' << mid << " | " << mid + 1 << ' ' << r << '\n';
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) {
// auto S = IT[id];
// cerr << "at range(" << l << ", " << r << ") = ";
// cerr << S.a << ' ' << S.b << ' ' << S.t << ' ' << S.val << '\n';
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);
// cerr << l << ' ' << mid << " | " << mid + 1 << ' ' << r << '\n';
if (L.val < 0) return R;
if (R.val < 0) return L;
return merge(L, R);
}
inline Node get(int x, int y) {
// auto S = get(x, y, 1, 1, n - 1);
// fprintf(stderr, "get(%d, %d) = {%d, %d, %d, %lld}\n", x, y, S.a, S.b, S.t, S.val);
return get(x, y, 1, 1, n - 1);
}
} seg, revseg;
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
// merge(Node(2, 4, 1, 0), Node(-1, 1, 1, 0));
// cerr << "wtf\n\n";
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);
// cerr << string(10, '>') << "at i = " << n - i << ' ' << l - (n - i) << ' ' << r - (n - i) << '\n';
// cerr << '\n';
}
// cerr << '\n';
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;
}
// cerr << n - A + 1 << ' ' << n - B << '\n';
// cerr << S.a << ' ' << S.b << ' ' << S.t << ' ' << S.val << '\n';
}
cout << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
56664 KB |
Output is correct |
2 |
Correct |
7 ms |
56668 KB |
Output is correct |
3 |
Correct |
7 ms |
56776 KB |
Output is correct |
4 |
Correct |
7 ms |
56668 KB |
Output is correct |
5 |
Correct |
7 ms |
56828 KB |
Output is correct |
6 |
Correct |
7 ms |
56668 KB |
Output is correct |
7 |
Correct |
8 ms |
56604 KB |
Output is correct |
8 |
Correct |
8 ms |
56668 KB |
Output is correct |
9 |
Correct |
7 ms |
56668 KB |
Output is correct |
10 |
Correct |
7 ms |
56668 KB |
Output is correct |
11 |
Correct |
8 ms |
56868 KB |
Output is correct |
12 |
Correct |
8 ms |
56668 KB |
Output is correct |
13 |
Correct |
8 ms |
56832 KB |
Output is correct |
14 |
Correct |
8 ms |
56668 KB |
Output is correct |
15 |
Correct |
8 ms |
56668 KB |
Output is correct |
16 |
Correct |
8 ms |
56664 KB |
Output is correct |
17 |
Correct |
9 ms |
56668 KB |
Output is correct |
18 |
Correct |
8 ms |
56668 KB |
Output is correct |
19 |
Correct |
8 ms |
56668 KB |
Output is correct |
20 |
Correct |
8 ms |
56868 KB |
Output is correct |
21 |
Correct |
9 ms |
56668 KB |
Output is correct |
22 |
Correct |
8 ms |
56668 KB |
Output is correct |
23 |
Correct |
8 ms |
56668 KB |
Output is correct |
24 |
Correct |
8 ms |
56668 KB |
Output is correct |
25 |
Correct |
8 ms |
56872 KB |
Output is correct |
26 |
Correct |
8 ms |
56768 KB |
Output is correct |
27 |
Correct |
8 ms |
56664 KB |
Output is correct |
28 |
Correct |
8 ms |
56668 KB |
Output is correct |
29 |
Correct |
8 ms |
56800 KB |
Output is correct |
30 |
Correct |
8 ms |
56664 KB |
Output is correct |
31 |
Correct |
8 ms |
56664 KB |
Output is correct |
32 |
Correct |
8 ms |
56668 KB |
Output is correct |
33 |
Correct |
8 ms |
56772 KB |
Output is correct |
34 |
Correct |
9 ms |
56668 KB |
Output is correct |
35 |
Correct |
10 ms |
56668 KB |
Output is correct |
36 |
Correct |
8 ms |
56664 KB |
Output is correct |
37 |
Correct |
8 ms |
56668 KB |
Output is correct |
38 |
Correct |
8 ms |
56668 KB |
Output is correct |
39 |
Correct |
8 ms |
56748 KB |
Output is correct |
40 |
Correct |
8 ms |
56668 KB |
Output is correct |
41 |
Correct |
7 ms |
56740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
499 ms |
73344 KB |
Output is correct |
2 |
Correct |
502 ms |
73040 KB |
Output is correct |
3 |
Correct |
496 ms |
72952 KB |
Output is correct |
4 |
Correct |
466 ms |
72896 KB |
Output is correct |
5 |
Correct |
554 ms |
73436 KB |
Output is correct |
6 |
Correct |
512 ms |
73128 KB |
Output is correct |
7 |
Correct |
484 ms |
73412 KB |
Output is correct |
8 |
Correct |
498 ms |
73540 KB |
Output is correct |
9 |
Correct |
467 ms |
73092 KB |
Output is correct |
10 |
Correct |
493 ms |
73536 KB |
Output is correct |
11 |
Correct |
480 ms |
73088 KB |
Output is correct |
12 |
Correct |
507 ms |
73520 KB |
Output is correct |
13 |
Correct |
508 ms |
73564 KB |
Output is correct |
14 |
Correct |
7 ms |
56668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
56664 KB |
Output is correct |
2 |
Correct |
7 ms |
56668 KB |
Output is correct |
3 |
Correct |
7 ms |
56776 KB |
Output is correct |
4 |
Correct |
7 ms |
56668 KB |
Output is correct |
5 |
Correct |
7 ms |
56828 KB |
Output is correct |
6 |
Correct |
7 ms |
56668 KB |
Output is correct |
7 |
Correct |
8 ms |
56604 KB |
Output is correct |
8 |
Correct |
8 ms |
56668 KB |
Output is correct |
9 |
Correct |
7 ms |
56668 KB |
Output is correct |
10 |
Correct |
7 ms |
56668 KB |
Output is correct |
11 |
Correct |
8 ms |
56868 KB |
Output is correct |
12 |
Correct |
8 ms |
56668 KB |
Output is correct |
13 |
Correct |
8 ms |
56832 KB |
Output is correct |
14 |
Correct |
8 ms |
56668 KB |
Output is correct |
15 |
Correct |
8 ms |
56668 KB |
Output is correct |
16 |
Correct |
8 ms |
56664 KB |
Output is correct |
17 |
Correct |
9 ms |
56668 KB |
Output is correct |
18 |
Correct |
8 ms |
56668 KB |
Output is correct |
19 |
Correct |
8 ms |
56668 KB |
Output is correct |
20 |
Correct |
8 ms |
56868 KB |
Output is correct |
21 |
Correct |
9 ms |
56668 KB |
Output is correct |
22 |
Correct |
8 ms |
56668 KB |
Output is correct |
23 |
Correct |
8 ms |
56668 KB |
Output is correct |
24 |
Correct |
8 ms |
56668 KB |
Output is correct |
25 |
Correct |
8 ms |
56872 KB |
Output is correct |
26 |
Correct |
8 ms |
56768 KB |
Output is correct |
27 |
Correct |
8 ms |
56664 KB |
Output is correct |
28 |
Correct |
8 ms |
56668 KB |
Output is correct |
29 |
Correct |
8 ms |
56800 KB |
Output is correct |
30 |
Correct |
8 ms |
56664 KB |
Output is correct |
31 |
Correct |
8 ms |
56664 KB |
Output is correct |
32 |
Correct |
8 ms |
56668 KB |
Output is correct |
33 |
Correct |
8 ms |
56772 KB |
Output is correct |
34 |
Correct |
9 ms |
56668 KB |
Output is correct |
35 |
Correct |
10 ms |
56668 KB |
Output is correct |
36 |
Correct |
8 ms |
56664 KB |
Output is correct |
37 |
Correct |
8 ms |
56668 KB |
Output is correct |
38 |
Correct |
8 ms |
56668 KB |
Output is correct |
39 |
Correct |
8 ms |
56748 KB |
Output is correct |
40 |
Correct |
8 ms |
56668 KB |
Output is correct |
41 |
Correct |
7 ms |
56740 KB |
Output is correct |
42 |
Correct |
499 ms |
73344 KB |
Output is correct |
43 |
Correct |
502 ms |
73040 KB |
Output is correct |
44 |
Correct |
496 ms |
72952 KB |
Output is correct |
45 |
Correct |
466 ms |
72896 KB |
Output is correct |
46 |
Correct |
554 ms |
73436 KB |
Output is correct |
47 |
Correct |
512 ms |
73128 KB |
Output is correct |
48 |
Correct |
484 ms |
73412 KB |
Output is correct |
49 |
Correct |
498 ms |
73540 KB |
Output is correct |
50 |
Correct |
467 ms |
73092 KB |
Output is correct |
51 |
Correct |
493 ms |
73536 KB |
Output is correct |
52 |
Correct |
480 ms |
73088 KB |
Output is correct |
53 |
Correct |
507 ms |
73520 KB |
Output is correct |
54 |
Correct |
508 ms |
73564 KB |
Output is correct |
55 |
Correct |
7 ms |
56668 KB |
Output is correct |
56 |
Correct |
500 ms |
70132 KB |
Output is correct |
57 |
Correct |
476 ms |
69908 KB |
Output is correct |
58 |
Correct |
504 ms |
70352 KB |
Output is correct |
59 |
Correct |
510 ms |
70440 KB |
Output is correct |
60 |
Correct |
470 ms |
69712 KB |
Output is correct |
61 |
Correct |
502 ms |
70224 KB |
Output is correct |
62 |
Correct |
513 ms |
70224 KB |
Output is correct |
63 |
Correct |
507 ms |
70336 KB |
Output is correct |
64 |
Correct |
509 ms |
70664 KB |
Output is correct |
65 |
Correct |
495 ms |
69964 KB |
Output is correct |
66 |
Correct |
497 ms |
70048 KB |
Output is correct |
67 |
Correct |
506 ms |
70676 KB |
Output is correct |
68 |
Correct |
479 ms |
69748 KB |
Output is correct |
69 |
Correct |
507 ms |
70484 KB |
Output is correct |
70 |
Correct |
472 ms |
69892 KB |
Output is correct |
71 |
Correct |
500 ms |
69308 KB |
Output is correct |
72 |
Correct |
504 ms |
69640 KB |
Output is correct |
73 |
Correct |
524 ms |
69976 KB |
Output is correct |
74 |
Correct |
503 ms |
70048 KB |
Output is correct |
75 |
Correct |
518 ms |
69932 KB |
Output is correct |
76 |
Correct |
514 ms |
70448 KB |
Output is correct |
77 |
Correct |
7 ms |
56756 KB |
Output is correct |