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 <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e3 + 69;
int seg[2 * N][4];
int a[N][N];
int ok;
void update(int l, int r, int pos, int x, int v, int i){
    x--;
    int n = ok;
    x += n;
    if (seg[x][i] > v) return;
    for (seg[x][i] = v; x; x >>= 1){
        seg[x >> 1][i] = max(seg[x][i], seg[x ^ 1][i]);
    }
}
int query(int ll, int rr, int pos, int l, int r, int i){
    l--;
    int ans = -INF;
    int n = ok;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1){
        if (l & 1) ans = max(ans, seg[l++][i]);
        if (r & 1) ans = max(ans, seg[--r][i]);
    }
    
    return ans;
}
void Solve() 
{
    int n, m; cin >> n >> m;
    ok = m;
    
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    
    for (int i = 0; i < 2 * N; i++){
        for (int j = 0; j < 4; j++) {
            seg[i][j] = -INF;
        }
    }
    
    int ans = -1;
    
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            // 0 --> a, y -, - 
            // 1 --> a, y +, - 
            // 2 --> a, y -, +
            // 3 --> a, y +, + 
            
            ans = max(ans, query(1, m, 1, j, m, 0) + a[i][j] + j - i - 1);
            ans = max(ans, query(1, m, 1, j, m, 1) - a[i][j] + j - i - 1);
            ans = max(ans, query(1, m, 1, 1, j, 2) + a[i][j] - j - i - 1);
            ans = max(ans, query(1, m, 1, 1, j, 3) - a[i][j] - j - i - 1);
            
            update(1, m, 1, j, -a[i][j] - j + i, 0);
            update(1, m, 1, j, a[i][j] - j + i, 1);
            update(1, m, 1, j, -a[i][j] + j + i, 2);
            update(1, m, 1, j, a[i][j] + j + i, 3);
        }
    }
    
    cout << ans << "\n";
}
int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
  //  cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |