Problem p03833 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

int n, m;
ll a[5000];
int b[200][5000];
int d[200][5000][13];
ll s[5002][5002];

void init() {
  rep(i, m) {
    rep(j, n) { d[i][j][0] = j; }
    for (int k = 1; k <= 12; ++k) {
      rep(j, n) {
        int p = d[i][j][k - 1];
        if (j + (1 << k - 1) >= n) {
          d[i][j][k] = p;
        } else {
          int q = d[i][j + (1 << k - 1)][k - 1];
          d[i][j][k] = b[i][p] >= b[i][q] ? p : q;
        }
      }
    }
  }
}
int query(int i, int u, int v) {
  int p = u;
  for (int k = 12; k >= 0; --k) {
    if (u + (1 << k) <= v) {
      int q = d[i][u][k];
      p = b[i][p] >= b[i][q] ? p : q;
      u += 1 << i;
    }
  }
  return p;
}

void add(int a, int b, int c, int d, int x) {
  s[a][b] += x;
  s[a][d] -= x;
  s[c][b] -= x;
  s[c][d] += x;
}
void sum() {
  rep(i, n + 1) {
    rep(j, n + 1) { s[i][j + 1] += s[i][j]; }
  }
  rep(i, n + 1) {
    rep(j, n + 1) { s[i + 1][j] += s[i][j]; }
  }
}

void calc(int i, int u, int v) {
  if (u == v) {
    return;
  }
  int p = query(i, u, v);
  add(u, p + 1, p + 1, v + 1, b[i][p]);
  calc(i, u, p);
  calc(i, p + 1, v);
}

int main() {
  cin >> n >> m;
  rep(i, n - 1) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  rep(j, n) {
    rep(i, m) { cin >> b[i][j]; }
  }

  init();
  rep(i, m) { calc(i, 0, n); }
  sum();

  ll ans = 0;
  rep(i, n) {
    for (int j = i + 1; j <= n; ++j) {
      ans = max(s[i][j] - (a[j - 1] - a[i]), ans);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

int n, m;
ll a[5000];
int b[200][5000];
int d[200][5000][13];
ll s[5002][5002];

void init() {
  rep(i, m) {
    rep(j, n) { d[i][j][0] = j; }
    for (int k = 1; k <= 12; ++k) {
      rep(j, n) {
        int p = d[i][j][k - 1];
        if (j + (1 << k - 1) >= n) {
          d[i][j][k] = p;
        } else {
          int q = d[i][j + (1 << k - 1)][k - 1];
          d[i][j][k] = b[i][p] >= b[i][q] ? p : q;
        }
      }
    }
  }
}
int query(int i, int u, int v) {
  int p = u;
  for (int k = 12; k >= 0; --k) {
    if (u + (1 << k) <= v) {
      int q = d[i][u][k];
      p = b[i][p] >= b[i][q] ? p : q;
      u += 1 << k;
    }
  }
  return p;
}

void add(int a, int b, int c, int d, int x) {
  s[a][b] += x;
  s[a][d] -= x;
  s[c][b] -= x;
  s[c][d] += x;
}
void sum() {
  rep(i, n + 1) {
    rep(j, n + 1) { s[i][j + 1] += s[i][j]; }
  }
  rep(i, n + 1) {
    rep(j, n + 1) { s[i + 1][j] += s[i][j]; }
  }
}

void calc(int i, int u, int v) {
  if (u == v) {
    return;
  }
  int p = query(i, u, v);
  add(u, p + 1, p + 1, v + 1, b[i][p]);
  calc(i, u, p);
  calc(i, p + 1, v);
}

int main() {
  cin >> n >> m;
  rep(i, n - 1) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
  }
  rep(j, n) {
    rep(i, m) { cin >> b[i][j]; }
  }

  init();
  rep(i, m) { calc(i, 0, n); }
  sum();

  ll ans = 0;
  rep(i, n) {
    for (int j = i + 1; j <= n; ++j) {
      ans = max(s[i][j] - (a[j - 1] - a[i]), ans);
    }
  }
  cout << ans << endl;
  return 0;
}