#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<lint, lint>;
const int MAXN = 600005;
const int B = 66;
int n, q;
pi a[MAXN];
struct bucket{
lint ints, inte;
int destination;
lint offset;
bool found;
void remake(int s, int e){
s = max(s, 1); e = min(e, 2 * n - 1);
ints = -1e9, inte = 2e9;
offset = 0;
found = 0;
for(int i=s; i<=e; i++){
if(max(ints, a[i].first) <= min(inte, a[i].second)){
inte = min(inte, a[i].second);
ints = max(ints, a[i].first);
}
else{
found = 1;
if(a[i].first > inte) ints = inte;
if(a[i].second < ints) inte = ints;
int x = inte;
for(int j=i; j<=e; j++){
if(x < a[j].first) x = a[j].first;
if(x > a[j].second){
offset += x - a[j].second;
x = a[j].second;
}
}
destination = x;
break;
}
}
}
pi process(int x){
if(!found){
if(x > inte) return pi(x - inte, inte);
else return pi(0ll, max(x * 1ll, ints));
}
lint ret = offset + max(x - ints, 0ll);
return pi(ret, 1ll * destination);
}
}bkt[10000];
pi solve(int s, int e, int x){
if(e - s <= 3*B){
lint ans = 0;
for(int i=s; i<=e; i++){
if(x < a[i].first) x = a[i].first;
if(x > a[i].second){
ans += x - a[i].second;
x = a[i].second;
}
}
return pi(ans, x);
}
else{
auto ret1 = solve(s, (s/B + 1) * B - 1, x);
lint ans = ret1.first;
x = ret1.second;
for(int i=s/B+1; i<e/B; i++){
auto nxt = bkt[i].process(x);
ans += nxt.first;
x = nxt.second;
}
auto ret2 = solve((e / B) * B, e, x);
ans += ret2.first;
x = ret2.second;
return pi(ans, x);
}
}
int main(){
scanf("%d %d",&n,&q);
for(int i=1; i<n; i++){
scanf("%lld %lld",&a[i].first,&a[i].second);
a[2*n-i] = a[i];
}
a[n] = pi(6, 9);
for(int i=1; i<=2*n-1; i++){
a[i].first -= i;
a[i].second -= i + 1;
}
for(int i=0; i<=2*n-1; i+=B){
bkt[i/B].remake(i, i + B - 1);
}
for(int i=0; i<q; i++){
int t; scanf("%d",&t);
if(t == 1){
int p, s, e; scanf("%d %d %d",&p,&s,&e);
a[p] = pi(s - p, e - p - 1);
a[2*n-p] = pi(s - 2 * n + p, e - 2 * n + p - 1);
{
int key = p / B;
bkt[key].remake(key * B, key * B + B - 1);
}
{
int key = (2 * n - p) / B;
bkt[key].remake(key * B, key * B + B - 1);
}
}
else{
int p, q, r, s; scanf("%d %d %d %d",&p,&q,&r,&s);
lint ans = 0;
if(p < r){
q -= p;
s -= r;
auto dap = solve(p, r - 1, q);
ans = dap.first + max(dap.second - s, 0ll);
}
else if(p > r){
q -= 2 * n - p + 1;
s -= 2 * n - r + 1;
auto dap = solve(2 * n - p + 1, 2 * n - r, q);
ans = dap.first + max(dap.second - s, 0ll);
}
else{
ans = max(q - s, 0);
}
printf("%lld\n", ans);
}
}
}
Compilation message
timeleap.cpp: In function 'int main()':
timeleap.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&q);
~~~~~^~~~~~~~~~~~~~~
timeleap.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&a[i].first,&a[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:96:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int t; scanf("%d",&t);
~~~~~^~~~~~~~~
timeleap.cpp:98:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int p, s, e; scanf("%d %d %d",&p,&s,&e);
~~~~~^~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:111:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int p, q, r, s; scanf("%d %d %d %d",&p,&q,&r,&s);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
408 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
4 ms |
384 KB |
Output is correct |
38 |
Correct |
4 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
380 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1624 ms |
13480 KB |
Output is correct |
2 |
Correct |
1491 ms |
13176 KB |
Output is correct |
3 |
Correct |
1351 ms |
13132 KB |
Output is correct |
4 |
Correct |
1298 ms |
12744 KB |
Output is correct |
5 |
Correct |
1637 ms |
13460 KB |
Output is correct |
6 |
Correct |
1688 ms |
13236 KB |
Output is correct |
7 |
Correct |
1653 ms |
13840 KB |
Output is correct |
8 |
Correct |
1667 ms |
14616 KB |
Output is correct |
9 |
Correct |
1512 ms |
13436 KB |
Output is correct |
10 |
Correct |
1560 ms |
14132 KB |
Output is correct |
11 |
Correct |
1677 ms |
13888 KB |
Output is correct |
12 |
Correct |
1569 ms |
14712 KB |
Output is correct |
13 |
Correct |
1729 ms |
14712 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
408 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
4 ms |
384 KB |
Output is correct |
38 |
Correct |
4 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
380 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
1624 ms |
13480 KB |
Output is correct |
43 |
Correct |
1491 ms |
13176 KB |
Output is correct |
44 |
Correct |
1351 ms |
13132 KB |
Output is correct |
45 |
Correct |
1298 ms |
12744 KB |
Output is correct |
46 |
Correct |
1637 ms |
13460 KB |
Output is correct |
47 |
Correct |
1688 ms |
13236 KB |
Output is correct |
48 |
Correct |
1653 ms |
13840 KB |
Output is correct |
49 |
Correct |
1667 ms |
14616 KB |
Output is correct |
50 |
Correct |
1512 ms |
13436 KB |
Output is correct |
51 |
Correct |
1560 ms |
14132 KB |
Output is correct |
52 |
Correct |
1677 ms |
13888 KB |
Output is correct |
53 |
Correct |
1569 ms |
14712 KB |
Output is correct |
54 |
Correct |
1729 ms |
14712 KB |
Output is correct |
55 |
Correct |
2 ms |
256 KB |
Output is correct |
56 |
Correct |
1218 ms |
11768 KB |
Output is correct |
57 |
Correct |
1141 ms |
11128 KB |
Output is correct |
58 |
Correct |
1237 ms |
11956 KB |
Output is correct |
59 |
Correct |
1167 ms |
12072 KB |
Output is correct |
60 |
Correct |
1130 ms |
11200 KB |
Output is correct |
61 |
Correct |
1287 ms |
12292 KB |
Output is correct |
62 |
Correct |
1313 ms |
12268 KB |
Output is correct |
63 |
Correct |
1275 ms |
12304 KB |
Output is correct |
64 |
Correct |
1316 ms |
12224 KB |
Output is correct |
65 |
Correct |
1281 ms |
11716 KB |
Output is correct |
66 |
Correct |
1161 ms |
11892 KB |
Output is correct |
67 |
Correct |
1014 ms |
12256 KB |
Output is correct |
68 |
Correct |
937 ms |
11608 KB |
Output is correct |
69 |
Correct |
1061 ms |
12420 KB |
Output is correct |
70 |
Correct |
1177 ms |
11392 KB |
Output is correct |
71 |
Correct |
1182 ms |
10924 KB |
Output is correct |
72 |
Correct |
1171 ms |
11236 KB |
Output is correct |
73 |
Correct |
1207 ms |
12280 KB |
Output is correct |
74 |
Correct |
1098 ms |
12288 KB |
Output is correct |
75 |
Correct |
1139 ms |
12356 KB |
Output is correct |
76 |
Correct |
1162 ms |
12508 KB |
Output is correct |
77 |
Correct |
2 ms |
256 KB |
Output is correct |