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<iostream>
using namespace std;
int r,s,a,b;
long long **sum_pr,odp;
long long sumzkwr(int xpd,int ypd,int xlg,int ylg)
{
return sum_pr[xpd][ypd]-sum_pr[xlg-1][ypd]-sum_pr[xpd][ylg-1]+sum_pr[xlg-1][ylg-1];
}
void bs(int j,int i,int k)
{
int pocz=1,kon=i+1,sr=(pocz+kon)>>1;
while(pocz!=sr)
{
if(sumzkwr(j,i,k,sr)>=b)
pocz=sr;
else
kon=sr;
sr=(pocz+kon)>>1;
}
long long pom=sumzkwr(j,i,k,sr);
odp=min(odp,abs(a-pom)+abs(b-pom));
if(pom>b&&sr<i)
{
++sr;
pom=sumzkwr(j,i,k,sr);
odp=min(odp,abs(a-pom)+abs(b-pom));
}
}
int main()
{
scanf("%d%d%d%d",&r,&s,&a,&b);
if(a>b)
swap(a,b);
odp=a+b;
sum_pr=new long long *[s+1];
for(int i=0;i<=s;++i)
sum_pr[i]=new long long [r+1];
for(int i=1;i<=r;++i)
{
for(int j=1;j<=s;++j)
{
scanf("%lld",&sum_pr[j][i]);
sum_pr[j][i]+=sum_pr[j-1][i]+sum_pr[j][i-1]-sum_pr[j-1][i-1];
for(int k=j;k>=1;--k)
bs(j,i,k);
}
}
printf("%lld",odp);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d%d%d%d",&r,&s,&a,&b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:42:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%lld",&sum_pr[j][i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |