Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int MODULO = 1000000000;
const int INF = 2000000000; // 1e9 * 2
const double EPS = 1e-7;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef complex<double> Cd;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

// start

int *table[5010];

int main() {
  FOR(i, 5010) table[i] = (int *)calloc((i + 3), sizeof(int));

  int n, m;
  cin >> n >> m;
  FOR(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    table[a][b]++;
    table[a][b + 1]--;
    table[a + x + 1][b + x + 2]++;
    table[a + x + 1][b]--;
    table[a + x + 2][b + 1]++;
    table[a + x + 2][b + x + 2]--;
  }

  // 右
  FOR(i, n + 2) FOR(j, i + 1) table[i][j + 1] += table[i][j];
  // 下
  FOR(i, n + 1) FOR(j, i + 1) table[i + 1][j] += table[i][j];
  // 右下
  FOR(i, n + 1) FOR(j, i + 1) table[i + 1][j + 1] += table[i][j];

  int ans = 0;
  FOR(i, n) FOR(j, i + 1) ans += table[i][j] > 0;
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int MODULO = 1000000000;
const int INF = 2000000000; // 1e9 * 2
const double EPS = 1e-7;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef complex<double> Cd;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

// start

int *table[5010];

int main() {
  FOR(i, 5010) table[i] = (int *)calloc((i + 3), sizeof(int));

  int n, m;
  cin >> n >> m;
  FOR(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    table[a][b]++;
    table[a][b + 1]--;
    table[a + x + 1][b + x + 2]++;
    table[a + x + 1][b]--;
    table[a + x + 2][b + 1]++;
    table[a + x + 2][b + x + 2]--;
  }

  // 右
  FOR(i, 5010) {
    for (int j = 1; j < 5010; j++)
      table[i][j] += table[i][j - 1];
  }
  // 下
  FOR(i, 5010) {
    for (int j = 1; j < 5010; j++)
      table[i + 1][j] += table[i][j];
  }
  // 右下
  FOR(i, n + 1) FOR(j, i + 1) table[i + 1][j + 1] += table[i][j];

  int ans = 0;
  FOR(i, n) FOR(j, i + 1) ans += table[i][j] > 0;
  cout << ans << endl;

  return 0;
}