#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+2;
struct node{
int l,r,real_l,real_r,pts,cost=0;
node operator+(node other){
node ans;
ans.cost = cost + other.cost;
ans.real_l = max(real_l, other.real_l);
ans.real_r = min(real_r, other.real_r);
if(ans.real_l <= ans.real_r){
ans.pts = ans.real_r;
ans.cost = 0;
ans.l = ans.real_l;
ans.r = ans.real_r;
}
else{
if(real_l <= real_r){
if(other.real_l <= other.real_r){
if(other.l > r){
ans.l = ans.r = other.l;
ans.cost += 0;
ans.pts = real_r;
}
else{
ans.l = ans.r = other.r;
ans.cost += l - other.r;
ans.pts = l;
}
}
else{
ans.l = other.l;
ans.r = other.r;
if(other.pts > r){
ans.cost += 0;
ans.pts = r;
}
else{
if(other.pts >= l){
ans.cost += 0;
ans.pts = other.pts;
}
else{
ans.cost += l - other.pts;
ans.pts = l;
}
}
}
}
else{
if(l > other.pts){
ans.cost += l - other.pts;
}
ans.pts = pts;
if(other.r < l){
ans.l = ans.r = other.r;
}
else{
if(other.l <= l){
ans.l = ans.r = l;
}
else{
ans.l = ans.r = other.l;
}
}
}
}
return ans;
}
};
node it[4*N],it_rev[4*N];
pair<int,int> lis[N],lis_rev[N];
void build(int idx,int l,int r){
if(l==r){
it[idx].l = it[idx].real_l = lis[l].first;
it[idx].r = it[idx].real_r = lis[l].second;
it[idx].pts = it[idx].real_r;
it[idx].cost = 0;
return;
}
build(2*idx,l,(l+r)/2);
build(2*idx+1,(l+r)/2+1,r);
it[idx]=it[2*idx]+it[2*idx+1];
}
void upd(int idx,int l,int r,int pos,pair<int,int> val){
if(l>pos||r<pos){
return;
}
if(l==r){
it[idx].l = it[idx].real_l = val.first;
it[idx].r = it[idx].real_r = val.second;
it[idx].pts = it[idx].real_r;
it[idx].cost = 0;
return;
}
upd(2*idx,l,(l+r)/2,pos,val);
upd(2*idx+1,(l+r)/2+1,r,pos,val);
it[idx]=it[2*idx]+it[2*idx+1];
}
node get(int idx,int l,int r,int lef,int rig){
if(l>=lef&&r<=rig){
return it[idx];
}
int mid=(l+r)/2;
if(lef>mid){
return get(2*idx+1,mid+1,r,lef,rig);
}
if(mid>=rig){
return get(2*idx,l,mid,lef,rig);
}
return get(2*idx,l,mid,lef,rig)+get(2*idx+1,mid+1,r,lef,rig);
}
void build_rev(int idx,int l,int r){
if(l==r){
it_rev[idx].l = it_rev[idx].real_l = lis_rev[l].first;
it_rev[idx].r = it_rev[idx].real_r = lis_rev[l].second;
it_rev[idx].pts = it_rev[idx].real_r;
it_rev[idx].cost = 0;
return;
}
build_rev(2*idx,l,(l+r)/2);
build_rev(2*idx+1,(l+r)/2+1,r);
it_rev[idx]=it_rev[2*idx]+it_rev[2*idx+1];
}
void upd_rev(int idx,int l,int r,int pos,pair<int,int> val){
if(l>pos||r<pos){
return;
}
if(l==r){
it_rev[idx].l = it_rev[idx].real_l = val.first;
it_rev[idx].r = it_rev[idx].real_r = val.second;
it_rev[idx].pts = val.second;
it_rev[idx].cost = 0;
return;
}
upd_rev(2*idx,l,(l+r)/2,pos,val);
upd_rev(2*idx+1,(l+r)/2+1,r,pos,val);
it_rev[idx]=it_rev[2*idx]+it_rev[2*idx+1];
}
node get_rev(int idx,int l,int r,int lef,int rig){
if(l>=lef&&r<=rig){
return it_rev[idx];
}
int mid=(l+r)/2;
if(lef>mid){
return get_rev(2*idx+1,mid+1,r,lef,rig);
}
if(mid>=rig){
return get_rev(2*idx,l,mid,lef,rig);
}
return get_rev(2*idx,l,mid,lef,rig)+get_rev(2*idx+1,mid+1,r,lef,rig);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,i,j,k,l,q,z,t,m;
cin>>n>>q;
for(i=1;i<n;i++){
cin>>lis[i].first>>lis[i].second;
lis[i].second--;
lis_rev[n-i]=lis[i];
}
for(i=1;i<n;i++){
lis[i].first-=i;
lis[i].second-=i;
lis_rev[i].first-=i;
lis_rev[i].second-=i;
}
if(n==1){
for(i=1;i<=q;i++){
cin>>j;
if(j==1){
cin>>k>>l>>m;
}
else{
cin>>k>>l>>z>>t;
cout<<max(0ll,l-t)<<'\n';
}
}
return 0;
}
build(1,1,n-1);
build_rev(1,1,n-1);
pair<int,int> cac;
for(i=1;i<=q;i++){
cin>>j;
if(j==1){
cin>>j>>k>>l;
l--;
cac={k-j,l-j};
upd(1,1,n-1,j,cac);
cac={k-(n-j),l-(n-j)};
upd_rev(1,1,n-1,n-j,cac);
}
else{
cin>>j>>k>>l>>m;
if(j==l){
cout<<max(0ll,k-m)<<'\n';
continue;
}
node ans;
if(j>l){
j=(n+1-j);
l=(n+1-l);
k-=j;
m-=l;
ans=get_rev(1,1,n-1,j,l-1);
}
else{
k-=j;
m-=l;
ans=get(1,1,n-1,j,l-1);
}
if(k>ans.r){ //time came out
z=ans.r;
}
else{
if(k>=ans.l){
z=k;
}
else{
z=ans.l;
}
}
cout<<ans.cost+max(0ll,k-ans.pts)+max(0ll,z-m)<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
113144 KB |
Output is correct |
2 |
Correct |
88 ms |
113100 KB |
Output is correct |
3 |
Correct |
87 ms |
113144 KB |
Output is correct |
4 |
Correct |
87 ms |
113016 KB |
Output is correct |
5 |
Correct |
88 ms |
113148 KB |
Output is correct |
6 |
Correct |
88 ms |
113116 KB |
Output is correct |
7 |
Correct |
89 ms |
113016 KB |
Output is correct |
8 |
Correct |
88 ms |
113016 KB |
Output is correct |
9 |
Correct |
88 ms |
113016 KB |
Output is correct |
10 |
Correct |
89 ms |
113200 KB |
Output is correct |
11 |
Correct |
90 ms |
113144 KB |
Output is correct |
12 |
Correct |
94 ms |
113272 KB |
Output is correct |
13 |
Correct |
90 ms |
113272 KB |
Output is correct |
14 |
Correct |
89 ms |
113144 KB |
Output is correct |
15 |
Correct |
89 ms |
113144 KB |
Output is correct |
16 |
Correct |
91 ms |
113144 KB |
Output is correct |
17 |
Correct |
90 ms |
113144 KB |
Output is correct |
18 |
Correct |
98 ms |
113272 KB |
Output is correct |
19 |
Correct |
96 ms |
113272 KB |
Output is correct |
20 |
Correct |
90 ms |
113116 KB |
Output is correct |
21 |
Correct |
172 ms |
113272 KB |
Output is correct |
22 |
Correct |
90 ms |
113256 KB |
Output is correct |
23 |
Correct |
93 ms |
113272 KB |
Output is correct |
24 |
Correct |
103 ms |
113148 KB |
Output is correct |
25 |
Correct |
102 ms |
113272 KB |
Output is correct |
26 |
Correct |
90 ms |
113144 KB |
Output is correct |
27 |
Correct |
89 ms |
113144 KB |
Output is correct |
28 |
Correct |
89 ms |
113172 KB |
Output is correct |
29 |
Correct |
90 ms |
113172 KB |
Output is correct |
30 |
Correct |
90 ms |
113144 KB |
Output is correct |
31 |
Correct |
90 ms |
113144 KB |
Output is correct |
32 |
Correct |
109 ms |
113272 KB |
Output is correct |
33 |
Correct |
95 ms |
113144 KB |
Output is correct |
34 |
Correct |
92 ms |
113144 KB |
Output is correct |
35 |
Correct |
97 ms |
113148 KB |
Output is correct |
36 |
Correct |
91 ms |
113272 KB |
Output is correct |
37 |
Correct |
90 ms |
113144 KB |
Output is correct |
38 |
Correct |
90 ms |
113180 KB |
Output is correct |
39 |
Correct |
90 ms |
113016 KB |
Output is correct |
40 |
Correct |
90 ms |
113272 KB |
Output is correct |
41 |
Correct |
88 ms |
113144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
754 ms |
141512 KB |
Output is correct |
2 |
Correct |
711 ms |
136568 KB |
Output is correct |
3 |
Correct |
714 ms |
136668 KB |
Output is correct |
4 |
Correct |
711 ms |
136440 KB |
Output is correct |
5 |
Correct |
724 ms |
136796 KB |
Output is correct |
6 |
Correct |
735 ms |
136696 KB |
Output is correct |
7 |
Correct |
746 ms |
137336 KB |
Output is correct |
8 |
Correct |
734 ms |
137376 KB |
Output is correct |
9 |
Correct |
741 ms |
136568 KB |
Output is correct |
10 |
Correct |
736 ms |
136932 KB |
Output is correct |
11 |
Correct |
753 ms |
141612 KB |
Output is correct |
12 |
Correct |
759 ms |
142644 KB |
Output is correct |
13 |
Correct |
756 ms |
142908 KB |
Output is correct |
14 |
Correct |
87 ms |
113060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
113144 KB |
Output is correct |
2 |
Correct |
88 ms |
113100 KB |
Output is correct |
3 |
Correct |
87 ms |
113144 KB |
Output is correct |
4 |
Correct |
87 ms |
113016 KB |
Output is correct |
5 |
Correct |
88 ms |
113148 KB |
Output is correct |
6 |
Correct |
88 ms |
113116 KB |
Output is correct |
7 |
Correct |
89 ms |
113016 KB |
Output is correct |
8 |
Correct |
88 ms |
113016 KB |
Output is correct |
9 |
Correct |
88 ms |
113016 KB |
Output is correct |
10 |
Correct |
89 ms |
113200 KB |
Output is correct |
11 |
Correct |
90 ms |
113144 KB |
Output is correct |
12 |
Correct |
94 ms |
113272 KB |
Output is correct |
13 |
Correct |
90 ms |
113272 KB |
Output is correct |
14 |
Correct |
89 ms |
113144 KB |
Output is correct |
15 |
Correct |
89 ms |
113144 KB |
Output is correct |
16 |
Correct |
91 ms |
113144 KB |
Output is correct |
17 |
Correct |
90 ms |
113144 KB |
Output is correct |
18 |
Correct |
98 ms |
113272 KB |
Output is correct |
19 |
Correct |
96 ms |
113272 KB |
Output is correct |
20 |
Correct |
90 ms |
113116 KB |
Output is correct |
21 |
Correct |
172 ms |
113272 KB |
Output is correct |
22 |
Correct |
90 ms |
113256 KB |
Output is correct |
23 |
Correct |
93 ms |
113272 KB |
Output is correct |
24 |
Correct |
103 ms |
113148 KB |
Output is correct |
25 |
Correct |
102 ms |
113272 KB |
Output is correct |
26 |
Correct |
90 ms |
113144 KB |
Output is correct |
27 |
Correct |
89 ms |
113144 KB |
Output is correct |
28 |
Correct |
89 ms |
113172 KB |
Output is correct |
29 |
Correct |
90 ms |
113172 KB |
Output is correct |
30 |
Correct |
90 ms |
113144 KB |
Output is correct |
31 |
Correct |
90 ms |
113144 KB |
Output is correct |
32 |
Correct |
109 ms |
113272 KB |
Output is correct |
33 |
Correct |
95 ms |
113144 KB |
Output is correct |
34 |
Correct |
92 ms |
113144 KB |
Output is correct |
35 |
Correct |
97 ms |
113148 KB |
Output is correct |
36 |
Correct |
91 ms |
113272 KB |
Output is correct |
37 |
Correct |
90 ms |
113144 KB |
Output is correct |
38 |
Correct |
90 ms |
113180 KB |
Output is correct |
39 |
Correct |
90 ms |
113016 KB |
Output is correct |
40 |
Correct |
90 ms |
113272 KB |
Output is correct |
41 |
Correct |
88 ms |
113144 KB |
Output is correct |
42 |
Correct |
754 ms |
141512 KB |
Output is correct |
43 |
Correct |
711 ms |
136568 KB |
Output is correct |
44 |
Correct |
714 ms |
136668 KB |
Output is correct |
45 |
Correct |
711 ms |
136440 KB |
Output is correct |
46 |
Correct |
724 ms |
136796 KB |
Output is correct |
47 |
Correct |
735 ms |
136696 KB |
Output is correct |
48 |
Correct |
746 ms |
137336 KB |
Output is correct |
49 |
Correct |
734 ms |
137376 KB |
Output is correct |
50 |
Correct |
741 ms |
136568 KB |
Output is correct |
51 |
Correct |
736 ms |
136932 KB |
Output is correct |
52 |
Correct |
753 ms |
141612 KB |
Output is correct |
53 |
Correct |
759 ms |
142644 KB |
Output is correct |
54 |
Correct |
756 ms |
142908 KB |
Output is correct |
55 |
Correct |
87 ms |
113060 KB |
Output is correct |
56 |
Correct |
802 ms |
138176 KB |
Output is correct |
57 |
Correct |
787 ms |
137356 KB |
Output is correct |
58 |
Correct |
814 ms |
138596 KB |
Output is correct |
59 |
Correct |
815 ms |
138872 KB |
Output is correct |
60 |
Correct |
789 ms |
137692 KB |
Output is correct |
61 |
Correct |
849 ms |
139356 KB |
Output is correct |
62 |
Correct |
850 ms |
139256 KB |
Output is correct |
63 |
Correct |
822 ms |
139264 KB |
Output is correct |
64 |
Correct |
816 ms |
139312 KB |
Output is correct |
65 |
Correct |
805 ms |
138492 KB |
Output is correct |
66 |
Correct |
786 ms |
138572 KB |
Output is correct |
67 |
Correct |
861 ms |
139256 KB |
Output is correct |
68 |
Correct |
810 ms |
137532 KB |
Output is correct |
69 |
Correct |
927 ms |
139512 KB |
Output is correct |
70 |
Correct |
751 ms |
137820 KB |
Output is correct |
71 |
Correct |
795 ms |
136796 KB |
Output is correct |
72 |
Correct |
787 ms |
137332 KB |
Output is correct |
73 |
Correct |
814 ms |
138812 KB |
Output is correct |
74 |
Correct |
814 ms |
138840 KB |
Output is correct |
75 |
Correct |
942 ms |
139348 KB |
Output is correct |
76 |
Correct |
894 ms |
139736 KB |
Output is correct |
77 |
Correct |
85 ms |
113144 KB |
Output is correct |