#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
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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
6 ms |
340 KB |
Output is correct |
11 |
Correct |
16 ms |
380 KB |
Output is correct |
12 |
Correct |
16 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
20 ms |
340 KB |
Output is correct |
16 |
Correct |
16 ms |
372 KB |
Output is correct |
17 |
Correct |
16 ms |
340 KB |
Output is correct |
18 |
Correct |
13 ms |
340 KB |
Output is correct |
19 |
Correct |
14 ms |
340 KB |
Output is correct |
20 |
Correct |
13 ms |
376 KB |
Output is correct |
21 |
Correct |
14 ms |
340 KB |
Output is correct |
22 |
Correct |
13 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
6 ms |
340 KB |
Output is correct |
11 |
Correct |
16 ms |
380 KB |
Output is correct |
12 |
Correct |
16 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
20 ms |
340 KB |
Output is correct |
16 |
Correct |
16 ms |
372 KB |
Output is correct |
17 |
Correct |
16 ms |
340 KB |
Output is correct |
18 |
Correct |
13 ms |
340 KB |
Output is correct |
19 |
Correct |
14 ms |
340 KB |
Output is correct |
20 |
Correct |
13 ms |
376 KB |
Output is correct |
21 |
Correct |
14 ms |
340 KB |
Output is correct |
22 |
Correct |
13 ms |
372 KB |
Output is correct |
23 |
Execution timed out |
2052 ms |
2260 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |