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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int M, N, K;
const int MAX_M = 1010;
const int MAX_N = 1010;

char board[MAX_M][MAX_N];
char c;
int a[1010][1010]; // 長方形内のJの数
int b[1010][1010]; // 長方形内のOの数
char s[1010];
int main() {
  scanf("%d%d%d", &M, &N, &K);
  for (int i = 1; i <= M; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= N; j++) {
      if (s[j] == 'J') {
        a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + 1;
        b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
      } else if (s[j] == 'O') {
        a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + 1;
      } else {
        a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
      }
    }
  }
  int s, t, u, v;
  for (int l = 0; l < K; l++) {
    int ansJ, ansO, ansI;
    scanf("%d%d%d%d", &s, &t, &u, &v);
    s--;
    t--;
    ansJ = a[u][v] - a[s][v] - a[u][t] + a[s][t];
    ansO = b[u][v] - b[s][v] - b[u][t] + b[s][t];
    ansI = (u - s) * (v - t) - (ansJ + ansO);
    printf("%d %d %d\\n", ansJ, ansO, ansI);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int M, N, K;
const int MAX_M = 1010;
const int MAX_N = 1010;

char board[MAX_M][MAX_N];
char c;
int main() {
  scanf("%d %d", &M, &N);
  scanf("%d", &K);
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> c;
      board[i][j] = c;
    }
  }
  int a, b, c, d;
  for (int l = 0; l < K; l++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);

    int ansJ = 0;
    int ansO = 0;
    int ansI = 0;
    for (int i = a; i <= c; i++) {
      for (int j = b; j <= d; j++) {
        if (board[i][j] == 'J')
          ansJ++;
        else if (board[i][j] == 'O')
          ansO++;
        else if (board[i][j] == 'I')
          ansI++;
      }
    }
    printf("%d %d %d\\n", ansJ, ansO, ansI);
  }
  return 0;
}