#include <bits/stdc++.h>
using namespace std;
const int nx=1505;
int n, m, a[nx][nx], res=INT_MIN;
struct segtree
{
int d[4*nx];
void build(int l, int r, int i)
{
d[i]=-2e9;
if (l==r) return;
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
}
void update(int l, int r, int i, int idx, int vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]=max(d[i], vl), void();
int md=(l+r)/2;
update(l, md, 2*i, idx, vl);
update(md+1, r, 2*i+1, idx, vl);
d[i]=max(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return INT_MIN;
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return max(query(l, md, 2*i, ql, qr), query(md+1, r, 2*i+1, ql, qr));
}
} up, dn;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) cin>>a[i][j];
up.build(1, m, 1);
dn.build(1, m, 1);
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
up.update(1, m, 1, j, -a[i][j]+j+i);
//cout<<"query up "<<i<<' '<<j<<' '<<a[i][j]+i+j+up.query(1, n, 1, 1, j)-1<<'\n';
res=max(res, a[i][j]-i-j+up.query(1, m, 1, 1, j)-1);
}
for (int j=m; j>=1; j--)
{
dn.update(1, m, 1, j, -a[i][j]-j+i);
res=max(res, a[i][j]-i+j+dn.query(1, m, 1, j, m)-1);
}
}
up.build(1, m, 1);
dn.build(1, m, 1);
for (int i=n; i>=1; i--)
{
for (int j=1; j<=m; j++)
{
up.update(1, m, 1, j, -a[i][j]+j-i);
res=max(res, a[i][j]+i-j+up.query(1, m, 1, 1, j)-1);
}
for (int j=m; j>=1; j--)
{
dn.update(1, m, 1, j, -a[i][j]-j-i);
res=max(res, a[i][j]+i+j+dn.query(1, m, 1, j, m)-1);
}
}
cout<<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2520 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2520 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
238 ms |
15152 KB |
Output is correct |
19 |
Correct |
233 ms |
15160 KB |
Output is correct |
20 |
Correct |
212 ms |
14764 KB |
Output is correct |
21 |
Correct |
230 ms |
15152 KB |
Output is correct |
22 |
Correct |
234 ms |
15012 KB |
Output is correct |
23 |
Correct |
244 ms |
15188 KB |
Output is correct |
24 |
Correct |
217 ms |
16012 KB |
Output is correct |