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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;

int n, m, d, e, f, g, o;
int map_info[1001][1001][3] = {};
int res[3];
string str = "JOI";
string a;
VS ar;
int main() {
  scanf("%d %d", &n, &m);
  scanf("%d", &o);
  for (int i = 0; i < m + 2; i++) {
    a += " ";
  }
  ar.push_back(a);
  for (int i = 0; i < n; i++) {
    cin >> a;
    ar.push_back(" " + a + " ");
  }
  ar.push_back(ar[0]);
  for (int i = 0; i < 3; i++) {
    map_info[1][1][i] = str[i] == ar[1][1];
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < m + 1; j++) {
      if (i == 1 && j == 1) {
        continue;
      }
      for (int k = 0; k < 3; k++) {
        map_info[i][j][k] = map_info[i][j - 1][k] + map_info[i - 1][j][k] -
                            map_info[i - 1][j - 1][k] +
                            (int)(str[k] == ar[i][j]);
      }
    }
  }
  for (int i = 0; i < o; i++) {
    scanf("%d %d %d %d", &d, &e, &f, &g);
    for (int j = 0; j < 3; j++) {
      res[j] = map_info[f][g][j] - map_info[d - 1][g][j] -
               map_info[f][e - 1][j] + map_info[d - 1][e - 1][j];
    }
    printf("%d %d %d\\n", res[0], res[1], res[2]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
using namespace std;
#define MAX_N 1000
#define MAX_M 1000
#define MAX_K 100000
int i, j, m, n, k, Jungle, Ocean, Ice, c, nw[2], se[2];
char str[MAX_M][MAX_N];

// 無駄ソート
int sort(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main() {
  scanf("%d %d", &m, &n);
  scanf("%d", &c);
  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      scanf("%1s", &str[i][j]);
  for (i = 0; i < c; i++) {
    Jungle = 0;
    Ocean = 0;
    Ice = 0;
    scanf("%d %d %d %d", &nw[0], &nw[1], &se[0], &se[1]);
    for (j = nw[0] - 1; j < se[0]; j++) {
      for (k = nw[1] - 1; k < se[1]; k++) {
        if (str[j][k] == 'J')
          Jungle++;
        if (str[j][k] == 'O')
          Ocean++;
        if (str[j][k] == 'I')
          Ice++;
      }
    }
    printf("%d %d %d\\n", Jungle, Ocean, Ice);
  }
  return 0;
}