#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
template<class T> bool chmin(T& a, const T& b) {return a>b? a=b, true:false;}
template<class T> bool chmax(T& a, const T& b) {return a<b? a=b, true:false;}
const int mod = 1e9+7, N = 1005;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
int n, m, k;
int a[N][N];
struct segtree{
vector<int> t,lz;
int sz;
void init(int row){
sz=1;
while(sz < m)sz<<=1;
t.assign(sz*2,-mod*mod);
lz.assign(sz*2,0);
for(int j=0;j<m;j++){
t[j+sz] = a[row][j] - j - 1;
}
for(int j=sz-1;j>0;j--){
t[j] = max(t[j*2],t[j*2+1]);
}
}
void push(int x){
if(lz[x] == 0)return;
t[x] += lz[x];
if(x < sz){
lz[x*2] += lz[x];
lz[x*2+1] += lz[x];
}lz[x] = 0;
return;
}
void update(int l, int r, int L, int R, int x, int val){
push(x);
if(R <= l || L >= r)return;
if(L >= l && R <= r){
lz[x] += val;
push(x);
return;
}int mid = (L+R)/2;
update(l,r,L,mid,x*2,val);
update(l,r,mid,R,x*2+1,val);
t[x] = max(t[x*2], t[x*2+1]);
}
void chm(int pos, int val){
update(pos,pos+1,0,sz,1,0);
pos += sz;
for(chmax(t[pos],val);pos>1;pos>>=1)t[pos>>1] = max(t[pos],t[pos^1]);
return;
}
void update(int l, int r, int val){
update(l,r,0,sz,1,val);
return;
}
int get(){
return t[1];
}
};
void solve(int test_case){
int i, j;
memset(a,-1,sizeof a);
cin >> n >> m;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin >> a[i][j];
}
}
//~ cout << a[123][123] << '\n';
int ans = -2;
{
segtree seg;
seg.init(0);
//~ for(i=0;i<m;i++){
//~ seg.update(0,m,0);
//~ cout << seg.t[i+seg.sz] << ' ';
//~ }cout << '\n';
for(i=0;i<n;i++){
//~ for(j=0;j<m;j++){
//~ seg.update(j,j+1,0);
//~ cout << seg.t[j+seg.sz] << ' ';
//~ }cout << '\n';
chmax(ans, seg.get() - a[i][0]);
for(j=1;j<m;j++){
seg.update(0,j,-1);
seg.update(j,m,1);
chmax(ans, seg.get()-a[i][j]);
}
for(j=m-1;j>0;j--){
seg.update(0,j,1);
seg.update(j,m,-1);
}
seg.update(0,m,-1);
for(j=0;j<m;j++){
seg.chm(j,a[i+1][j]-j-1);
}
}
}
for(int l=0,r=n-1;l<r;l++,r--){
for(j=0;j<m;j++){
swap(a[l][j],a[r][j]);
}
}
{
segtree seg;
seg.init(0);
//~ for(i=0;i<m;i++){
//~ seg.update(0,m,0);
//~ cout << seg.t[i+seg.sz] << ' ';
//~ }cout << '\n';
for(i=0;i<n;i++){
//~ for(j=0;j<m;j++){
//~ seg.update(j,j+1,0);
//~ cout << seg.t[j+seg.sz] << ' ';
//~ }cout << '\n';
chmax(ans, seg.get() - a[i][0]);
for(j=1;j<m;j++){
seg.update(0,j,-1);
seg.update(j,m,1);
chmax(ans, seg.get()-a[i][j]);
}
for(j=m-1;j>0;j--){
seg.update(0,j,1);
seg.update(j,m,-1);
}
seg.update(0,m,-1);
for(j=0;j<m;j++){
seg.chm(j,a[i+1][j]-j-1);
}
}
}
//~ for(i=0;i<n;i++){
//~ for(j=0;j<m;j++){
//~ cout << a[i][j] << ' ';
//~ }cout << '\n';
//~ }
cout << ans << '\n';
return;
}
/*
2 3
5 7 5
2 4 3
*/
signed main(){
FASTIO;
//~ #define MULTITEST 1
#if MULTITEST
int _T;
cin >> _T;
for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
5 ms |
8172 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
5 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8300 KB |
Output is correct |
2 |
Correct |
7 ms |
8300 KB |
Output is correct |
3 |
Correct |
7 ms |
8300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
5 ms |
8172 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
5 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8300 KB |
Output is correct |
10 |
Correct |
8 ms |
8300 KB |
Output is correct |
11 |
Correct |
8 ms |
8300 KB |
Output is correct |
12 |
Correct |
8 ms |
8300 KB |
Output is correct |
13 |
Correct |
8 ms |
8300 KB |
Output is correct |
14 |
Correct |
8 ms |
8300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
5 ms |
8172 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
5 ms |
8172 KB |
Output is correct |
7 |
Correct |
5 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
7 ms |
8300 KB |
Output is correct |
10 |
Correct |
7 ms |
8300 KB |
Output is correct |
11 |
Correct |
7 ms |
8300 KB |
Output is correct |
12 |
Correct |
9 ms |
8300 KB |
Output is correct |
13 |
Correct |
8 ms |
8300 KB |
Output is correct |
14 |
Correct |
8 ms |
8300 KB |
Output is correct |
15 |
Correct |
8 ms |
8300 KB |
Output is correct |
16 |
Correct |
8 ms |
8300 KB |
Output is correct |
17 |
Correct |
8 ms |
8300 KB |
Output is correct |
18 |
Execution timed out |
1097 ms |
8300 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |