#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 10000000000007;
int n,m;
int mat[1001][1001];
int aint[4100][4];///aint[0] = stanga sus val - lin - col
///aint[1] = dreapta sus
void upd(int nod, int st, int dr, int poz, int newv, int dim)
{
if(st==dr)
{
aint[nod][dim] = max(aint[nod][dim], newv);
return;
}
int mij=(st+dr)/2;
if(poz<=mij)
upd(nod*2,st,mij,poz,newv,dim);
else
upd(nod*2+1,mij+1,dr,poz,newv,dim);
aint[nod][dim] = max(aint[nod*2][dim], aint[nod*2+1][dim]);
}
int qry(int nod, int st, int dr, int le, int ri, int dim)
{
if(le>ri)
return -INF;
if(le==st && dr==ri)
return aint[nod][dim];
int mij=(st+dr)/2;
return max(qry(nod*2,st,mij,le,min(mij,ri),dim),
qry(nod*2+1,mij+1,dr,max(mij+1,le),ri,dim));
}
void reset_aint()
{
for(int i=0;i<4100;i++)
for(int dim=0;dim<4;dim++)
aint[i][dim]=-INF;
}
int solve()
{
reset_aint();
int rez=-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
upd(1,1,m,j,mat[i][j]+i+j,0);
upd(1,1,m,j,mat[i][j]+i-j,1);
}
for(int j=1;j<=m;j++)
{
rez = max(rez, qry(1,1,m,1,j-1,0)-mat[i][j]-i-j-1);
rez = max(rez, qry(1,1,m,j+1,m,1)-mat[i][j]-i+j-1);
}
}
return rez;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>mat[i][j];
int ceva = solve();
for(int i=1;i<=n/2;i++)
for(int j=1;j<=m;j++)
swap(mat[i][j], mat[n-i+1][j]);
ceva = max(ceva, solve());
cout<<ceva;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
684 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
684 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Execution timed out |
598 ms |
16780 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |