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

#include <cstdio>
#include <string>
#include <vector>
using namespace std;

int m, n, k;
vector<string> v;
char t[1001];
int dp[1000][1000][3];

void solve(int a, int b, int c, int d) {
  int j = 0, o = 0, i = 0;
  for (int x = a - 1; x < c; x++) {
    for (int y = b - 1; y < d; y++) {
      if (v[x][y] == 'J')
        j++;
      else if (v[x][y] == 'O')
        o++;
      else
        i++;
    }
  }
  printf("%d %d %d\\n", j, o, i);
}

int main() {
  scanf("%d %d %d", &m, &n, &k);
  for (int i = 0; i < m; i++) {
    scanf("%s", t);
    v.push_back(t);
  }
  /*	int a, b, c, d;
          for(int i=0; i<k; i++){
                  scanf("%d %d %d %d", &a, &b, &c, &d);
                  solve(a, b, c, d);
          }
  */
  if (v[0][0] == 'J')
    dp[0][0][0] = 1;
  else if (v[0][0] == 'O')
    dp[0][0][1] = 1;
  else
    dp[0][0][2] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][0][j] += dp[i - 1][0][j];
    }
    if (v[0][i] == 'J')
      dp[i][0][0] += 1;
    else if (v[0][i] == 'O')
      dp[i][0][1] += 1;
    else
      dp[i][0][2] += 1;
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < 3; j++) {
      dp[0][i][j] += dp[0][i - 1][j];
    }
    if (v[i][0] == 'J')
      dp[0][i][0] += 1;
    else if (v[i][0] == 'O')
      dp[0][i][1] += 1;
    else
      dp[0][i][2] += 1;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      for (int l = 0; l < 3; l++) {
        dp[i][j][l] = dp[i - 1][j][l] + dp[i][j - 1][l] - dp[i - 1][j - 1][l];
      }
      if (v[j][i] == 'J')
        dp[i][j][0] += 1;
      else if (v[j][i] == 'O')
        dp[i][j][1] += 1;
      else
        dp[i][j][2] += 1;
    }
  }
  /*
          for(int i=0; i<m; i++){
                  for(int j=0; j<n; j++){
                          printf("%d\\t", dp[j][i][0]);
                  }
                  printf("\\n");
          }
  */
  int a, b, c, d;
  for (int i = 0; i < k; i++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans[3];
    if (a == 1 && b == 1)
      for (int j = 0; j < 3; j++)
        ans[j] = dp[d - 1][c - 1][j];
    else if (a == 1 && b != 1)
      for (int j = 0; j < 3; j++)
        ans[j] = dp[d - 1][c - 1][j] - dp[b - 2][c - 1][j];
    else if (a != 1 && b == 1)
      for (int j = 0; j < 3; j++)
        ans[j] = dp[d - 1][c - 1][j] - dp[d - 1][a - 2][j];
    else
      for (int j = 0; j < 3; j++)
        ans[j] = dp[d - 1][c - 1][j] - dp[d - 1][a - 2][j] -
                 dp[b - 2][c - 1][j] + dp[b - 2][a - 2][j];
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <string>
#include <vector>
using namespace std;

int m, n, k;
vector<string> v;
char t[1001];
int dp[1000][1000][3];

void solve(int a, int b, int c, int d) {
  int j = 0, o = 0, i = 0;
  for (int x = a - 1; x < c; x++) {
    for (int y = b - 1; y < d; y++) {
      if (v[x][y] == 'J')
        j++;
      else if (v[x][y] == 'O')
        o++;
      else
        i++;
    }
  }
  printf("%d %d %d\\n", j, o, i);
}

int main() {
  scanf("%d %d %d", &m, &n, &k);
  for (int i = 0; i < m; i++) {
    scanf("%s", t);
    v.push_back(t);
  }
  /*	int a, b, c, d;
          for(int i=0; i<k; i++){
                  scanf("%d %d %d %d", &a, &b, &c, &d);
                  solve(a, b, c, d);
          }
  */
  if (v[0][0] == 'J')
    dp[0][0][0] = 1;
  else if (v[0][0] == 'O')
    dp[0][0][1] = 1;
  else
    dp[0][0][2] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][0][j] += dp[i - 1][0][j];
    }
    if (v[0][i] == 'J')
      dp[i][0][0] += 1;
    else if (v[0][i] == 'O')
      dp[i][0][1] += 1;
    else
      dp[i][0][2] += 1;
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < 3; j++) {
      dp[0][i][j] += dp[0][i - 1][j];
    }
    if (v[i][0] == 'J')
      dp[0][i][0] += 1;
    else if (v[i][0] == 'O')
      dp[0][i][1] += 1;
    else
      dp[0][i][2] += 1;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      for (int l = 0; l < 3; l++) {
        dp[i][j][l] = dp[i - 1][j][l] + dp[i][j - 1][l] - dp[i - 1][j - 1][l];
      }
      if (v[j][i] == 'J')
        dp[i][j][0] += 1;
      else if (v[j][i] == 'O')
        dp[i][j][1] += 1;
      else
        dp[i][j][2] += 1;
    }
  }
  /*
          for(int i=0; i<m; i++){
                  for(int j=0; j<n; j++){
                          printf("%d\\t", dp[j][i][0]);
                  }
                  printf("\\n");
          }
  */
  int a, b, c, d;
  for (int i = 0; i < k; i++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans[3];
    if (a == 1 && b == 1)
      for (int j = 0; j < 3; j++)
        ans[j] = dp[d - 1][c - 1][j];
    else
      for (int j = 0; j < 3; j++)
        ans[j] = dp[d - 1][c - 1][j] - dp[d - 1][a - 2][j] -
                 dp[b - 2][c - 1][j] + dp[b - 2][a - 2][j];
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}