alexjercan/bugnet huggingface dataset:
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  std::vector<int> number;
  int inputNum, tmp;

  std::cin >> inputNum;

  for (int i = 0; i < inputNum; ++i) {
    std::cin >> tmp;
    number.push_back(tmp);
  }

  std::vector<std::vector<long int>> vec(inputNum + 1,
                                         std::vector<long int>(21, 0));

  vec[0][number[0]] = 1;

  for (int i = 0; i < inputNum; ++i) {
    for (int j = 0; j < 21; j++) {
      if (vec[i][j] != 0) {
        if (j + number[i + 1] <= 20) {
          vec[i + 1][j + number[i + 1]] += vec[i][j];
        }
        if (j - number[i + 1] >= 0) {
          vec[i + 1][j - number[i + 1]] += vec[i][j];
        }
      }
    }
  }

  std::cout << vec[inputNum - 2][number[inputNum - 1]] << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

// num:my number, depth
long int node(std::vector<int> number, int sum, int depth, bool ope) {
  long int ans_num = 0;

  if (depth == number.size() - 1) {
    if (sum == number[depth])
      return 1;
    else
      return 0;
  }

  if (ope)
    sum += number[depth];
  else
    sum -= number[depth];

  if (sum > 20)
    return 0;
  else if (sum < 0)
    return 0;

  // plus
  ans_num += node(number, sum, depth + 1, 1);
  // negative
  ans_num += node(number, sum, depth + 1, 0);

  return ans_num;
}

int main() {
  long int ans_num = 0;
  std::vector<int> number;
  int input_num, tmp;

  std::cin >> input_num;

  for (int i = 0; i < input_num; ++i) {
    std::cin >> tmp;
    number.push_back(tmp);
  }

  ans_num += node(number, number[0], 1, 1);

  ans_num += node(number, number[0], 1, 0);

  std::cout << ans_num / 2 << std::endl;

  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>
using namespace std;

inline bool valid(int x) { return 0 <= x && x <= 20; }

int main() {
  int N;
  scanf("%d\\n", &N);

  vector<int> a(N);
  for (int i = 0; i < N; i++)
    scanf("%d", &a[i]);
  int answer = a.back();
  a.pop_back();

  vector<vector<long long>> dp(a.size(), vector<long long>(21, 0));

  dp[0][a[0]] = 1;

  // 左からi番目の穴に+か-を入れてjにするパターンは
  // a[i] + これまで = j or これまで - a[i] = j となる時のどちらか
  //<=> これまで = j - a[i] or これまで = j + a[i]
  // よってこれまでのパターン == dp[i - 1][j - a[i]] + dp[i - 1][j + a[i]]

  for (int i = 1; i < dp.size(); i++) {
    for (int j = 0; j < 21; j++) {
      if (valid(j - a[i]))
        dp[i][j] += dp[i - 1][j - a[i]];
      if (valid(j + a[i]))
        dp[i][j] += dp[i - 1][j + a[i]];
    }
  }

  printf("%lld\\n", dp[dp.size() - 1][answer]);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define MAX 110

long long int dp[MAX][MAX];
int input[MAX];
int target;
int n;

// 再起関数
long long int dfs(int now, int sum, int ans) {
  if (now == n - 1) {
    if (sum == target) {
      return ans + 1;
    }
    return ans;
  }
  if (sum > 20 || 0 > sum) {
    return ans;
  }

  ans = dfs(now + 1, sum + input[now], ans);
  ans = dfs(now + 1, sum - input[now], ans);

  return ans;
}

int main(void) {
  long long int ans = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> input[i];
  }
  target = input[n - 1];

  ans = dfs(0, 0, 0);

  cout << ans << endl;

  return 0;
}
Problem p00482 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define reps(i, j, n) for (int i = j; i < n; ++i)
#define rep(i, n) reps(i, 0, n)
int dp[2][1 << 20][2] = {{{1}}};
int main() {
  int h, w, next = 1, cur = 0, mod = 100000, ans = 1, mkbit, mki;
  char mas[20][20], tmp[] = "JOI";
  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> mas[i][j];
  rep(x, h) {
    rep(y, w) {
      fill_n(dp[next][0], 2 * (1 << 20), 0);
      rep(bit, 1 << w) rep(i, 2) if (dp[cur][bit][i]) rep(j, 3) {
        mkbit = bit & ~(1 << y), mki = (j == 0);
        if (mas[x][y] != '?' && mas[x][y] != tmp[j])
          continue;
        if ((bit & 1 << y) && j == 2)
          continue;
        if (i == 1 && j == 1)
          mkbit |= 1 << y - 1;
        dp[next][mkbit][mki] += dp[cur][bit][i];
        if (dp[next][mkbit][mki] >= mod)
          dp[next][mkbit][mki] -= mod;
      }
      swap(next, cur);
    }
    rep(bit, 1 << w) {
      dp[cur][bit][0] += dp[cur][bit][1];
      if (dp[cur][bit][0] >= mod)
        dp[cur][bit][0] -= mod;
      dp[cur][bit][1] = 0;
    }
  }
  rep(i, h) rep(j, w) if (mas[i][j] == '?') ans = (ans * 3) % mod;
  rep(bit, 1 << w) ans = (ans - dp[cur][bit][0] + mod) % mod;
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define reps(i, j, n) for (int i = j; i < n; ++i)
#define rep(i, n) reps(i, 0, n)
int main() {
  int h, w, dp[2][1 << 20][2] = {{{1}}}, next = 1, cur = 0, mod = 100000,
                       ans = 1, mkbit, mki;
  char mas[20][20], tmp[] = "JOI";
  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> mas[i][j];
  rep(x, h) {
    rep(y, w) {
      fill_n(dp[next][0], 2 * (1 << 20), 0);
      rep(bit, 1 << w) rep(i, 2) if (dp[cur][bit][i]) rep(j, 3) {
        mkbit = bit & ~(1 << y), mki = (j == 0);
        if (mas[x][y] != '?' && mas[x][y] != tmp[j])
          continue;
        if ((bit & 1 << y) && j == 2)
          continue;
        if (i == 1 && j == 1)
          mkbit |= 1 << y - 1;
        dp[next][mkbit][mki] += dp[cur][bit][i];
        if (dp[next][mkbit][mki] >= mod)
          dp[next][mkbit][mki] -= mod;
      }
      swap(next, cur);
    }
    rep(bit, 1 << w) {
      dp[cur][bit][0] += dp[cur][bit][1];
      if (dp[cur][bit][0] >= mod)
        dp[cur][bit][0] -= mod;
      dp[cur][bit][1] = 0;
    }
  }
  rep(i, h) rep(j, w) if (mas[i][j] == '?') ans = (ans * 3) % mod;
  rep(bit, 1 << w) ans = (ans - dp[cur][bit][0] + mod) % mod;
  cout << ans << endl;
}
Problem p00482 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 100000
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int dp[2][1 << 19];
char s[21][21];
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  int cnt = 1;
  rep(i, m) scanf("%s", s[i]);
  rep(i, m) rep(j, n) {
    if (s[i][j] == '?')
      cnt = (cnt * 3) % MOD;
  }
  int *now = dp[0], *nxt = dp[1];
  now[0] = 1;
  rep(i, m) rep(j, n) {
    fill(nxt, nxt + (1 << n - 1), 0);
    rep(k, 1 << n - 1) {
      if (now[k] == 0)
        continue;
      int a = k & ~(1 << (j - 1));
      if (s[i][j] == 'J' || s[i][j] == '?') {
        if (j < n - 1)
          (nxt[a | 1 << j] += now[k]) %= MOD;
        else
          (nxt[a & ~(1 << j)] += now[k]) %= MOD;
      }
      if (s[i][j] == 'O' || s[i][j] == '?')
        (nxt[k & ~(1 << j)] += now[k]) %= MOD;
      if (s[i][j] == 'I' || s[i][j] == '?') {
        if (!(k >> j & 1))
          (nxt[a & ~(1 << j)] += now[k]) %= MOD;
      }
    }
    swap(now, nxt);
  }
  rep(i, 1 << n - 1) cnt = (cnt + MOD - now[i]) % MOD;
  printf("%d\\n", cnt);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 100000
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int dp[2][1 << 19];
char s[21][21];
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  int cnt = 1;
  rep(i, m) scanf("%s", s[i]);
  rep(i, m) rep(j, n) {
    if (s[i][j] == '?')
      cnt = (cnt * 3) % MOD;
  }
  int *now = dp[0], *nxt = dp[1];
  now[0] = 1;
  rep(i, m) rep(j, n) {
    fill(nxt, nxt + (1 << n), 0);
    rep(k, 1 << n - 1) {
      if (now[k] == 0)
        continue;
      int a = k & ~(1 << (j - 1));
      if (s[i][j] == 'J' || s[i][j] == '?') {
        if (j < n - 1)
          (nxt[a | 1 << j] += now[k]) %= MOD;
        else
          (nxt[a & ~(1 << j)] += now[k]) %= MOD;
      }
      if (s[i][j] == 'O' || s[i][j] == '?')
        (nxt[k & ~(1 << j)] += now[k]) %= MOD;
      if (s[i][j] == 'I' || s[i][j] == '?') {
        if (!(k >> j & 1))
          (nxt[a & ~(1 << j)] += now[k]) %= MOD;
      }
    }
    swap(now, nxt);
  }
  rep(i, 1 << n) cnt = (cnt + MOD - now[i]) % MOD;
  printf("%d\\n", cnt);
}
Problem p00482 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long lli;
const int MAXH = 21;
const int MAXW = 21;
const lli mod = 100000LL;
const string C = "JOI";

int H, W;
char G[MAXH][MAXW];
map<int, lli> dp[2];

int main() {
  lli all = 1;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
      if (G[i][j] == '?') {
        all = (all * 3) % mod;
      }
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      bool p = (i * W + j) % 2;
      dp[!p].clear();
      for (map<int, lli>::iterator it = dp[p].begin(); it != dp[p].end();
           ++it) {
        int b = it->first;
        for (int k = 0; k < 3; ++k) {
          if (G[i][j] == '?' || G[i][j] == C[k]) {
            int nb = b & ~(1 << j);
            if (C[k] == 'J') {
              nb &= ~(1 << (j - 1));
              if (j + 1 != W)
                nb |= (1 << j);
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'O') {
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'I') {
              if (!(b & (1 << j))) {
                nb &= ~(1 << (j - 1));
                dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
              }
            }
          }
        }
      }
    }
  }
  lli sum = 0;
  for (map<int, lli>::iterator it = dp[H * W % 2].begin();
       it != dp[H * W % 2].end(); ++it) {
    sum = (sum + it->second) % mod;
  }
  cout << (all - sum + mod) % mod << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long lli;
