Problem p00483 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

const int INF = 1e9, N = 1010;
const ll INFL = 1e18;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int n, m, k;
char a[N][N];
int T[N][N];

struct query {
  int X1;
  int Y1;
  int X2;
  int Y2;
  int J;
  int O;
  int I;
} q[N * 10];

void upd(int x, int y) {
  for (int i = x; i <= n; i += i & -i)
    for (int j = y; j <= m; j += j & -j)
      T[i][j]++;
}
int get(int x, int y) {
  int res = 0;
  for (int i = x; i > 0; i -= i & -i)
    for (int j = y; j > 0; j -= j & -j)
      res += T[i][j];
  return res;
}
int get(query qu) {
  return get(qu.X2, qu.Y2) - get(qu.X1 - 1, qu.Y2) - get(qu.X2, qu.Y1 - 1) +
         get(qu.X1 - 1, qu.Y1 - 1);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> k;
  ff(i, 1, n) ff(j, 1, m) cin >> a[i][j];
  ff(i, 1, k) cin >> q[i].X1 >> q[i].Y1 >> q[i].X2 >> q[i].Y2;

  memset(T, 0, sizeof T);
  ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'J') upd(i, j);
  ff(i, 1, k) q[i].J = get(q[i]);

  memset(T, 0, sizeof T);
  ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'O') upd(i, j);
  ff(i, 1, k) q[i].O = get(q[i]);

  memset(T, 0, sizeof T);
  ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'I') upd(i, j);
  ff(i, 1, k) q[i].I = get(q[i]);

  ff(i, 1, k) printf("%d %d %d\n", q[i].J, q[i].O, q[i].I);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

const int INF = 1e9, N = 1010;
const ll INFL = 1e18;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int n, m, k;
char a[N][N];
int T[N][N];

struct query {
  int X1;
  int Y1;
  int X2;
  int Y2;
  int J;
  int O;
  int I;
} q[N * 100];

void upd(int x, int y) {
  for (int i = x; i <= n; i += i & -i)
    for (int j = y; j <= m; j += j & -j)
      T[i][j]++;
}
int get(int x, int y) {
  int res = 0;
  for (int i = x; i > 0; i -= i & -i)
    for (int j = y; j > 0; j -= j & -j)
      res += T[i][j];
  return res;
}
int get(query qu) {
  return get(qu.X2, qu.Y2) - get(qu.X1 - 1, qu.Y2) - get(qu.X2, qu.Y1 - 1) +
         get(qu.X1 - 1, qu.Y1 - 1);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> k;
  ff(i, 1, n) ff(j, 1, m) cin >> a[i][j];
  ff(i, 1, k) cin >> q[i].X1 >> q[i].Y1 >> q[i].X2 >> q[i].Y2;

  memset(T, 0, sizeof T);
  ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'J') upd(i, j);
  ff(i, 1, k) q[i].J = get(q[i]);

  memset(T, 0, sizeof T);
  ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'O') upd(i, j);
  ff(i, 1, k) q[i].O = get(q[i]);

  memset(T, 0, sizeof T);
  ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'I') upd(i, j);
  ff(i, 1, k) q[i].I = get(q[i]);

  ff(i, 1, k) printf("%d %d %d\n", q[i].J, q[i].O, q[i].I);
}