Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int ans[1001][1001][3];
char m[3] = {'J', 'O', 'I'};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char z;
  for (int i = 0; i < h; i++)
    for (int r = 0; r < 3; r++)
      ans[i][0][r] = 0;
  for (int j = 0; j < w; j++)
    for (int r = 0; r < 3; r++)
      ans[0][j][r] = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> z;
      for (int r = 0; r < 3; r++) {
        ans[i][j][r] =
            ans[i - 1][j][r] + ans[i][j - 1][r] - ans[i - 1][j - 1][r];
        if (m[r] == z)
          ans[i][j][r]++;
      }
    }
  }
  int x[2], y[2];
  for (int u = 0; u < k; u++) {
    for (int i = 0; i < 2; i++)
      cin >> y[i] >> x[i];
    for (int r = 0; r < 3; r++) {
      if (r)
        cout << " ";
      int cnt = ans[y[1]][x[1]][r] + ans[y[0] - 1][x[0] - 1][r];
      cnt -= ans[y[1]][x[0] - 1][r] + ans[y[0] - 1][x[1]][r];
      cout << cnt;
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
pair<int, int> J[1000001];
pair<int, int> O[1000001];
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char z;
  int jtop = 0, otop = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> z;
      if (z == 'J') {
        J[jtop].first = i;
        J[jtop].second = j;
        jtop++;
      }
      if (z == 'O') {
        O[otop].first = i;
        O[otop].second = j;
        otop++;
      }
    }
  }
  int x[2], y[2];
  for (int u = 0; u < k; u++) {
    for (int i = 0; i < 2; i++) {
      cin >> y[i] >> x[i];
      y[i]--;
      x[i]--;
    }

    int left = 0, right = jtop, midl;
    while (true) {
      if (left >= right - 1)
        break;
      midl = (right + left) / 2;
      if (J[midl].first < y[0])
        left = midl;
      else
        right = midl;
    }
    left = 0, right = jtop;
    int midr;
    while (true) {
      if (left >= right - 1)
        break;
      midr = (right + left) / 2;
      if (J[midr].first <= y[1])
        left = midr;
      else
        right = midr;
    }
    if (midr == jtop - 1)
      midr++;
    int jans = 0;
    for (int i = max(0, midl - 1); i <= min(midr + 1, jtop - 1); i++) {
      if (J[i].second >= x[0] && J[i].second <= x[1]) {
        if (J[i].first >= y[0] && J[i].first <= y[1])
          jans++;
      }
    }

    left = 0, right = otop;
    while (true) {
      if (left >= right - 1)
        break;
      midl = (right + left) / 2;
      if (O[midl].first < y[0])
        left = midl;
      else
        right = midl;
    }
    left = 0, right = otop;
    while (true) {
      if (left >= right - 1)
        break;
      midr = (right + left) / 2;
      if (O[midr].first <= y[1])
        left = midr;
      else
        right = midr;
    }
    if (midr == otop - 1)
      midr++;
    int oans = 0;
    for (int i = max(midl - 1, 0); i <= min(midr + 1, otop - 1); i++) {
      if (O[i].second >= x[0] && O[i].second <= x[1]) {
        if (O[i].first >= y[0] && O[i].first <= y[1])
          oans++;
      }
    }

    int s = (x[1] - x[0] + 1) * (y[1] - y[0] + 1);
    cout << jans << " " << oans << " " << s - jans - oans << endl;
  }
  return 0;
}