const int MAXH = 21;
const int MAXW = 21;
const lli mod = 100000LL;
const string C = "JOI";

int H, W;
char G[MAXH][MAXW];
map<int, lli> dp[2];

int main() {
  lli all = 1;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
      if (G[i][j] == '?') {
        all = (all * 3) % mod;
      }
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      bool p = (i * W + j) % 2;
      dp[!p].clear();
      for (map<int, lli>::iterator it = dp[p].begin(); it != dp[p].end();
           ++it) {
        int b = it->first;
        for (int k = 0; k < 3; ++k) {
          if (G[i][j] == '?' || G[i][j] == C[k]) {
            int nb = b & ~(1 << j);
            if (C[k] == 'J') {
              nb &= ~(1 << (j - 1));
              if (j + 1 != W)
                nb |= (1 << j);
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'O') {
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'I') {
              if (!(b & (1 << j))) {
                nb &= ~(1 << (j - 1));
                dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
              }
            }
          }
        }
      }
    }
  }
  lli sum = 0;
  for (int b = 0; b < (1 << W); ++b) {
    sum = (sum + dp[H * W % 2][b]) % mod;
  }
  cout << (all - sum + mod) % mod << endl;
  return 0;
}
Problem p00482 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
int m, n, ans;
int dp[2][1 << 19][2], fill, all = 1;
char c;
int main() {
  scanf("%d%d", &m, &n);
  fill = (1 << (n - 1)) - 1;
  dp[1][0][0] = 1;
  for (int i = 0; i < m * n; i++) {
    scanf(" %c", &c);
    if (c == '?') {
      all *= 3;
      all %= 100000;
    }
    memset(dp[i % 2], 0, sizeof(dp[i % 2]));
    for (int j = 0; j <= fill; j++) {
      int next = (j << 1) & fill;
      if (c == 'J' || c == '?') {
        dp[i % 2][next][1] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
      }
      if (c == 'O' || c == '?') {
        dp[i % 2][next][0] += dp[(i + 1) % 2][j][0];
        dp[i % 2][next + 1][0] += dp[(i + 1) % 2][j][1];
      }
      if (c == 'I' || c == '?') {
        if (j >> (n - 2) == 0 || i % n == n - 1) {
          dp[i % 2][next][0] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
        }
      }
      dp[i % 2][next][0] %= 100000;
      dp[i % 2][next][1] %= 100000;
      dp[i % 2][next + 1][0] %= 100000;
      dp[i % 2][next + 1][1] %= 100000;
    }
  }
  for (int i = 0; i <= fill; i++) {
    ans += dp[(m * n - 1) % 2][i][0] + dp[(m * n - 1) % 2][i][1];
    ans %= 100000;
  }
  printf("%d\\n", (100000 + all - ans) % 100000);
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
int m, n, ans;
int dp[2][1 << 21][2], fill, all = 1;
char c;
int main() {
  scanf("%d%d", &m, &n);
  fill = (1 << (n - 1)) - 1;
  dp[1][0][0] = 1;
  for (int i = 0; i < m * n; i++) {
    scanf(" %c", &c);
    if (c == '?') {
      all *= 3;
      all %= 100000;
    }
    memset(dp[i % 2], 0, sizeof(dp[i % 2]));
    for (int j = 0; j <= fill; j++) {
      int next = (j << 1) & fill;
      if (c == 'J' || c == '?') {
        dp[i % 2][next][1] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
      }
      if (c == 'O' || c == '?') {
        dp[i % 2][next][0] += dp[(i + 1) % 2][j][0];
        dp[i % 2][next + 1][0] += dp[(i + 1) % 2][j][1];
      }
      if (c == 'I' || c == '?') {
        if (j >> (n - 2) == 0 || i % n == n - 1) {
          dp[i % 2][next][0] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
        }
      }
      dp[i % 2][next][0] %= 100000;
      dp[i % 2][next][1] %= 100000;
      dp[i % 2][next + 1][0] %= 100000;
      dp[i % 2][next + 1][1] %= 100000;
    }
  }
  for (int i = 0; i <= fill; i++) {
    ans += dp[(m * n - 1) % 2][i][0] + dp[(m * n - 1) % 2][i][1];
    ans %= 100000;
  }
  printf("%d\\n", (100000 + all - ans) % 100000);
  return 0;
}
Problem p00482 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int M, N;
  cin >> M >> N;
  char g[20][21];
  for (int i = 0; i < M; i++) {
    cin >> g[i];
  }
  static int dp[2][2][1 << 20];
  dp[0][0][0] = 1;
  int s = M * N;
  int MOD = 100000;
  int all = 1;
  for (int i = 0; i < s; i++) {
    int y = i / N;
    int x = i % N;
    bool q = g[y][x] == '?';
    all = (all * (q ? 3 : 1)) % MOD;
    int bx = i % 2;
    int dx = bx ^ 1;
    fill(dp[dx][0], dp[dx + 1][0], 0);
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 1 << 20; l++) {
        if (!dp[bx][k][l])
          continue;
        int db = l & ~(1 << x);
        if (q || g[y][x] == 'J') {
          dp[dx][1][db] = (dp[dx][1][db] + dp[bx][k][l]) % MOD;
        }
        if (q || g[y][x] == 'O') {
          dp[dx][0][db | k << x] =
              (dp[dx][0][db | k << x] + dp[bx][k][l]) % MOD;
        }
        if (!(l >> x + 1 & 1) && (q || g[y][x] == 'I')) {
          dp[dx][0][db] = (dp[dx][0][db] + dp[bx][k][l]) % MOD;
        }
      }
    }
    if (x == s % N - 1) {
      for (int k = 0; k < 1 << 20; k++) {
        dp[dx][0][k] += dp[dx][1][k];
        dp[dx][1][k] = 0;
      }
    }
  }
  int a = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 1 << 20; j++) {
      a = (a + dp[s % 2][i][j]) % MOD;
    }
  }
  cout << (all - a + MOD) % MOD << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int M, N;
  cin >> M >> N;
  char g[20][21];
  for (int i = 0; i < M; i++) {
    cin >> g[i];
  }
  static int dp[2][2][1 << 20];
  dp[0][0][0] = 1;
  int s = M * N;
  int MOD = 100000;
  int all = 1;
  for (int i = 0; i < s; i++) {
    int y = i / N;
    int x = i % N;
    bool q = g[y][x] == '?';
    all = (all * (q ? 3 : 1)) % MOD;
    int bx = i % 2;
    int dx = bx ^ 1;
    fill(dp[dx][0], dp[dx + 1][0], 0);
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 1 << 20; l++) {
        int db = l & ~(1 << x);
        if (q || g[y][x] == 'J') {
          dp[dx][1][db] = (dp[dx][1][db] + dp[bx][k][l]) % MOD;
        }
        if (q || g[y][x] == 'O') {
          dp[dx][0][db | k << x] =
              (dp[dx][0][db | k << x] + dp[bx][k][l]) % MOD;
        }
        if (!(l >> x + 1 & 1) && (q || g[y][x] == 'I')) {
          dp[dx][0][db] = (dp[dx][0][db] + dp[bx][k][l]) % MOD;
        }
      }
    }
    if (x == s % N - 1) {
      for (int k = 0; k < 1 << 20; k++) {
        dp[dx][0][k] += dp[dx][1][k];
        dp[dx][1][k] = 0;
      }
    }
  }
  int a = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 1 << 20; j++) {
      a = (a + dp[s % 2][i][j]) % MOD;
    }
  }
  cout << (all - a + MOD) % MOD << endl;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
