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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
int n, m;
// int *nail[5005];
vector<int> nail[5005];
int main(void) {
  scanf("%d %d", &n, &m);

  /*
  for(int i=0;i<=n+3;i++){
    nail[i] = (int*)calloc(i+3,sizeof(int));
  }
  */

  for (int i = 0; i <= n + 3; i++) {
    nail[i].resize(i + 3, 0);
  }

  for (int i = 0; i < m; i++) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    nail[a][b]++;
    nail[a][b + 1]--;
    nail[a + x + 1][b]--;
    nail[a + x + 2][b + 1]++;
    nail[a + x + 1][b + x + 2]++;
    nail[a + x + 2][b + x + 2]--;
  }

  /*
  for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  // 横
  for (int i = 1; i <= n + 2; i++) {
    for (int j = 1; j <= i + 2; j++) {
      nail[i][j] += nail[i][j - 1];
    }
  }

  /*
   for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  // 縦
  for (int i = 1; i <= n + 2; i++) {
    for (int j = i; j <= n + 2; j++) {
      nail[j][i] += nail[j - 1][i];
    }
  }

  /*
   for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  int ret = 0;
  // 斜め
  for (int i = 1; i <= n; i++) {
    if (nail[i][1] > 0)
      ret++;
    for (int j = 1; j <= n - i + 1; j++) {
      nail[i + j][j + 1] += nail[i + j - 1][j];
      if (nail[i + j][j + 1] > 0)
        ret++;
    }
  }

  /*
   for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  /*
  for(int i=1;i<=n;i++){
    for(int j=1;j<=i;j++){
      if(nail[i][j]>0){
        ret++;
      }
    }
  }
  */

  printf("%d\\n", ret);

  /*
  for(int i=0;i<=n+5;i++){
    free(nail[i]);
  }
  */
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
int n, m;
int *nail[5005];
int main(void) {
  scanf("%d %d", &n, &m);

  for (int i = 0; i <= n + 3; i++) {
    nail[i] = (int *)calloc(i + 3, sizeof(int));
  }

  for (int i = 0; i < m; i++) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    nail[a][b]++;
    nail[a][b + 1]--;
    nail[a + x + 1][b]--;
    nail[a + x + 2][b + 1]++;
    nail[a + x + 1][b + x + 2]++;
    nail[a + x + 2][b + x + 2]--;
  }

  /*
  for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  // 横
  for (int i = 1; i <= n + 2; i++) {
    for (int j = 1; j <= i + 2; j++) {
      nail[i][j] += nail[i][j - 1];
    }
  }

  /*
   for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  // 縦
  for (int i = 1; i <= n + 2; i++) {
    for (int j = i; j <= n + 2; j++) {
      nail[j][i] += nail[j - 1][i];
    }
  }

  /*
   for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  int ret = 0;
  // 斜め
  for (int i = 1; i <= n; i++) {
    if (nail[i][1] > 0)
      ret++;
    for (int j = 1; j <= n - i + 1; j++) {
      nail[i + j][j + 1] += nail[i + j - 1][j];
      if (nail[i + j][j + 1] > 0)
        ret++;
    }
  }

  /*
   for(int i=1;i<=n+1;i++){
    for(int j=1;j<=n+1;j++){
      printf("%2d",nail[i][j]);
    }
    puts("");
  }
  puts("");
  */

  /*
  for(int i=1;i<=n;i++){
    for(int j=1;j<=i;j++){
      if(nail[i][j]>0){
        ret++;
      }
    }
  }
  */

  printf("%d\\n", ret);

  for (int i = 0; i <= n + 5; i++) {
    free(nail[i]);
  }
}