alexjercan/bugnet huggingface dataset:
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int M, N, K;

int field[1001][1001][3];

int main() {

  scanf("%d%d%d", &M, &N, &K);

  for (int i = 0; i <= M; i++) {
    for (int j = 0; j < 3; j++) {
      field[i][0][j] = 0;
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      field[0][i][j] = 0;
    }
  }

  char aaaaaaaaaaaaaaaaaaaaa;
  scanf("%c", &aaaaaaaaaaaaaaaaaaaaa);

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      char in;
      scanf("%c", &in);
      for (int k = 0; k < 3; k++) {
        field[i][j][k] =
            field[i - 1][j][k] + field[i][j - 1][k] - field[i - 1][j - 1][k];
      }
      if (in == 'J') {
        field[i][j][0]++;
      } else if (in == 'O') {
        field[i][j][1]++;
      } else {
        field[i][j][2]++;
      }
    }
    char aaaaaaaaaaaaaaaaaaaaa;
    scanf("%c", &aaaaaaaaaaaaaaaaaaaaa);
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    for (int j = 0; j < 3; j++) {
      int ret = field[c][d][j] - field[c][b - 1][j] - field[a - 1][d][j] +
                field[a - 1][b - 1][j];
      printf("%d", ret);
      if (j != 2) {
        printf(" ");
      }
    }
    printf("\\n");
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

int M, N, K;

int main() {

  scanf("%d%d%d", &M, &N, &K);

  int field[M + 1][N + 1][3];

  for (int i = 0; i <= M; i++) {
    for (int j = 0; j < 3; j++) {
      field[i][0][j] = 0;
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      field[0][i][j] = 0;
    }
  }

  char aaaaaaaaaaaaaaaaaaaaa;
  scanf("%c", &aaaaaaaaaaaaaaaaaaaaa);

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      char in;
      scanf("%c", &in);
      for (int k = 0; k < 3; k++) {
        field[i][j][k] =
            field[i - 1][j][k] + field[i][j - 1][k] - field[i - 1][j - 1][k];
      }
      if (in == 'J') {
        field[i][j][0]++;
      } else if (in == 'O') {
        field[i][j][1]++;
      } else {
        field[i][j][2]++;
      }
    }
    char in;
    scanf("%c", &in);
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    for (int j = 0; j < 3; j++) {
      int ret = field[c][d][j] - field[c][b - 1][j] - field[a - 1][d][j] +
                field[a - 1][b - 1][j];
      printf("%d", ret);
      if (j != 2) {
        printf(" ");
      }
    }
    printf("\\n");
  }

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

