Problem p03237 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC diagnostic ignored "-Wunused-result"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 502;
// const ll INF = (ll)1e18 + 41;
const int INF = (int)1e9 + 41;
const ll MD = (ll)1e9 + 7;
bool D = false;

int sum[N][N], a[N][N];
int max_depth[N][N];
short int r[2][N][N];
short int l[2][N][N];

void solve() {
  int n, m;
  cin >> n;
  m = n;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      a[i][j] = 0;
      max_depth[i][j] = i;
      sum[i][j] = 0;
    }
  }

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j <= m; j++) {
      for (int h = 0; h <= m; h++) {
        r[i][j][h] = (short int)-1;
        l[i][j][h] = (short int)N;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '#')
        a[i][j] = 0;
      else
        a[i][j] = s[j] - '0';
    }
  }
  ll ans = 0;

  for (int i = n - 1; i >= 0; i--) {
    int my = i % 2;
    int nx = (i + 1) % 2;

    for (int j = 0; j <= m; j++) {
      for (int h = i; h <= n; h++) {
        l[my][j][h] = (short int)N;
        r[my][j][h] = (short int)-1;
      }
    }

    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 0) {
        max_depth[i][j] = -1;
        continue;
      }

      short int *left = r[nx][j];
      short int *_left = l[nx][j];
      short int *rait = l[my][j + 1];
      short int *_rait = r[my][j + 1];
      short int *meL = l[my][j];
      short int *meR = r[my][j];

      meL[i] = (short int)j;
      meR[i] = max(meL[i], _rait[i]);
      for (int h = i + 1; h < n; h++) {
        meL[h] = min(_left[h], rait[h]);
        meR[h] = max(left[h], _rait[h]);
      }
      max_depth[i][j] = max(i, max(max_depth[i + 1][j], max_depth[i][j + 1]));

      int cur_sum = sum[i][j + 1] + sum[i + 1][j];
      int h = i;
      // cout << i << " " << j << endl;
      if (i == 1 && j == 0) {
        j = i * j;
      }
      while (h < n) {
        while (h < n && left[h] < rait[h])
          h++;
        if (h >= n)
          continue;

        cur_sum -= sum[h][rait[h]];
        h = max_depth[h][rait[h]] + 2;
      }

      sum[i][j] = cur_sum + a[i][j];
      ans += a[i][j] * cur_sum;
    }
  }

  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tst = 1;
  // scanf("%d", &tst);
  // cin >> tst;
#ifdef MADE_BY_SERT
  // freopen("a.in", "r", stdin);
  D = true;
  tst = 4;
  while (tst--) {
    solve();
  }
#else
  while (tst--)
    solve();
#endif
}


Fixed submission:
#pragma GCC diagnostic ignored "-Wunused-result"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1502;
// const ll INF = (ll)1e18 + 41;
const int INF = (int)1e9 + 41;
const ll MD = (ll)1e9 + 7;
bool D = false;

int sum[N][N], a[N][N];
int max_depth[N][N];
short int r[2][N][N];
short int l[2][N][N];

void solve() {
  int n, m;
  cin >> n;
  m = n;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      a[i][j] = 0;
      max_depth[i][j] = i;
      sum[i][j] = 0;
    }
  }

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j <= m; j++) {
      for (int h = 0; h <= m; h++) {
        r[i][j][h] = (short int)-1;
        l[i][j][h] = (short int)N;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '#')
        a[i][j] = 0;
      else
        a[i][j] = s[j] - '0';
    }
  }
  ll ans = 0;

  for (int i = n - 1; i >= 0; i--) {
    int my = i % 2;
    int nx = (i + 1) % 2;

    for (int j = 0; j <= m; j++) {
      for (int h = i; h <= n; h++) {
        l[my][j][h] = (short int)N;
        r[my][j][h] = (short int)-1;
      }
    }

    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 0) {
        max_depth[i][j] = -1;
        continue;
      }

      short int *left = r[nx][j];
      short int *_left = l[nx][j];
      short int *rait = l[my][j + 1];
      short int *_rait = r[my][j + 1];
      short int *meL = l[my][j];
      short int *meR = r[my][j];

      meL[i] = (short int)j;
      meR[i] = max(meL[i], _rait[i]);
      for (int h = i + 1; h < n; h++) {
        meL[h] = min(_left[h], rait[h]);
        meR[h] = max(left[h], _rait[h]);
      }
      max_depth[i][j] = max(i, max(max_depth[i + 1][j], max_depth[i][j + 1]));

      int cur_sum = sum[i][j + 1] + sum[i + 1][j];
      int h = i;
      // cout << i << " " << j << endl;
      if (i == 1 && j == 0) {
        j = i * j;
      }
      while (h < n) {
        while (h < n && left[h] < rait[h])
          h++;
        if (h >= n)
          continue;

        cur_sum -= sum[h][rait[h]];
        h = max_depth[h][rait[h]] + 2;
      }

      sum[i][j] = cur_sum + a[i][j];
      ans += a[i][j] * cur_sum;
    }
  }

  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tst = 1;
  // scanf("%d", &tst);
  // cin >> tst;
#ifdef MADE_BY_SERT
  // freopen("a.in", "r", stdin);
  D = true;
  tst = 4;
  while (tst--) {
    solve();
  }
#else
  while (tst--)
    solve();
#endif
}