Problem p00497 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

// n?¬??????????????????????????¬¬????????°???????????????????????¨???????????????????????????
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

signed main() {
  int n, m;

  scanf("%d %d", &n, &m);
  int dp[5002][5002] = {};
  REP(i, m) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    dp[a - 1][b - 1] = max(dp[a - 1][b - 1], c + 1);
  }
  int ans = 0;
  REP(i, n) {
    REP(j, n) {
      if (dp[i][j]) {
        ans++;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - 1);
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] - 1);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

// n?¬??????????????????????????¬¬????????°???????????????????????¨???????????????????????????
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

signed main() {
  int n, m;

  scanf("%d %d", &n, &m);
  vector<vector<int>> dp(5002, vector<int>(5002));
  REP(i, m) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    dp[a - 1][b - 1] = max(dp[a - 1][b - 1], c + 1);
  }
  int ans = 0;
  REP(i, n) {
    REP(j, n) {
      if (dp[i][j]) {
        ans++;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - 1);
        dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] - 1);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}