int n[2200][2200][3], l[2200][3], h, w, q, a, b, c, d;
char s;
int main() {
  cin >> h >> w >> q;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> s;
      if (s == 'J') {
        l[i][0]++;
      } else if (s == 'O') {
        l[i][1]++;
      } else {
        l[i][2]++;
      }
      n[i][j][0] = n[i - 1][j][0] + l[i][0];
      n[i][j][1] = n[i - 1][j][1] + l[i][1];
      n[i][j][2] = n[i - 1][j][2] + l[i][2];
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> a >> b >> c >> d;
    for (int j = 0; j < 3; j++) {
      if (j) {
        cout << ' ';
      }
      cout << n[a - 1][b - 1][j] + n[c][d][j] - n[a - 1][d][j] - n[c][b - 1][j];
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int n[10000][10000][3], l[10000][3], h, w, q, a, b, c, d;
char s;
int main() {
  cin >> h >> w >> q;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> s;
      if (s == 'J') {
        l[i][0]++;
      } else if (s == 'O') {
        l[i][1]++;
      } else {
        l[i][2]++;
      }
      n[i][j][0] = n[i - 1][j][0] + l[i][0];
      n[i][j][1] = n[i - 1][j][1] + l[i][1];
      n[i][j][2] = n[i - 1][j][2] + l[i][2];
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> a >> b >> c >> d;
    for (i = 0; i < 3; i++) {
      if (i) {
        cout << ' ';
      }
      cout << n[a - 1][b - 1][i] + n[c][d][i] - n[a - 1][d][i] - n[c][b - 1][i];
    }
    cout << endl;
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int M, N, K;
string field[1000];
char trg[4] = "JOI";
int cnt[3][1000][1010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> M >> N >> K;
  for (int x = 0; x < M; x++) {
    cin >> field[x];
  }
  for (int x = 0; x < M; x++) {
    fill(cnt[0][x], cnt[0][x] + N + 1, 0);
    fill(cnt[1][x], cnt[1][x] + N + 1, 0);
    fill(cnt[2][x], cnt[2][x] + N + 1, 0);
  }
  for (int x = 0; x < M; x++) {
    for (int y = 1; y <= N; y++) {
      for (int i = 0; i < 3; i++) {
        cnt[i][x][y] = cnt[i][x][y - 1];
        if (field[x][y - 1] == trg[i]) {
          ++cnt[i][x][y];
        }
      }
    }
  }
  for (; K--;) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int res[3] = {0};
    for (int x = a - 1; x < c; x++) {
      for (int i = 0; i < 3; i++) {
        res[i] += cnt[i][x][d] - cnt[i][x][b - 1];
      }
    }
    cout << res[0] << ' ' << res[1] << ' ' << res[2] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int M, N, K;
string field[1000];
char trg[4] = "JOI";
int cnt[3][1000][1001];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> M >> N >> K;
  for (int x = 0; x < M; x++) {
    cin >> field[x];
  }
  for (int x = 0; x < M; x++) {
    fill(cnt[0][x], cnt[0][x] + N + 1, 0);
    fill(cnt[1][x], cnt[1][x] + N + 1, 0);
    fill(cnt[2][x], cnt[2][x] + N + 1, 0);
  }
  for (int x = 0; x < M; x++) {
    for (int y = 1; y <= N; y++) {
      for (int i = 0; i < 3; i++) {
        cnt[i][x][y] = cnt[i][x][y - 1];
        if (field[x][y - 1] == trg[i]) {
          ++cnt[i][x][y];
        }
      }
    }
  }
  for (; K--;) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    int res[3] = {0};
    for (int x = a - 1; x < c; x++) {
      for (int i = 0; i < 3; i++) {
        res[i] += cnt[i][x][d] - cnt[i][x][b - 1];
      }
    }
    cout << res[0] << ' ' << res[1] << ' ' << res[2] << endl;
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int j_wa[1005][1005], o_wa[1005][1005], i_wa[1005][1005];
int ans, j, ii, o, a, b, c, d, n, m, k;
char planet;

int main() {
  cin >> m >> n >> k;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> planet;
      if (planet == 'J')
        j_wa[j][i]++;
      if (planet == 'O')
        o_wa[j][i]++;
      if (planet == 'I')
        i_wa[j][i]++;
      j_wa[j][i] += j_wa[j - 1][i];
      o_wa[j][i] += o_wa[j - 1][i];
      i_wa[j][i] += i_wa[j - 1][i];
    }
    for (int j = 1; j <= n; j++) {

      j_wa[j][i] += j_wa[j][i - 1];
      o_wa[j][i] += o_wa[j][i - 1];
      i_wa[j][i] += i_wa[j][i - 1];
    }
  }

  for (int i = 0; i < k; i++) {
    j = 0, o = 0, ii = 0;
    cin >> a >> b >> c >> d;
    j = j_wa[d][c], o = o_wa[d][c], ii = i_wa[d][c];

    j = j - j_wa[b - 1][c] - j_wa[d][a - 1];
    o = o - o_wa[b - 1][c] - o_wa[d][a - 1];
    ii = ii - i_wa[b - 1][c] - i_wa[d][a - 1];

    j += j_wa[b - 1][a - 1];
    o += o_wa[b - 1][a - 1];
    ii += i_wa[b - 1][a - 1];

    printf("%d %d %d\\n", j, o, ii);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int j_wa[1005][1005], o_wa[1005][1005], i_wa[1005][1005];
int ans, j, i, o, a, b, c, d, n, m, k;
char planet;

int main() {
  cin >> m >> n >> k;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> planet;
      if (planet == 'J')
        j_wa[j][i]++;
      if (planet == 'O')
        o_wa[j][i]++;
      if (planet == 'I')
        i_wa[j][i]++;
      j_wa[j][i] += j_wa[j - 1][i];
      o_wa[j][i] += o_wa[j - 1][i];
      i_wa[j][i] += i_wa[j - 1][i];
    }
    for (int j = 1; j <= n; j++) {
      j_wa[j][i] += j_wa[j][i - 1];
      o_wa[j][i] += o_wa[j][i - 1];
      i_wa[j][i] += i_wa[j][i - 1];
    }
  }

  for (int i = 0; i < k; i++) {
    j = 0, o = 0, i = 0;
    cin >> a >> b >> c >> d;
    j = j_wa[d][c], o = o_wa[d][c], i = i_wa[d][c];

    j = j - j_wa[b - 1][c] - j_wa[d][a - 1];
    o = o - o_wa[b - 1][c] - o_wa[d][a - 1];
    i = i - i_wa[b - 1][c] - i_wa[d][a - 1];

    j += j_wa[b - 1][a - 1];
    o += o_wa[b - 1][a - 1];
    i += i_wa[b - 1][a - 1];

    printf("%d %d %d\\n", j, o, i);
  }

  return 0;
}