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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long
const int mod = 1000000007;

int a[200001] = {}, b[200001] = {}, c[200001] = {}, d[200001] = {};
int memo[2001][2001][3] = {};
char w[2001][2001] = {};

signed main() {
  int M, N, K;
  cin >> M >> N >> K;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> w[i][j];
    }
  }
  for (int i = 0; i < K; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (int i = 0; i < M; i++) {
    int t1 = 0, t2 = 0, t3 = 0;
    for (int j = 0; j < N; j++) {
      if (w[i][j] == 'J')
        t1++;
      else if (w[i][j] == 'O')
        t2++;
      else
        t3++;

      if (i == 0) {
        memo[i][j][0] = t1;
        memo[i][j][1] = t2;
        memo[i][j][2] = t3;
      } else {
        memo[i][j][0] = memo[i - 1][j][0] + t1;
        memo[i][j][1] = memo[i - 1][j][1] + t2;
        memo[i][j][2] = memo[i - 1][j][2] + t3;
      }
      // cout << " i" << i << " j" << j << " " << memo[i][j][0] << memo[i][j][1]
      // << memo[i][j][2];
    }
    // cout << endl;
  }
  for (int i = 0; i < K; i++) {
    int a1, a2, a3;
    a1 = memo[c[i] - 1][d[i] - 1][0] - memo[a[i] - 2][d[i] - 1][0] -
         memo[c[i] - 1][b[i] - 2][0] + memo[a[i] - 2][b[i] - 2][0];
    a2 = memo[c[i] - 1][d[i] - 1][1] - memo[a[i] - 2][d[i] - 1][1] -
         memo[c[i] - 1][b[i] - 2][1] + memo[a[i] - 2][b[i] - 2][1];
    a3 = memo[c[i] - 1][d[i] - 1][2] - memo[a[i] - 2][d[i] - 1][2] -
         memo[c[i] - 1][b[i] - 2][2] + memo[a[i] - 2][b[i] - 2][2];
    cout << a1 << " " << a2 << " " << a3 << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long
const int mod = 1000000007;

int a[100000] = {}, b[100000] = {}, c[100000] = {}, d[100000] = {};
int memo[100][100][100][3] = {};
char w[1000][1000] = {};

signed main() {
  int M, N, K;
  cin >> M >> N >> K;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> w[i][j];
    }
  }
  for (int i = 0; i < K; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      int t1 = 0, t2 = 0, t3 = 0;
      for (int k = j; k < N; k++) {
        if (w[i][k] == 'J')
          t1++;
        else if (w[i][k] == 'O')
          t2++;
        else
          t3++;
        memo[j][k][i][0] = t1;
        memo[j][k][i][1] = t2;
        memo[j][k][i][2] = t3;
        //<<"i"<<i<<" j"<<j<<" k"<<k<<" "<< t1 << " " << t2 << " " << t3 <<
        //endl;
      }
    }
  }

  for (int i = 0; i < K; i++) {
    int t1 = 0, t2 = 0, t3 = 0;
    for (int j = a[i] - 1; j < c[i]; j++) {
      t1 += memo[b[i] - 1][d[i] - 1][j][0];
      t2 += memo[b[i] - 1][d[i] - 1][j][1];
      t3 += memo[b[i] - 1][d[i] - 1][j][2];
    }
    cout << t1 << " " << t2 << " " << t3 << endl;
  }
}