# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
978373 | sleepntsheep | Fish 3 (JOI24_fish3) | C++17 | 2069 ms | 12896 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
long long lo(long long a,long long b){return a<b?a:b;}
long long hi(long long a,long long b){return a>b?a:b;}
#include<stdio.h>
#include<assert.h>
#include<string.h>
#define N 300005
int n,q;
long long d,c[N],t[N],b[N],pc[N],bo,suff_min[N];
int main()
{
scanf("%d%lld",&n,&d);
for(int i=1;i<=n;++i)
{
scanf("%lld",c+i),pc[i]=pc[i-1]+c[i];
if(!c[i])b[bo++]=i;
}
scanf("%d",&q);
for(int l,r,i=0;i<q;++i)
{
scanf("%d%d",&l,&r);
memcpy(t,c,sizeof t);
long long run=0,z=0;
for(int i=l;i<=r;++i)
{
if(t[i]<run)
{
puts("-1");
goto X;
}
run+=(t[i]-run)%d;
t[i]-=run;
}
suff_min[r]=t[r];
for(int i=r-1;i>=l;--i)suff_min[i]=lo(suff_min[i+1],t[i]);
run=0;
for(int i=l;i<=r;++i)
{
if(suff_min[i]-run>=d)
{
for(int j=i;j<=r;++j)
t[i]-=(suff_min[i]-run)/d*d;
run+=(suff_min[i]-run)/d*d;
}
}
for(int i=l;i<=r;++i)
{
if(t[i]%d){puts("-1");goto X;}
z+=t[i]/d;
}
printf("%lld\n",z);
X:;
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |