#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
struct P{
int l, r;
ll k;
inline P(){}
inline P(int l, int r, ll k): l(l), r(r), k(k) {}
inline friend P operator + (P a, P b){
if(a.k==-1 && b.k==-1){
if(max(a.l, b.l)<=min(a.r, b.r)) return P(max(a.l, b.l), min(a.r, b.r), -1);
if(a.l>b.r) return P(a.l, b.r, a.l-b.r);
if(a.r<b.l) return P(a.r, b.l, 0);
}
if(a.k==-1){
if(b.l>a.r) return P(a.r, b.r, b.k);
if(b.l<a.l) return P(a.l, b.r, b.k+a.l-b.l);
return b;
}
if(b.k==-1){
if(b.l>a.r) return P(a.l, b.l, a.k);
if(b.r<a.r) return P(a.l, b.r, a.k+a.r-b.r);
return a;
}
return P(a.l, b.r, a.k+b.k+max(0, a.r-b.l));
}
};
const int N=300000;
int n, q;
struct segtree{
vector<P> seg;
segtree(){
seg.resize(4*n);
}
void build(vector<P> &v, int x, int lx, int rx){
if(rx-lx==1){
seg[x]=v[lx];
return;
}
int m=(lx+rx)/2;
build(v, 2*x+1, lx, m);
build(v, 2*x+2, m, rx);
seg[x]=seg[2*x+1]+seg[2*x+2];
// cout << "seg " << lx << " " << rx << " " << seg[x].l << " " << seg[x].r << " " << seg[x].k << "\n";
}
void build(vector<P> v){ build(v, 0, 0, n-1); }
void upd(int i, P p, int x, int lx, int rx){
if(rx-lx==1){
seg[x]=p;
return;
}
int m=(lx+rx)/2;
if(i<m) upd(i, p, 2*x+1, lx, m);
else upd(i, p, 2*x+2, m, rx);
seg[x]=seg[2*x+1]+seg[2*x+2];
}
void upd(int i, P p){ upd(i, p, 0, 0, n-1); }
P query(int l, int r, int x, int lx, int rx){
if(lx>=l && rx<=r) return seg[x];
if(lx>=r || rx<=l) return P(-1e9, 1e9, -1);
int m=(lx+rx)/2;
return query(l, r, 2*x+1, lx, m)+query(l, r, 2*x+2, m, rx);
}
P query(int l, int r){ return query(l, r, 0, 0, n-1); }
};
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
vector<P> path1, path2;
segtree st1, st2;
for(int i=0; i<n-1; i++){
int a, b; cin >> a >> b;
path1.push_back(P(a-i, b-i-1, -1));
path2.push_back(P(a-(n-2-i), b-(n-2-i)-1, -1));
}
if(n>1){
st1.build(path1);
reverse(path2.begin(), path2.end());
st2.build(path2);
}
auto solve=[&](int a, int b, int c, int d, P p)->ll{
b-=a, d-=c;
if(p.k==-1){
int x=b;
x=max(x, p.l); x=min(x, p.r);
return (ll)max(0, b-x)+max(0, x-d);
}
else return (ll)max(0, b-p.l)+p.k+max(0, p.r-d);
};
while(q--){
int op; cin >> op;
if(op==1){
int i, a, b; cin >> i >> a >> b;
--i;
st1.upd(i, P(a-i, b-i-1, -1));
st2.upd(n-2-i, P(a-(n-2-i), b-(n-2-i)-1, -1));
}
else{
int a, b, c, d; cin >> a >> b >> c >> d;
--a, --c;
if(n==1){
cout << max(0, b-d) << "\n"; continue;
}
if(a==c) cout << max(0, b-d) << "\n";
if(a<c) cout << solve(a, b, c, d, st1.query(a, c)) << "\n";
if(a>c) cout << solve(n-1-a, b, n-1-c, d, st2.query(n-1-a, n-1-c)) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
368 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
479 ms |
50228 KB |
Output is correct |
2 |
Correct |
428 ms |
33128 KB |
Output is correct |
3 |
Correct |
444 ms |
33272 KB |
Output is correct |
4 |
Correct |
420 ms |
32764 KB |
Output is correct |
5 |
Correct |
500 ms |
50140 KB |
Output is correct |
6 |
Correct |
450 ms |
49868 KB |
Output is correct |
7 |
Correct |
470 ms |
50536 KB |
Output is correct |
8 |
Correct |
491 ms |
51104 KB |
Output is correct |
9 |
Correct |
508 ms |
33072 KB |
Output is correct |
10 |
Correct |
473 ms |
50284 KB |
Output is correct |
11 |
Correct |
455 ms |
50228 KB |
Output is correct |
12 |
Correct |
507 ms |
51308 KB |
Output is correct |
13 |
Correct |
480 ms |
51452 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
368 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
479 ms |
50228 KB |
Output is correct |
43 |
Correct |
428 ms |
33128 KB |
Output is correct |
44 |
Correct |
444 ms |
33272 KB |
Output is correct |
45 |
Correct |
420 ms |
32764 KB |
Output is correct |
46 |
Correct |
500 ms |
50140 KB |
Output is correct |
47 |
Correct |
450 ms |
49868 KB |
Output is correct |
48 |
Correct |
470 ms |
50536 KB |
Output is correct |
49 |
Correct |
491 ms |
51104 KB |
Output is correct |
50 |
Correct |
508 ms |
33072 KB |
Output is correct |
51 |
Correct |
473 ms |
50284 KB |
Output is correct |
52 |
Correct |
455 ms |
50228 KB |
Output is correct |
53 |
Correct |
507 ms |
51308 KB |
Output is correct |
54 |
Correct |
480 ms |
51452 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
467 ms |
62488 KB |
Output is correct |
57 |
Correct |
450 ms |
44916 KB |
Output is correct |
58 |
Correct |
455 ms |
63264 KB |
Output is correct |
59 |
Correct |
465 ms |
63336 KB |
Output is correct |
60 |
Correct |
428 ms |
45384 KB |
Output is correct |
61 |
Correct |
462 ms |
63796 KB |
Output is correct |
62 |
Correct |
498 ms |
63704 KB |
Output is correct |
63 |
Correct |
501 ms |
63796 KB |
Output is correct |
64 |
Correct |
456 ms |
63948 KB |
Output is correct |
65 |
Correct |
468 ms |
62852 KB |
Output is correct |
66 |
Correct |
503 ms |
63020 KB |
Output is correct |
67 |
Correct |
496 ms |
63708 KB |
Output is correct |
68 |
Correct |
447 ms |
61880 KB |
Output is correct |
69 |
Correct |
475 ms |
64336 KB |
Output is correct |
70 |
Correct |
457 ms |
45516 KB |
Output is correct |
71 |
Correct |
440 ms |
44204 KB |
Output is correct |
72 |
Correct |
438 ms |
56108 KB |
Output is correct |
73 |
Correct |
482 ms |
63308 KB |
Output is correct |
74 |
Correct |
480 ms |
63744 KB |
Output is correct |
75 |
Correct |
477 ms |
63880 KB |
Output is correct |
76 |
Correct |
468 ms |
64508 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |