#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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 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 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1520 ms |
13672 KB |
Output is correct |
2 |
Correct |
1434 ms |
13048 KB |
Output is correct |
3 |
Correct |
1539 ms |
13176 KB |
Output is correct |
4 |
Correct |
1456 ms |
12952 KB |
Output is correct |
5 |
Correct |
1506 ms |
13432 KB |
Output is correct |
6 |
Correct |
1564 ms |
13396 KB |
Output is correct |
7 |
Incorrect |
1425 ms |
13700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 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 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |