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 ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=2e5+5;
const ll inf=2e18;
int n,q;
pll b[maxN+1];
ll x[maxN+1],a[maxN+1],w[maxN+1],pre1[maxN+1],pre2[maxN+1],sum[maxN+1];
bool check(int k,ll tmp)
{
return ((pre1[k]+pre2[k])<=tmp);
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>x[i];
}
for(int i=1;i<=q;i++)
{
cin>>w[i];
}
sum[0]=0;
pre1[0]=pre2[0]=0;
for(int j=1;j<=q;j++)
{
sum[j]=sum[j-1]+w[j];
pre1[j]=max(pre1[j-1],sum[j]);
pre2[j]=max(pre2[j-1],-sum[j]);
}
a[1]=pre2[q];
a[n]=pre1[q];
for(int i=1;i<n;i++)
{
ll tmp=x[i+1]-x[i];
int l=1,r=q,res=0;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid,tmp))
{
res=mid;
l=mid+1;
}
else r=mid-1;
}
if(res>0)
{
b[i].fi=pre1[res];
b[i].se=pre2[res];
}
if(res<q)
{
ll pre_l=b[i].fi,pre_r=b[i].se;
ll cur=tmp-pre_l-pre_r;
if(sum[res+1]>0)
{
b[i].fi+=cur;
}
else b[i].se+=cur;
}
}
for(int i=1;i<=n;i++)
{
if(i<n)
{
a[i]+=b[i].fi;
a[i+1]+=b[i].se;
}
cout<<a[i]<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |