alexjercan/bugnet huggingface dataset:
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int joi2num(char c) {
  if (c == 'J')
    return 0;
  if (c == 'O')
    return 1;
  return 2;
}

int main() {
  int m, n, k;
  static char fld[1024][1024];
  static int sum[1024][1024][3] = {0};
  scanf("%d%d%d", &m, &n, &k);
  for (int x = 0; x < m; ++x)
    scanf("%s", fld[x]);
  for (int i = 0; i < 3; ++i)
    sum[0][0][i] = (i == joi2num(fld[0][0]));
  for (int x = 1; x < m; ++x) {
    for (int i = 0; i < 3; ++i)
      sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
  }
  for (int y = 1; y < n; ++y) {
    for (int i = 0; i < 3; ++i)
      sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
  }
  for (int x = 1; x < m; ++x) {
    for (int y = 1; y < n; ++y) {
      for (int i = 0; i < 3; ++i) {
        int temp = 0;
        temp += sum[x - 1][y][i];
        temp += sum[x][y - 1][i];
        temp -= sum[x - 1][y - 1][i];
        sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    int ax, ay, bx, by;
    scanf("%d%d%d%d", &ax, &ay, &bx, &by);
    ax -= 1, ay -= 1, bx -= 1, by -= 1;
    int ans[3] = {0};
    for (int j = 0; j < 3; ++j) {
      ans[j] += sum[bx][by][j];
      ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
      ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
      ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
    }
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>

int joi2num(char c) {
  if (c == 'J')
    return 0;
  if (c == 'O')
    return 1;
  return 2;
}

int main() {
  int m, n, k;
  char fld[1024][1024];
  int sum[1024][1024][3];
  scanf("%d%d%d", &m, &n, &k);
  for (int x = 0; x < m; ++x)
    scanf("%s", fld[x]);
  for (int i = 0; i < 3; ++i)
    sum[0][0][i] = (i == joi2num(fld[0][0]));
  for (int x = 1; x < m; ++x) {
    for (int i = 0; i < 3; ++i)
      sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
  }
  for (int y = 1; y < n; ++y) {
    for (int i = 0; i < 3; ++i)
      sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
  }
  for (int x = 1; x < m; ++x) {
    for (int y = 1; y < n; ++y) {
      for (int i = 0; i < 3; ++i) {
        int temp = 0;
        temp += sum[x - 1][y][i];
        temp += sum[x][y - 1][i];
        temp -= sum[x - 1][y - 1][i];
        sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    int ax, ay, bx, by;
    scanf("%d%d%d%d", &ax, &ay, &bx, &by);
    ax -= 1, ay -= 1, bx -= 1, by -= 1;
    int ans[3] = {0};
    for (int j = 0; j < 3; ++j) {
      ans[j] += sum[bx][by][j];
      ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
      ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
      ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
    }
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;

const int MAX_M = 1000, MAX_N = 1000;
const int MAX_K = 100000;

int m, n;
int k;
char field[MAX_N + 1];
struct type {
  int j, o, i;
};
type operator+(const type &a, const type &b) {
  type c;
  c.j = a.j + b.j;
  c.o = a.o + b.o;
  c.i = a.i + b.i;
  return c;
}
type operator-(const type &a, const type &b) {
  type c;
  c.j = a.j - b.j;
  c.o = a.o - b.o;
  c.i = a.i - b.i;
  return c;
}
type table[MAX_M + 1][MAX_N + 1];
type res[MAX_K];

int main() {
  scanf("%d %d", &m, &n);
  scanf("%d", &k);
  for (int y = 0; y < m; y++) {
    scanf("%s", field);
    for (int x = 0; x < n; x++) {
      table[y + 1][x + 1] = table[y][x + 1] + table[y + 1][x] - table[y][x];
      switch (field[x]) {
      case 'J':
        table[y + 1][x + 1].j++;
        break;
      case 'O':
        table[y + 1][x + 1].o++;
        break;
      case 'I':
        table[y + 1][x + 1].i++;
        break;
      }
    }
  }
  for (int p = 0; p < k; p++) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    res[p] =
        table[c][d] - table[c][b - 1] - table[a - 1][d] + table[a - 1][b - 1];
  }
  for (int p = 0; p < k; p++) {
    printf("%d %d %d\\n", res[p].j, res[p].o, res[p].i);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

const int MAX_M = 1000, MAX_N = 1000;
const int MAX_K = 100000;

int m, n;
int k;
char field[MAX_M][MAX_N];
int j[MAX_K], o[MAX_K], i[MAX_K];

int main() {
  scanf("%d %d", &m, &n);
  scanf("%d", &k);
  for (int p = 0; p < m; p++) {
    scanf("%s", field[p]);
  }
  for (int p = 0; p < k; p++) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    for (int y = a - 1; y < c; y++) {
      for (int x = b - 1; x < d; x++) {
        switch (field[y][x]) {
        case 'J':
          j[p]++;
          break;
        case 'O':
          o[p]++;
          break;
        case 'I':
          i[p]++;
          break;
        }
      }
    }
  }
  for (int p = 0; p < k; p++) {
    printf("%d %d %d\\n", j[p], o[p], i[p]);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define foru(i, a, b) for (int i = int(a); i <= int(b); i++)
using namespace std;

const int N = 1005;
const int K = 100005;

int n, m, k, ans1[K], ans2[K], ans3[K];
int x[K], y[K], a[K], b[K], t[N][N];
char s[N][N];

int rec(int x, int y, int a, int b) {
  return t[a][b] - t[a][y - 1] - t[x - 1][b] + t[x - 1][y - 1];
}

void sol(char cc, int ans[]) {
  foru(i, 1, n) foru(j, 1, m) t[i][j] = 0;
  foru(i, 1, n) foru(j, 1, m) t[i][j] =
      t[i - 1][j] + t[i][j - 1] - t[i - 1][j - 1] + (s[i - 1][j - 1] == cc);
  foru(i, 1, k) ans[i] = rec(x[i], y[i], a[i], b[i]);
}

int main() {
  // freopen("a.txt", "r", stdin);

  scanf("%d%d%d", &n, &m, &k);
  foru(i, 0, n - 1) scanf("%s", &s[i]);
  foru(i, 1, k) scanf("%d%d%d%d", &x[i], &y[i], &a[i], &b[i]);

  sol('J', ans1);
  sol('O', ans2);
  sol('I', ans3);

  foru(i, 1, k) printf("%d %d %d\\n", ans1[i], ans2[i], ans3[i]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define foru(i, a, b) for (int i = int(a); i <= int(b); i++)
using namespace std;

const int N = 1005;
const int K = 100005;

int n, m, k, ans1[K], ans2[K], ans3[K];
int x[N], y[N], a[N], b[N], t[N][N];
string s[N];
char c[N];

int rec(int x, int y, int a, int b) {
  return t[a][b] - t[a][y - 1] - t[x - 1][b] + t[x - 1][y - 1];
}

void sol(char cc, int ans[]) {
  foru(i, 1, n) foru(j, 1, m) t[i][j] = 0;
  foru(i, 1, n) foru(j, 1, m) t[i][j] =
      t[i - 1][j] + t[i][j - 1] - t[i - 1][j - 1] + (s[i][j] == cc);
  foru(i, 1, k) ans[i] = rec(x[i], y[i], a[i], b[i]);
}

int main() {
  // freopen("a.txt", "r", stdin);

  scanf("%d%d%d", &n, &m, &k);
  foru(i, 1, n) {
    scanf("%s", &c);
    s[i] = c;
    s[i] = '%' + s[i];
  }
  foru(i, 1, k) scanf("%d%d%d%d", &x[i], &y[i], &a[i], &b[i]);
  sol('J', ans1);
  sol('O', ans2);
  sol('I', ans3);
  foru(i, 1, k) printf("%d %d %d\\n", ans1[i], ans2[i], ans3[i]);
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

const int M = 1000;
const int N = 1000;
const int K = 100000;

char joi[M][N];
int jc[M][N] = {};
int oc[M][N] = {};
int ic[M][N] = {};
/*
void check(int a, int b, int c, int d)
{
        int jn = 0;
        int on = 0;
        int in = 0;
        for(int i = a; i <= c; ++i){
                for(int j = b; j <= d; ++j){
                        if(joi[i][j] == 'J') jn++;
                        if(joi[i][j] == 'O') on++;
                        if(joi[i][j] == 'I') in++;
                }
        }
        cout << jn << " " << on << " " << in << endl;
}
*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int m, n, k;
  cin >> m >> n >> k;

  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      char c;
      cin >> c;

      jc[i][j] = jc[i - 1][j] + jc[i][j - 1] - jc[i - 1][j - 1];
      oc[i][j] = oc[i - 1][j] + oc[i][j - 1] - oc[i - 1][j - 1];
      ic[i][j] = ic[i - 1][j] + ic[i][j - 1] - ic[i - 1][j - 1];

      if (c == 'J')
        jc[i][j]++;
      if (c == 'O')
        oc[i][j]++;
      if (c == 'I')
        ic[i][j]++;

      joi[i][j] = c;
      // cout << jc[i][j] << " " << oc[i][j] << " " << ic[i][j] << endl;
    }
  }

  for (int i = 0; i != k; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    cout << jc[c][d] - jc[c][b - 1] - jc[a - 1][d] + jc[a - 1][b - 1] << " "
         << oc[c][d] - oc[c][b - 1] - oc[a - 1][d] + oc[a - 1][b - 1] << " "
         << ic[c][d] - ic[c][b - 1] - ic[a - 1][d] + ic[a - 1][b - 1] << endl;

    // a--, b--, c--, d--;
    // check(a, b, c, d);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

const int M = 1000;
const int N = 1000;
const int K = 100000;

char joi[M][N];

void check(int a, int b, int c, int d) {
  int jn = 0;
  int on = 0;
  int in = 0;
  for (int i = a; i <= c; ++i) {
    for (int j = b; j <= d; ++j) {
      if (joi[i][j] == 'J')
        jn++;
      if (joi[i][j] == 'O')
        on++;
      if (joi[i][j] == 'I')
        in++;
    }
  }
  cout << jn << " " << on << " " << in << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int m, n, k;
  cin >> m >> n >> k;

  for (int i = 0; i != m; ++i) {
    for (int j = 0; j != n; ++j) {
      cin >> joi[i][j];
    }
  }

  for (int i = 0; i != k; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--, b--, c--, d--;
    check(a, b, c, d);
  }

  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int masj[1001][1001];
int maso[1001][1001];
int masi[1001][1001];
int main() {
  int m, n;
  int k;
  memset(masj, 0, sizeof(masj));
  memset(maso, 0, sizeof(maso));
  memset(masi, 0, sizeof(masi));
  cin >> m >> n;
  cin >> k;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      masj[i][j] = masj[i - 1][j] + masj[i][j - 1] - masj[i - 1][j - 1];
      maso[i][j] = maso[i - 1][j] + maso[i][j - 1] - maso[i - 1][j - 1];
      masi[i][j] = masi[i - 1][j] + masi[i][j - 1] - masi[i - 1][j - 1];
      if (c == 'J')
        masj[i][j]++;
      else if (c == 'O')
        maso[i][j]++;
      else if (c == 'I')
        masi[i][j]++;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    cout << masj[c][d] + masj[a - 1][b - 1] - masj[a - 1][d] - masj[c][b - 1]
         << ' ';
    cout << maso[c][d] + maso[a - 1][b - 1] - maso[a - 1][d] - maso[c][b - 1]
         << ' ';
    cout << masi[c][d] + masi[a - 1][b - 1] - masi[a - 1][d] - masi[c][b - 1]
         << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int m, n;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char mas[1001][1001];
char test[1001][1001];
int sx, sy, gx, gy;
int J, O, I;
void dfs(int x, int y) {
  if (mas[x][y] == 'J') {
    J++;
  } else if (mas[x][y] == 'O') {
    O++;
  } else if (mas[x][y] == 'I') {
    I++;
  }
  if (x == gx && y == gy)
    return;
  for (int i = 0; i < 4; i++) {
    int nx = dx[i] + x;
    int ny = dy[i] + y;
    if (sx <= nx && nx <= gx && sy <= ny && ny <= gy && test[nx][ny] == '1') {
      test[nx][ny] = '0';
      dfs(nx, ny);
    }
  }
}
int main() {
  cin >> m >> n;
  int k;
  cin >> k;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> mas[i][j];
    }
  }
  for (int i = 0; i < k; i++) {
    cin >> sx >> sy >> gx >> gy;
    sx--;
    sy--;
    gx--;
    gy--;
    for (int i = sx; i <= gx; i++) {
      for (int j = sy; j <= gy; j++) {
        test[i][j] = '1';
      }
    }

    J = 0;
    O = 0;
    I = 0;
    test[sx][sy] = 0;
    dfs(sx, sy);
    cout << J << " " << O << " " << I << endl;
  }
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int dp[1010][1010][3] = {0};
int main() {
  char tmp[3] = {'J', 'O', 'I'};
  int a, b, n;
  cin >> a >> b >> n;
  vector<string> in(a);
  rep(i, a) cin >> in[i];
  loop(i, 1, a + 1) loop(j, 1, b + 1) rep(k, 3) {
    dp[i][j][k] = dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
    if ((char)in[i - 1][j - 1] == tmp[k])
      dp[i][j][k]++;
  }
  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int out[3] = {0};
    rep(j, 3) out[j] =
        dp[c][d][j] - dp[c][b - 1][j] - dp[a - 1][d][j] + dp[a - 1][b - 1][j];
    cout << out[0] << " " << out[1] << " " << out[2] << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  char tmp[3] = {'J', 'O', 'I'};
  int a, b, n;
  cin >> a >> b >> n;
  vector<string> in(a);
  int dp[1010][1010][3] = {0};
  rep(i, a) cin >> in[i];
  loop(i, 1, a + 1) loop(j, 1, b + 1) rep(k, 3) {
    dp[i][j][k] = dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
    if ((char)in[i - 1][j - 1] == tmp[k])
      dp[i][j][k]++;
  }
  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int out[3] = {0};
    rep(j, 3) out[j] =
        dp[c][d][j] - dp[c][b - 1][j] - dp[a - 1][d][j] + dp[a - 1][b - 1][j];
    cout << out[0] << " " << out[1] << " " << out[2] << endl;
  }
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int n, m, q, a, b, c, d, rui[1005][1005][3], ret[3];
char f[1005][1005], e[3] = {'J', 'O', 'I'};
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    scanf("%s", &f[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 0; k < 3; k++)
        rui[i][j][k] = rui[i - 1][j][k] + rui[i][j - 1][k] -
                       rui[i - 1][j - 1][k] + (f[i - 1][j - 1] == e[k]);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    for (int j = 0; j < 3; j++)
      ret[j] = rui[c][d][j] - rui[c][b - 1][j] - rui[a - 1][d][j] +
               rui[a - 1][b - 1][j];
    printf("%d %d %d\\n", ret[0], ret[1], ret[2]);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

char T[1005];
int sum[3][1005][1005];

int main() {
  int m, n, k;
  scanf("%d%d%d", &m, &n, &k);
  for (int i = 0; i < m; i++) {
    scanf("%s", T);
    for (int j = 0; j < n; j++) {
      if (T[j] == 'J')
        sum[0][i][j]++;
      else if (T[j] == 'O')
        sum[1][i][j]++;
      else
        sum[2][i][j]++;
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 1; j < m; j++) {
      for (int t = 0; t < n; t++) {
        sum[i][j][t] += sum[i][j - 1][t];
      }
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < m; j++) {
      for (int t = 1; t < n; t++) {
        sum[i][j][t] += sum[i][j][t - 1];
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    a--;
    b--;
    c--;
    d--;
    int ans[3];
    for (int j = 0; j < 3; j++)
      ans[j] = sum[j][c][d] - sum[j][c][b - 1] - sum[j][a - 1][d] +
               sum[j][a - 1][b - 1];
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 100000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

char T[1000];
int sum[3][1000][1000];

int main() {
  int m, n, k;
  scanf("%d%d%d", &m, &n, &k);
  rep(i, m) {
    scanf("%s", T);
    rep(j, n) {
      switch (T[j]) {
      case 'J':
        sum[0][i][j]++;
        break;
      case 'O':
        sum[1][i][j]++;
        break;
      case 'I':
        sum[2][i][j]++;
        break;
      }
    }
  }
  rep(i, 3) for (int j = 1; j < m; j++) rep(t, n) sum[i][j][t] +=
      sum[i][j - 1][t];
  rep(i, 3) rep(j, m) for (int t = 1; t < n; t++) sum[i][j][t] +=
      sum[i][j][t - 1];
  rep(i, k) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    a--;
    b--;
    c--;
    d--;
    rep(i, 3) {
      int ans = sum[i][c][d];
      if (b)
        ans -= sum[i][c][b - 1];
      if (a)
        ans -= sum[i][a - 1][d];
      if (a && b)
        ans += sum[i][a - 1][b - 1];
      printf(i == 2 ? "%d\\n" : "%d ", ans);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

char T[1005];
int sum[3][1005][1005];

int main(void) {
  int m, n, k;
  scanf("%d%d%d", &m, &n, &k);
  for (int i = 0; i < m; i++) {
    scanf("%s", T);
    for (int j = 0; j < n; j++) {
      if (T[j] == 'J')
        sum[0][i][j]++;
      else if (T[j] == 'O')
        sum[1][i][j]++;
      else
        sum[2][i][j]++;
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 1; j < m; j++) {
      for (int t = 0; t < n; t++) {
        sum[i][j][t] += sum[i][j - 1][t];
      }
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < m; j++) {
      for (int t = 1; t < n; t++) {
        sum[i][j][t] += sum[i][j][t - 1];
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    a--;
    b--;
    c--;
    d--;
    int ans[3];
    for (int j = 0; j < 3; j++)
      ans[j] = sum[j][c][d] - sum[j][c][b - 1] - sum[j][a - 1][d] +
               sum[j][a - 1][b - 1];
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int M, N, K;
char map[1010][1010];
int jt[1010][1010];
int ot[1010][1010];
int it[1010][1010];

int main() {
  cin >> M >> N >> K;
  for (int i = 0; i < M; i++) {
    int hj = 0, ho = 0, hi = 0;
    for (int j = 0; j < N; j++) {
      cin >> map[i][j];
      if (i == 0 && j >= 1) {
        jt[i][j] = jt[i][j - 1];
        ot[i][j] = ot[i][j - 1];
        it[i][j] = it[i][j - 1];
      }
      if (i > 0) {
        if (j == 0)
          jt[i][j] = jt[i - 1][j];
        else
          jt[i][j] = jt[i - 1][j] + hj;

        if (j == 0)
          ot[i][j] = ot[i - 1][j];
        else
          ot[i][j] = ot[i - 1][j] + ho;

        if (j == 0)
          it[i][j] = it[i - 1][j];
        else
          it[i][j] = it[i - 1][j] + hi;
      }

      char c = map[i][j];
      if (c == 'J') {
        jt[i][j]++;
        hj++;
      }
      if (c == 'O') {
        ot[i][j]++;
        ho++;
      }
      if (c == 'I') {
        it[i][j]++;
        hi++;
      }
    }
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a -= 1;
    b -= 1;
    c -= 1;
    d -= 1;

    int J = 0;
    int O = 0;
    int I = 0;
    cout << jt[c][d] - (a > 0 ? jt[a - 1][d] : 0) - (b > 0 ? jt[c][b - 1] : 0) +
                (a > 0 & b > 0 ? jt[a - 1][b - 1] : 0)
         << " "
         << ot[c][d] - (a > 0 ? ot[a - 1][d] : 0) - (b > 0 ? ot[c][b - 1] : 0) +
                (a > 0 & b > 0 ? ot[a - 1][b - 1] : 0)
         << " "
         << it[c][d] - (a > 0 ? it[a - 1][d] : 0) - (b > 0 ? it[c][b - 1] : 0) +
                (a > 0 & b > 0 ? it[a - 1][b - 1] : 0)
         << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int M, N, K;
char map[1010][1010];

int main() {
  cin >> M >> N >> K;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> map[i][j];
    }
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int J = 0, O = 0, I = 0;
    for (int x = b - 1; x < d; x++) {
      for (int y = a - 1; y < c; y++) {
        // cout << "DEBUG: (" << x << ", "<<y <<")\\n";
        char c = map[y][x];
        if (c == 'J')
          J++;
        if (c == 'O')
          O++;
        if (c == 'I')
          I++;
      }
    }
    cout << J << " " << O << " " << I << "\\n";
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int JOI[1024][1024][3];
char m[1024][1024];
int main() {
  int W, H, K, x, y, k, a, b, c, d;
  scanf("%d%d%d", &H, &W, &K);
  for (y = 0; y < H; ++y)
    scanf("%s", m[y]);
  for (y = 0; y < H; ++y)
    for (x = 0; x < W; ++x) {
      for (k = 0; k < 3; ++k)
        JOI[y + 1][x + 1][k] =
            JOI[y + 1][x][k] + JOI[y][x + 1][k] - JOI[y][x][k];
      JOI[y + 1][x + 1][c = m[y][x], c == 'J' ? 0 : c == 'O' ? 1 : 2]++;
    }
  while (K--) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
#define P(k)                                                                   \\
  JOI[c][d][k] + JOI[a - 1][b - 1][k] - JOI[a - 1][d][k] - JOI[c][b - 1][k]
    printf("%d %d %d\\n", P(0), P(1), P(2));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using std::min;
const int N = 100;
char m[1024][1024];
int RMQ[1000 / N + 10][1000 / N + 10][3];
int h, w;
void F(int a, int b, int c, int d, int &J, int &O, int &I) {
  // printf("a:%d, b:&%d, c:%d, d:%d\\n",a,b,c,d);
  for (int y = a; y < c; ++y)
    for (int x = b; x < d; ++x)
      if (m[y][x] == 'J')
        ++J;
      else if (m[y][x] == 'O')
        ++O;
      else
        ++I;
}
int main() {
  int x, y, x2, y2, k, a, b, c, d, J, O, I;
  scanf("%d%d%d", &h, &w, &k);
  for (y = 0; y < h; ++y)
    scanf("%s", m[y]);

  for (y = 0; y + N < h; y += N)
    for (x = 0; x + N < w; x += N)
      F(y, x, y + N, x + N, RMQ[y][x][0], RMQ[y][x][1], RMQ[y][x][2]);

  while (k--) {
    J = O = I = 0;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    --a, --b;
    for (y = a, y2 = min(a / N * N + N, c); y < c; y = y2, y2 = min(y2 + N, c))
      for (x = b, x2 = min(b / N * N + N, d); x < d;
           x = x2, x2 = min(x2 + N, d)) {
        if (y % N == 0 && y2 % N == 0 && x % N == 0 && x2 % N == 0)
          J += RMQ[y][x][0], O += RMQ[y][x][1], I += RMQ[y][x][2];
        else
          F(y, x, y2, x2, J, O, I);
      }
    printf("%d %d %d\\n", J, O, I);
  }

  return 0;
}