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

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;

int main() {
  int m, n, k;
  cin >> m >> n >> k;
  VVI map_j(n + 1, VI(m + 1, 0));
  VVI map_o(n + 1, VI(m + 1, 0));
  VVI map_i(n + 1, VI(m + 1, 0));
  VVI ans;
  for (int y = 1; y <= m; y++) {
    for (int x = 1; x <= n; x++) {
      char temp;
      cin >> temp;
      map_j[x][y] = map_j[x - 1][y];
      map_o[x][y] = map_o[x - 1][y];
      map_i[x][y] = map_i[x - 1][y];
      switch (temp) {
      case 'J':
        map_j[x][y] = 1 + map_j[x - 1][y];
        break;
      case 'O':
        map_o[x][y] = 1 + map_o[x - 1][y];
        break;
      case 'I':
        map_i[x][y] = 1 + map_i[x - 1][y];
        break;
      }
    }
  }
  for (int x = 1; x <= n; x++) {
    for (int y = 1; y <= m; y++) {
      map_j[x][y] += map_j[x][y - 1];
      map_o[x][y] += map_o[x][y - 1];
      map_i[x][y] += map_i[x][y - 1];
    }
  }
  /*for(int y=0;y<=m;y++) {
          for(int x=0;x<=n;x++) {
                  cout << map_j[x][y] << " ";
          }
          cout << endl;
          }*/
  // return 0;
  for (int i = 0; i < k; i++) {
    int x0, y0, x1, y1;
    cin >> y0 >> x0 >> y1 >> x1;
    int ans_j = map_j[x1][y1] - map_j[x1][y0 - 1] - map_j[x0 - 1][y1] +
                map_j[x0 - 1][y0 - 1];
    int ans_o = map_o[x1][y1] - map_o[x1][y0 - 1] - map_o[x0 - 1][y1] +
                map_o[x0 - 1][y0 - 1];
    int ans_i = map_i[x1][y1] - map_i[x1][y0 - 1] - map_i[x0 - 1][y1] +
                map_i[x0 - 1][y0 - 1];
    VI e_ans;
    e_ans.push_back(ans_j);
    e_ans.push_back(ans_o);
    e_ans.push_back(ans_i);
    ans.push_back(e_ans);
  }
  for (auto it = ans.begin(); it != ans.end(); it++) {
    cout << (*it)[0] << " ";
    cout << (*it)[1] << " ";
    cout << (*it)[2] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

enum class Land {
  J,
  O,
  I,
};

using mat2 = std::vector<std::vector<Land>>;

std::tuple<int, int, int> assess(mat2 const &l, int a, int b, int c, int d);

int main(void) {
  int M, N, K;
  std::cin >> M >> N >> K;

  mat2 l;
  l.resize(M);
  for (auto &x : l) {
    x.resize(N);
  }

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      char tmp;
      std::cin >> tmp;
      switch (tmp) {
      case 'J':
        l[i][j] = Land::J;
        break;
      case 'O':
        l[i][j] = Land::O;
        break;
      case 'I':
        l[i][j] = Land::I;
        break;
      }
    }
  }

  for (int k = 0; k < K; k++) {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    auto s = assess(l, a - 1, b - 1, c - 1, d - 1);
    std::cout << std::get<0>(s) << " " << std::get<1>(s) << " "
              << std::get<2>(s) << std::endl;
  }
  return 0;
}

std::tuple<int, int, int> assess(mat2 const &l, int a, int b, int c, int d) {
  int J = 0, O = 0, I = 0;

  for (int i = a; i <= c; i++) {
    for (int j = b; j <= d; j++) {
      switch (l[i][j]) {
      case Land::J:
        J++;
        break;
      case Land::O:
        O++;
        break;
      case Land::I:
        I++;
        break;
      }
    }
  }
  return std::make_tuple(J, O, I);
}