#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e5 + 5;
int n, q, L[2][N], R[2][N];
struct node{
ll l, r, pos, cost;
} st[2][4 * N];
node merge(node a, node b){
node res;
if(a.l == a.r){
if(b.l == b.r){
res.l = res.r = a.l;
res.pos = b.pos;
res.cost = a.cost + b.cost + max(0LL, a.pos - b.l);
} else {
res.l = res.r = a.l;
res.pos = min(b.r, max(b.l, a.pos));
res.cost = a.cost + b.cost + max(0LL, a.pos - b.r);
}
} else {
if(b.l == b.r){
if(a.r < b.l){
res.l = res.r = a.r;
res.pos = b.pos;
res.cost = a.cost + b.cost;
} else if(a.l > b.l){
res.l = res.r = a.l;
res.pos = b.pos;
res.cost = a.cost + b.cost + a.pos - b.l;
} else {
res.l = res.r = b.l;
res.pos = b.pos;
res.cost = a.cost + b.cost;
}
} else {
if(a.r < b.l){
res.l = res.r = a.r;
res.pos = b.l;
res.cost = a.cost + b.cost;
} else if(a.l > b.r){
res.l = res.r = a.l;
res.pos = b.r;
res.cost = a.cost + b.cost + a.l - b.r;
} else {
res.l = max(a.l, b.l);
res.r = min(a.r, b.r);
res.pos = res.l;
res.cost = 0;
}
}
}
return res;
}
void build(int id, int l, int r, int t){
if(l == r){
st[t][id].l = L[t][l];
st[t][id].r = R[t][l];
st[t][id].pos = L[t][l];
st[t][id].cost = 0;
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid, t);
build(id << 1 | 1, mid + 1, r, t);
st[t][id] = merge(st[t][id << 1], st[t][id << 1 | 1]);
}
void update(int id, int l, int r, int i, node x, int t){
if(l > i || r < i){
return;
}
if(l == r){
st[t][id] = x;
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, i, x, t);
update(id << 1 | 1, mid + 1, r, i, x, t);
st[t][id] = merge(st[t][id << 1], st[t][id << 1 | 1]);
}
node get(int id, int l, int r, int i, int j, int t){
if(l > j || r < i || i > j){
return {(ll)-1e9, (ll)1e9, (ll)-1e9, 0};
}
if(l >= i && r <= j){
return st[t][id];
}
int mid = (l + r) >> 1;
return merge(get(id << 1, l, mid, i, j, t), get(id << 1 | 1, mid + 1, r, i, j, t));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
if(n == 1){
while(q--){
int t, a, b, c, d;
cin >> t;
if(t == 2){
cin >> a >> b >> c >> d;
cout << max(0, b - d) << "\n";
}
}
return 0;
}
for(int i = 1; i < n; i++){
cin >> L[0][i] >> R[0][i];
L[1][n - i] = L[0][i];
R[1][n - i] = R[0][i];
}
for(int i = 1; i < n; i++){
L[0][i] -= i; R[0][i] -= i + 1;
L[1][i] -= i; R[1][i] -= i + 1;
}
build(1, 1, n - 1, 0);
build(1, 1, n - 1, 1);
while(q--){
int t;
cin >> t;
if(t == 1){
int p, s, e;
cin >> p >> s >> e;
node tmp = {s - p, e - p - 1, s - p, 0};
update(1, 1, n - 1, p, tmp, 0);
p = n - p;
tmp = {s - p, e - p - 1, s - p, 0};
update(1, 1, n - 1, p, tmp, 1);
} else {
int a, b, c, d;
cin >> a >> b >> c >> d;
if(a == c){
cout << max(0, b - d) << "\n";
} else {
int ck = 0;
if(a > c){
a = n - a + 1; c = n - c + 1;
ck = 1;
}
node res = get(1, 1, n - 1, a, c - 1, ck);
b -= a; d -= c;
res = merge({b, b, b, 0}, res);
cout << res.cost + max(0LL, res.pos - d) << "\n";
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
7 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
6 ms |
640 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
6 ms |
640 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
640 KB |
Output is correct |
29 |
Correct |
7 ms |
640 KB |
Output is correct |
30 |
Correct |
7 ms |
640 KB |
Output is correct |
31 |
Correct |
6 ms |
640 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
6 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
512 KB |
Output is correct |
35 |
Correct |
6 ms |
640 KB |
Output is correct |
36 |
Correct |
6 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
512 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
512 KB |
Output is correct |
40 |
Correct |
6 ms |
512 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
701 ms |
90192 KB |
Output is correct |
2 |
Correct |
654 ms |
56744 KB |
Output is correct |
3 |
Correct |
653 ms |
56952 KB |
Output is correct |
4 |
Correct |
675 ms |
56672 KB |
Output is correct |
5 |
Correct |
695 ms |
90064 KB |
Output is correct |
6 |
Correct |
723 ms |
89848 KB |
Output is correct |
7 |
Correct |
690 ms |
90216 KB |
Output is correct |
8 |
Correct |
663 ms |
91000 KB |
Output is correct |
9 |
Correct |
654 ms |
56824 KB |
Output is correct |
10 |
Correct |
654 ms |
90232 KB |
Output is correct |
11 |
Correct |
658 ms |
90080 KB |
Output is correct |
12 |
Correct |
677 ms |
90984 KB |
Output is correct |
13 |
Correct |
735 ms |
91188 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
7 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
640 KB |
Output is correct |
17 |
Correct |
6 ms |
640 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
6 ms |
640 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
640 KB |
Output is correct |
29 |
Correct |
7 ms |
640 KB |
Output is correct |
30 |
Correct |
7 ms |
640 KB |
Output is correct |
31 |
Correct |
6 ms |
640 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
6 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
512 KB |
Output is correct |
35 |
Correct |
6 ms |
640 KB |
Output is correct |
36 |
Correct |
6 ms |
640 KB |
Output is correct |
37 |
Correct |
6 ms |
512 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
512 KB |
Output is correct |
40 |
Correct |
6 ms |
512 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
701 ms |
90192 KB |
Output is correct |
43 |
Correct |
654 ms |
56744 KB |
Output is correct |
44 |
Correct |
653 ms |
56952 KB |
Output is correct |
45 |
Correct |
675 ms |
56672 KB |
Output is correct |
46 |
Correct |
695 ms |
90064 KB |
Output is correct |
47 |
Correct |
723 ms |
89848 KB |
Output is correct |
48 |
Correct |
690 ms |
90216 KB |
Output is correct |
49 |
Correct |
663 ms |
91000 KB |
Output is correct |
50 |
Correct |
654 ms |
56824 KB |
Output is correct |
51 |
Correct |
654 ms |
90232 KB |
Output is correct |
52 |
Correct |
658 ms |
90080 KB |
Output is correct |
53 |
Correct |
677 ms |
90984 KB |
Output is correct |
54 |
Correct |
735 ms |
91188 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
713 ms |
86904 KB |
Output is correct |
57 |
Correct |
695 ms |
53344 KB |
Output is correct |
58 |
Correct |
690 ms |
87288 KB |
Output is correct |
59 |
Correct |
746 ms |
87416 KB |
Output is correct |
60 |
Correct |
669 ms |
53764 KB |
Output is correct |
61 |
Correct |
773 ms |
87672 KB |
Output is correct |
62 |
Correct |
752 ms |
87800 KB |
Output is correct |
63 |
Correct |
777 ms |
87660 KB |
Output is correct |
64 |
Correct |
744 ms |
87724 KB |
Output is correct |
65 |
Correct |
734 ms |
87288 KB |
Output is correct |
66 |
Correct |
710 ms |
87116 KB |
Output is correct |
67 |
Correct |
717 ms |
87788 KB |
Output is correct |
68 |
Correct |
717 ms |
72696 KB |
Output is correct |
69 |
Correct |
719 ms |
87800 KB |
Output is correct |
70 |
Correct |
688 ms |
53848 KB |
Output is correct |
71 |
Correct |
678 ms |
52856 KB |
Output is correct |
72 |
Correct |
670 ms |
64316 KB |
Output is correct |
73 |
Correct |
713 ms |
87136 KB |
Output is correct |
74 |
Correct |
697 ms |
87288 KB |
Output is correct |
75 |
Correct |
722 ms |
87416 KB |
Output is correct |
76 |
Correct |
719 ms |
88056 KB |
Output is correct |
77 |
Correct |
5 ms |
384 KB |
Output is correct |