#include <stdio.h>
int n, m, q, a, b, c, d, rui[1005][1005][3], ret[3];
char f[1005][1005], e[3] = {'J', 'O', 'I'};
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    scanf("%s", &f[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 0; k < 3; k++)
        rui[i][j][k] = rui[i - 1][j][k] + rui[i][j - 1][k] -
                       rui[i - 1][j - 1][k] + (f[i - 1][j - 1] == e[k]);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    for (int j = 0; j < 3; j++)
      ret[j] = rui[c][d][j] - rui[c][b - 1][j] - rui[a - 1][d][j] +
               rui[a - 1][b - 1][j];
    printf("%d %d %d\\n", ret[0], ret[1], ret[2]);
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  const int MAX_N = 1010;
  int m, n, k;
  string s[1010];
  scanf("%d%d%d", &m, &n, &k);
  for (int i = 0; i < m; i++)
    cin >> s[i];

  int j[MAX_N + 1][MAX_N + 1], o[MAX_N + 1][MAX_N + 1];
  int i[MAX_N + 1][MAX_N + 1];
  for (int I = 0; I <= m; I++) {
    for (int J = 0; J <= n; J++) {
      if (I == 0 || J == 0)
        j[I][J] = o[I][J] = i[I][J] = 0;
      else {
        j[I][J] = j[I - 1][J] + j[I][J - 1] - j[I - 1][J - 1];
        o[I][J] = o[I - 1][J] + o[I][J - 1] - o[I - 1][J - 1];
        i[I][J] = i[I - 1][J] + i[I][J - 1] - i[I - 1][J - 1];
        if (s[I - 1][J - 1] == 'J')
          j[I][J]++;
        if (s[I - 1][J - 1] == 'I')
          i[I][J]++;
        if (s[I - 1][J - 1] == 'O')
          o[I][J]++;
      }
    }
  }
  int a, b, c, d;
  int ansi, anso, ansj;
  for (int x = 0; x < k; x++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    ansj = j[c][d] + j[a - 1][b - 1] - j[a - 1][d] - j[c][b - 1];
    anso = o[c][d] + o[a - 1][b - 1] - o[a - 1][d] - o[c][b - 1];
    ansi = i[c][d] + i[a - 1][b - 1] - i[a - 1][d] - i[c][b - 1];
    printf("%d %d %d\\n", ansj, anso, ansi);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int M, N, K;
string S[1001];
int A, B, C, D;
int I[1001][1001];
int O[1001][1001];
int J[1001][1001];
int main() {
  scanf("%d%d%d", &M, &N, &K);
  for (int i = 0; i < M; i++)
    cin >> S[i + 1];
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      I[i][j] = I[i - 1][j] + I[i][j - 1] - I[i - 1][j - 1];
      O[i][j] = O[i - 1][j] + O[i][j - 1] - O[i - 1][j - 1];
      J[i][j] = J[i - 1][j] + J[i][j - 1] - J[i - 1][j - 1];
      if (S[i][j - 1] == 'J')
        J[i][j]++;
      if (S[i][j - 1] == 'O')
        O[i][j]++;
      if (S[i][j - 1] == 'I')
        I[i][j]++;
    }
  }
  for (int k = 0; k < K; k++) {
    scanf("%d%d%d%d", &A, &B, &C, &D);
    printf("%d %d %d\\n", J[C][D] - J[A - 1][D] - J[C][B - 1] + J[A - 1][B - 1],
           O[C][D] - O[A - 1][D] - O[C][B - 1] + O[A - 1][B - 1],
           I[C][D] - I[A - 1][D] - I[C][B - 1] + I[A - 1][B - 1]);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  const int MAX_N = 1010;
  int m, n, k;
  string s[1010];
  scanf("%d%d%d", &m, &n, &k);
  for (int i = 0; i < m; i++)
    cin >> s[i];

  int j[MAX_N + 1][MAX_N + 1], o[MAX_N + 1][MAX_N + 1];
  int i[MAX_N + 1][MAX_N + 1];
  for (int I = 0; I <= m; I++) {
    for (int J = 0; J <= n; J++) {
      if (I == 0 || J == 0)
        j[I][J] = o[I][J] = i[I][J] = 0;
      else {
        j[I][J] = j[I - 1][J] + j[I][J - 1] - j[I - 1][J - 1];
        o[I][J] = o[I - 1][J] + o[I][J - 1] - o[I - 1][J - 1];
        i[I][J] = i[I - 1][J] + i[I][J - 1] - i[I - 1][J - 1];
        if (s[I - 1][J - 1] == 'J')
          j[I][J]++;
        if (s[I - 1][J - 1] == 'I')
          i[I][J]++;
        if (s[I - 1][J - 1] == 'O')
          o[I][J]++;
      }
    }
  }
  int a, b, c, d;
  int ansi, anso, ansj;
  for (int x = 0; x < k; x++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    ansj = j[c][d] + j[a - 1][b - 1] - j[a - 1][d] - j[c][b - 1];
    anso = o[c][d] + o[a - 1][b - 1] - o[a - 1][d] - o[c][b - 1];
    ansi = i[c][d] + i[a - 1][b - 1] - i[a - 1][d] - i[c][b - 1];
    printf("%d %d %d\\n", ansj, anso, ansi);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int n, m, q, a, b, c, d, rui[1005][1005][3], ret[3];
char f[1005][1005], e[3] = {'J', 'O', 'I'};
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    scanf("%s", &f[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 0; k < 3; k++)
        rui[i][j][k] = rui[i - 1][j][k] + rui[i][j - 1][k] -
                       rui[i - 1][j - 1][k] + (f[i - 1][j - 1] == e[k]);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    for (int j = 0; j < 3; j++)
      ret[j] = rui[c][d][j] - rui[c][b - 1][j] - rui[a - 1][d][j] +
               rui[a - 1][b - 1][j];
    printf("%d %d %d\\n", ret[0], ret[1], ret[2]);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char JOI[1005][1005];

int main() {
  int m, n;
  cin >> m >> n;
  int k;
  cin >> k;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      cin >> JOI[i][j];
  int a, b, c, d;
  for (int i = 0; i < k; i++) {
    int jangle = 0, ocean = 0, ice = 0;
    cin >> a >> b >> c >> d;
    int half;
    half = (a + c) / 2 + 1;
    for (int h = a; h < half; h++) {
      for (int w = b; w <= d; w++) {
        if (JOI[h][w] == 'J')
          jangle++;
        else if (JOI[h][w] == 'O')
          ocean++;
        else if (JOI[h][w] == 'I')
          ice++;
      }
    }
    for (int h = half; h <= c; h++) {
      for (int w = b; w <= d; w++) {
        if (JOI[h][w] == 'J')
          jangle++;
        else if (JOI[h][w] == 'O')
          ocean++;
        else if (JOI[h][w] == 'I')
          ice++;
      }
    }
    cout << jangle << " " << ocean << " " << ice;
    cout << endl;
  }
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;

int jj[1001][1001], oo[1001][1001], ii[1001][1001];
char map[1001][1001];

int main(void) {
  int a, b, c, m, n, k;
  cin >> m >> n;
  cin >> k;
  for (a = 0; a < m; a++) {
    cin >> map[a];
  }
  memset(jj, 0, sizeof(jj));
  memset(jj, 0, sizeof(oo));
  memset(jj, 0, sizeof(ii));
  for (a = 0; a < m; a++) {
    for (b = 0; b < n; b++) {
      jj[b + 1][a + 1] = jj[b + 1][a] + jj[b][a + 1] - jj[b][a];
      oo[b + 1][a + 1] = oo[b + 1][a] + oo[b][a + 1] - oo[b][a];
      ii[b + 1][a + 1] = ii[b + 1][a] + ii[b][a + 1] - ii[b][a];
      if (map[a][b] == 'J')
        jj[b + 1][a + 1]++;
      if (map[a][b] == 'O')
        oo[b + 1][a + 1]++;
      if (map[a][b] == 'I')
        ii[b + 1][a + 1]++;
    }
  }
  for (c = 0; c < k; c++) {
    int ax, ay, bx, by;
    int ja = 0, oa = 0, ia = 0;
    cin >> ay >> ax >> by >> bx;
    ja = jj[bx][by] - jj[ax - 1][by] - jj[bx][ay - 1] + jj[ax - 1][ay - 1];
    oa = oo[bx][by] - oo[ax - 1][by] - oo[bx][ay - 1] + oo[ax - 1][ay - 1];
    ia = ii[bx][by] - ii[ax - 1][by] - ii[bx][ay - 1] + ii[ax - 1][ay - 1];
    cout << ja << " " << oa << " " << ia << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  char map[1001][1001];
  int a, b, c, m, n, k;
  cin >> m >> n;
  cin >> k;
  for (a = 0; a < m; a++) {
    cin >> map[a];
  }
  for (c = 0; c < k; c++) {
    int ax, ay, bx, by;
    int ja = 0, oa = 0, ia = 0;
    cin >> ay >> ax >> by >> bx;
    for (a = ay - 1; a < by; a++) {
      for (b = ax - 1; b < bx; b++) {
        if (map[a][b] == 'J')
          ja++;
        if (map[a][b] == 'O')
          oa++;
        if (map[a][b] == 'I')
          ia++;
      }
    }
    cout << ja << " " << oa << " " << ia << endl;
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

#define NMAX 1000
#define MMAX 1000

int M, N;
int K;
int JOI[3][NMAX + 1][MMAX + 1];
int a, b, c, d;
string in;

int main() {

  cin >> M >> N;
  cin >> K;

  for (int i = 1; i <= M; i++) {
    cin >> in;
    for (int j = 0; j < N; j++) {
      if (in[j] == 'J')
        JOI[0][i][j + 1] = 1;
      else if (in[j] == 'O')
        JOI[1][i][j + 1] = 1;
      else if (in[j] == 'I')
        JOI[2][i][j + 1] = 1;
    }
  }

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 0; k < 3; k++) {
        JOI[k][i][j] += JOI[k][i][j - 1];
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      for (int k = 0; k < 3; k++) {
        JOI[k][j][i] += JOI[k][j - 1][i];
      }
    }
  }

  for (int i = 0; i < K; i++) {
    cin >> a >> b >> c >> d;
    for (int j = 0; j < 3; j++) {
      cout << JOI[j][c][d] - JOI[j][c][b - 1] - JOI[j][a - 1][d] +
                  JOI[j][a - 1][b - 1];
      if (j != 2)
        cout << " ";
      else
        cout << endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

#define NMAX 999
#define MMAX 999

int main() {

  int M, N;
  int K;
  int JOI[3][NMAX + 1][MMAX + 1] = {0};
  int a, b, c, d;
  string in;

  cin >> M >> N;
  cin >> K;

  for (int i = 1; i <= M; i++) {
    cin >> in;
    for (int j = 0; j < N; j++) {
      if (in[j] == 'J')
        JOI[0][i][j + 1] = 1;
      else if (in[j] == 'O')
        JOI[1][i][j + 1] = 1;
      else if (in[j] == 'I')
        JOI[2][i][j + 1] = 1;
    }
  }

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 0; k < 3; k++) {
        JOI[k][i][j] += JOI[k][i][j - 1];
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      for (int k = 0; k < 3; k++) {
        JOI[k][j][i] += JOI[k][j - 1][i];
      }
    }
  }

  for (int i = 0; i < K; i++) {
    cin >> a >> b >> c >> d;
    for (int j = 0; j < 3; j++) {
      cout << JOI[j][c][d] - JOI[j][c][b - 1] - JOI[j][a - 1][d] +
                  JOI[j][a - 1][b - 1];
      if (j != 2)
        cout << " ";
      else
        cout << endl;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

struct joi {
  int j, o, i;
};

int main() {
  int r, c, n;
  cin >> r >> c >> n;

  string s[r + 1];
  vector<vector<joi>> m(r + 1, vector<joi>(c + 1, (joi){0, 0, 0}));
  FOR(i, 1, r + 1) {
    cin >> s[i];
    FOR(j, 1, c + 1) {
      m[i][j] = m[i][j - 1];
      if (s[i][j - 1] == 'J')
        m[i][j].j++;
      else if (s[i][j - 1] == 'O')
        m[i][j].o++;
      else if (s[i][j - 1] == 'I')
        m[i][j].i++;
    }
  }

  REP(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int J, O, I = O = J = 0;
    FOR(j, a, c + 1) {
      J += (m[j][d].j - m[j][b - 1].j);
      O += (m[j][d].o - m[j][b - 1].o);
      I += (m[j][d].i - m[j][b - 1].i);
    }

    cout << J << " " << O << " " << I << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  int r, c, n;
  cin >> r >> c >> n;

  string s[r];
  REP(i, r) { cin >> s[i]; }

  REP(i, n) {
    int a, b, c, d;
    int J, O, I = O = J = 0;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    c--;
    d--;
    FOR(j, a, c + 1) {
      FOR(k, b, d + 1) {
        if (s[j][k] == 'J')
          J++;
        else if (s[j][k] == 'O')
          O++;
        else
          I++;
      }
    }

    cout << J << " " << O << " " << I << endl;
  }
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int H, W, K;
int dp[1001][1001][3];

int solve(int x1, int y1, int x2, int y2, int type) {
  int res = 0;
  for (int y = y1; y <= y2; y++) {
    res += dp[y][x2][type] - dp[y][x1 - 1][type];
  }
  return res;
}

int main() {
  std::cin >> H >> W >> K;
  for (int y = 1; y <= H; y++) {
    for (int x = 1; x <= W; x++) {
      char c;
      std::cin >> c;
      if (c == 'J')
        dp[y][x][0]++;
      if (c == 'O')
        dp[y][x][1]++;
      if (c == 'I')
        dp[y][x][2]++;
      for (int i = 0; i < 3; i++) {
        dp[y][x][i] += dp[y][x - 1][i];
      }
    }
  }

  while (K--) {
    int x1, y1, x2, y2;
    std::cin >> y1 >> x1 >> y2 >> x2;
    std::cout << solve(x1, y1, x2, y2, 0);
    for (int t = 1; t < 3; t++) {
      std::cout << " " << solve(x1, y1, x2, y2, t);
    }
    std::cout << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int dp[1001][1001][3];
int M, N, K;
int a, b, c, d;
int j, o, i;

int solve(int a, int b, int c, int d, int t) {
  int ret = 0;
  for (int i = a; i <= c; i++) {
    ret += dp[i][d][t] - dp[i][b - 1][t];
  }
  return ret;
}

int main(void) {
  cin >> M >> N;
  cin >> K;
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      char c;
      cin >> c;
      if (c == 'J')
        dp[i][j][0]++;
      if (c == 'O')
        dp[i][j][1]++;
      if (c == 'I')
        dp[i][j][2]++;
      for (int k = 0; k < 3; k++) {
        dp[i][j][k] += dp[i][j - 1][k];
      }
    }
  }

  for (int i = 0; i < K; i++) {
    cin >> a >> b >> c >> d;
    j = solve(a, b, c, d, 0);
    o = solve(a, b, c, d, 1);
    i = solve(a, b, c, d, 2);
    cout << j << " " << o << " " << i << endl;
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

struct joi {
  int j;
  int o;
  int i;
};

int main() {
  int m, n, kk;
  cin >> m >> n >> kk;
  vector<joi> G(1001 * 1001 + 1);
  for (int k = 0; k < 1001; k++) {
    G[0 + 1000 * k].j = 0;
    G[0 + 1000 * k].o = 0;
    G[0 + 1000 * k].i = 0;
  }
  for (int l = 1; l <= m; l++) {
    string S;
    cin >> S;
    for (int k = 1; k <= S.length(); k++) {
      G[l * 1000 + k].j = G[l * 1000 + k - 1].j;
      G[l * 1000 + k].o = G[l * 1000 + k - 1].o;
      G[l * 1000 + k].i = G[l * 1000 + k - 1].i;
      if (S[k - 1] == 'J') {
        G[l * 1000 + k].j++;
      }
      if (S[k - 1] == 'O') {
        G[l * 1000 + k].o++;
      }
      if (S[k - 1] == 'I') {
        G[l * 1000 + k].i++;
      }
    }
  }
  for (int p = 0; p < kk; p++) {
    int ansj = 0, anso = 0, ansi = 0;
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    for (int q = min(x1, x2); q <= max(x1, x2); q++) {
      ansj += G[q * 1000 + max(y1, y2)].j - G[q * 1000 + min(y1, y2) - 1].j;
      anso += G[q * 1000 + max(y1, y2)].o - G[q * 1000 + min(y1, y2) - 1].o;
      ansi += G[q * 1000 + max(y1, y2)].i - G[q * 1000 + min(y1, y2) - 1].i;
    }
    cout << ansj << " " << anso << " " << ansi << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;
typedef long long int ll;

struct joi {
  int j;
  int o;
  int i;
};

int main() {
  int m, n, kk;
  cin >> m >> n >> kk;
  joi G[1002][1002];
  for (int k = 0; k < 1001; k++) {
    G[k][0] = {0, 0, 0};
  }
  for (int l = 1; l <= m; l++) {
    string S;
    cin >> S;
    for (int k = 1; k < S.length(); k++) {
      if (S[k] == 'J') {
        G[l][k].j = G[l][k - 1].j + 1;
      }
      if (S[k] == 'O') {
        G[l][k].o = G[l][k - 1].o + 1;
      }
      if (S[k] == 'I') {
        G[l][k].i = G[l][k - 1].i + 1;
      }
    }
  }
  for (int p = 0; p < kk; p++) {
    int ansj = 0, anso = 0, ansi = 0;
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    for (int q = x1; q <= x2;) {
      ansj += G[q][y2].j - G[q][y1].j;
      anso += G[q][y2].o - G[q][y1].o;
      ansi += G[q][y2].i - G[q][y1].i;
    }
    cout << ansj << " " << anso << " " << ansi << endl;
  }
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
char MM[1005][1003];
int MO[1005][1003] = {}, MJ[1005][1003] = {}, MI[1005][1003] = {};
int main() {

  string str;
  int y, x, n;
  cin >> y >> x;
  cin >> n;
  for (int i = 1; i <= y; i++) {
    cin >> str;
    for (int j = 0; j < str.size(); j++) {
      MM[i][j + 1] = str[j];
      if (str[j] == 'O')
        MO[i][j + 1]++;
      if (str[j] == 'J')
        MJ[i][j + 1]++;
      if (str[j] == 'I')
        MI[i][j + 1]++;

      MO[i][j + 1] += MO[i][j];
      MJ[i][j + 1] += MJ[i][j];
      MI[i][j + 1] += MI[i][j];
    }
  }
  for (int I = 0; I < n; I++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int J = 0, U = 0, II = 0;
    for (int i = a; i <= c; i++) {
      U += MO[i][d] - MO[i][b - 1];
      J += MJ[i][d] - MJ[i][b - 1];
      II += MI[i][d] - MI[i][b - 1];
      /*
      for(int j=b;j<=d;j++){
              if(MM[i][j]=='O')U++;
              if(MM[i][j]=='J')J++;
              if(MM[i][j]=='I')II++;

      //	cout<<MM[i][j];
      }*/
      //	cout<<endl;
    }

    cout << J << " " << U << " " << II << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
char MM[1005][1003];
int main() {

  string str;
  int y, x, n;
  cin >> y >> x;
  cin >> n;
  for (int i = 1; i <= y; i++) {
    cin >> str;
    for (int j = 0; j < str.size(); j++)
      MM[i][j + 1] = str[j];
  }
  for (int I = 0; I < n; I++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int J = 0, U = 0, II = 0;
    for (int i = a; i <= c; i++) {
      for (int j = b; j <= d; j++) {
        if (MM[i][j] == 'O')
          U++;
        if (MM[i][j] == 'J')
          J++;
        if (MM[i][j] == 'I')
          II++;

        //	cout<<MM[i][j];
      }
      //	cout<<endl;
    }

    cout << J << " " << U << " " << II << endl;
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <fstream>
#include <iostream>
using namespace std;

int cnt[1010][1010][3] = {0};

void count(int a, int b, char c) {
  if (c == 'J') {
    cnt[a][b][0]++;
  } else if (c == 'O') {
    cnt[a][b][1]++;
  } else {
    cnt[a][b][2]++;
  }
  return;
}

void printans(int a, int b, int c, int d) {

  int ans[3] = {0};

  for (int i = a - 1; i < c; i++) {
    ans[0] += cnt[i][d - 1][0] - cnt[i][b - 2][0];
    ans[1] += cnt[i][d - 1][1] - cnt[i][b - 2][1];
    ans[2] += cnt[i][d - 1][2] - cnt[i][b - 2][2];
  }

  cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;

  return;
}

int main() {

  // ifstream cin("in/01.in");
  // ofstream cout("OUTPUT.txt");

  int M, N, K;
  int a, b, c, d;
  char map[1010][1010];

  cin >> M >> N >> K;

  for (int i = 0; i < M; i++) {
    cin >> map[i];
  }

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      if (j != 0) {
        for (int k = 0; k < 3; k++) {
          cnt[i][j][k] = cnt[i][j - 1][k];
        }
      }
      count(i, j, map[i][j]);
    }
  }

  for (int i = 0; i < K; i++) {
    cin >> a >> b >> c >> d;
    printans(a, b, c, d);
  }

  /* for(int i = 0; i < K; i++) {
          int cnt[3] = {0};
          cin >> a >> b >> c >> d;
          for(int j = a-1; j < c; j++) {
                  for(int k = b-1; k < d; k++) {
                          if(map[j][k] == 'J') {
                                  cnt[0]++;
                          } else if(map[j][k] == 'O') {
                                  cnt[1]++;
                          } else {
                                  cnt[2]++;
                          }
                  }
          }
          cout << cnt[0] << " " << cnt[1] << " " << cnt[2] << endl;
  } */

  return 0;
}

Error: TLE

Fixed submission:

#include <fstream>
#include <iostream>
using namespace std;

int main() {

  // ifstream cin("in/01.in");
  // ofstream cout("OUTPUT.txt");

  int M, N, K;
  int a, b, c, d;
  char map[1010][1010];

  cin >> M >> N >> K;

  for (int i = 0; i < M; i++) {
    cin >> map[i];
  }

  for (int i = 0; i < K; i++) {
    int cnt[3] = {0};
    cin >> a >> b >> c >> d;
    for (int j = a - 1; j < c; j++) {
      for (int k = b - 1; k < d; k++) {
        if (map[j][k] == 'J') {
          cnt[0]++;
        } else if (map[j][k] == 'O') {
          cnt[1]++;
        } else {
          cnt[2]++;
        }
      }
    }
    cout << cnt[0] << " " << cnt[1] << " " << cnt[2] << endl;
  }

  return 0;
}