This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N=1e5+5,Q=1e5+5;
int n,q,x[N];
int pl[Q],pr[Q];
int reachL[N],reachR[N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin>>n>>q;
x[0]=-1e18-5e17; x[n+1]=1e18+5e17;
for(int i=1; i<=n; i++) cin>>x[i];
vector<int> seg;
for(int i=0; i<=n; i++) seg.push_back(x[i+1]-x[i]);
pl[0]=0; pr[0]=0; int po=0;
for(int i=1; i<=q; i++){
int v; cin>>v;
po += v;
pl[i]=min(pl[i-1],po);
pr[i]=max(pr[i-1],po);
}
pl[q+1]=-4e18; pr[q+1]=4e18;
reachL[1]=pl[q]; reachR[n]=pr[q];
for(int i=1; i<n; i++){
int len = seg[i];
int lo=0,hi=q+1; //first pos where pl+pr >= len
while(lo<hi){
int mid=(lo+hi)>>1;
if(pr[mid]-pl[mid] >= len) hi=mid;
else lo=mid+1;
}
//cout<<lo<<endl;
if(lo==q+1){
reachR[i]=pr[q];
reachL[i+1]=pl[q];
continue;
} //case where they never meet
assert(lo!=0); //len is positive
//assert(pl[lo]==pl[lo-1] || pr[lo]==pr[lo-1]);
assert(pl[lo]!=pl[lo-1] || pr[lo]!=pr[lo-1]);
if(pl[lo]!=pl[lo-1]){
reachR[i] = pr[lo-1];
reachL[i+1] = - (len - pr[lo-1]); //note reachL is negative
//cout<<"LEFTCAUSE"<<i<<endl;
} //left caused the ending
else{
reachL[i+1] = pl[lo-1];
reachR[i] = len + pl[lo-1]; //reachL is negative so it's subtraction
//cout<<"RIGHTCAUSE"<<i<<endl;
} //right caused the ending
}
for(int i=1; i<=n; i++){
cout<<reachR[i]-reachL[i]<<'\n';
}
/*for(int i=0; i<=q+1; i++){
cout<<pl[i]<<" "<<pr[i]<<endl;
}*/
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |