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

// c
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; i++)
#define REPR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PB push_back
#define MP make_pair

using namespace std;

static const double PI = acos(-1.0);
static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> pii;

int m, n, k;
vector<string> field;
int sum[3][1024][1024];

int main() {

  cin >> m >> n >> k;

  for (int i = 0; i < m; ++i) {

    string s;
    cin >> s;
    field.push_back(s);
  }

  CLR(sum);

  for (int i = 0; i < m; ++i) {

    int tmp[3] = {0};

    for (int j = 0; j < n; ++j) {

      tmp[(field[i][j] == 'J') ? 0 : ((field[i][j] == 'O') ? 1 : 2)]++;

      for (int k = 0; k < 3; ++k) {
        sum[k][i][j] = tmp[k] + (i > 0 ? sum[k][i - 1][j] : 0);
      }
    }
  }

  for (int i = 0; i < k; ++i) {

    int a, b, c, d, w, x, y, z;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    c--;
    d--;

    for (int t = 0; t < 3; ++t) {

      w = sum[t][c][d];
      x = (a != 0 && b != 0 ? sum[t][a - 1][b - 1] : 0);
      y = (a != 0 ? sum[t][a - 1][d] : 0);
      z = (b != 0 ? sum[t][c][b - 1] : 0);

      cout << w + x - y - z << (t != 2 ? ' ' : '\\n');
    }

    cout << flush;
  }

  return 0;
}

Error: TLE

Fixed submission:

// c
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; i++)
#define REPR(i, b, n) for (int i = n - 1; i >= b; i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PB push_back
#define MP make_pair

using namespace std;

static const double PI = acos(-1.0);
static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> pii;

int m, n, k;
vector<string> field;
int sum[3][1024][1024];

int main() {

  cin >> m >> n >> k;

  for (int i = 0; i < m; ++i) {

    string s;
    cin >> s;
    field.push_back(s);
  }

  CLR(sum);

  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int a = 0; a <= i; ++a) {
        for (int b = 0; b <= j; ++b) {

          int t = (field[a][b] == 'J') ? 0 : ((field[a][b] == 'O') ? 1 : 2);
          sum[t][i][j]++;
        }
      }
    }
  }

  for (int i = 0; i < k; ++i) {

    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    c--;
    d--;

    for (int t = 0; t < 3; ++t) {

      int w, x, y, z;

      w = sum[t][c][d];
      x = (a != 0 && b != 0 ? sum[t][a - 1][b - 1] : 0);
      y = (a != 0 ? sum[t][a - 1][d] : 0);
      z = (b != 0 ? sum[t][c][b - 1] : 0);

      cout << w + x - y - z << (t != 2 ? ' ' : '\\n');
    }

    cout << flush;
  }

  return 0;
}