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

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int max(int, int);

int main(void) {
  char str[200];
  int a, b;

  while (1) {
    for (int i = 0; i < 200; i++) {
      str[i] = '\\0';
    }
    scanf("%[^\\n]%*c", str);
    a = 0;
    b = 0;
    if (str[0] == '0')
      break;

    for (int i = 1; str[i] != '\\0'; i++) {
      if (str[i] == 'A')
        a++;
      else if (str[i] == 'B')
        b++;
    }

    if (a == max(a, b))
      a++;
    else if (b == max(a, b))
      b++;
    cout << a << " " << b << endl;
  }

  return 0;
}

int max(int x, int y) { return (x < y) ? y : x; }

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int max(int, int);

int main(void) {
  char str[200];
  int a, b;

  while (1) {
    for (int i = 0; i < 200; i++) {
      str[i] = '\\0';
    }
    scanf("%[^\\n]%*c", str);
    a = 0;
    b = 0;
    if (str[0] == '0')
      break;
    for (int i = 1; str[i] != '\\n'; i++) {

      if (str[i] == 'A')
        a++;
      else if (str[i] == 'B')
        b++;
    }

    if (a == max(a, b))
      a++;
    else if (b == max(a, b))
      b++;
    cout << a << " " << b << endl;
  }

  return 0;
}

int max(int x, int y) { return (x < y) ? y : x; }
Problem p00174 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  char ta[1000];
  int a, b;

  while (true) {
    for (int i = 0; i < 3; i++) {
      cin >> ta;
      if (strcmp(ta, "0") == 0)
        return 0;
      a = b = 0;
      for (int j = 1; ta[j] != '\\0'; j++) {
        (ta[j] == 'A') ? a++ : b++;
      }
      (a > b) ? a++ : b++;
      cout << a << " " << b << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  char ta[1000] = "";
  int a, b;

  while (true) {
    for (int i = 0; i < 3; i++) {
      cin >> ta;
      if (strcmp(ta, "0") == 0)
        break;
      a = b = 0;
      for (int j = (i >= 1); ta[j] != '\\0'; j++) {
        (ta[j] == 'A') ? a++ : b++;
      }
      (a > b) ? a++ : b++;
      cout << " " << a << " " << b << endl;
    }
  }

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int i, j, counta = 0, countb = 0, bx;
string str;
bool bo = false;
void duce();
int main() {
  for (i = 1;; i++) {
    getline(cin, str);
    if (str[0] == '0') {
      break;
    }
    for (j = 1; j < str.size(); j++) {
      // if(counta==10 && countb==10){
      //    duce();
      //  bo=true;
      // break;
      //}
      if (str[j] == 'A') {
        counta++;
      } else if (str[j] == 'B') {
        countb++;
      }
    }
    if (bo) {
      bo = false;
    } else if (counta < countb) {
      countb++;
    } else if (counta > countb) {
      counta++;
    }
    cout << counta << " " << countb << endl;
    counta = 0;
    countb = 0;
  }
  return 0;
}
void duce() {
  counta = 0;
  countb = 0;
  for (; j < str.size(); j++) {
    if (str[j] == 'A') {
      counta++;
    } else {
      countb++;
    }
  }

  return;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
  int i, j, counta = 0, countb = 0, bx;
  string str;
  for (i = 0; i < 20; i++) {
    getline(cin, str);
    if (str[1] == 0) {
      break;
    }
    for (j = 0; j < str.size(); j++) {
      bx = {j = 0};
      if (i > 0 && bx == 'A') {
        counta--;
      } else if (i > 0 && bx == 'B') {
        countb--;
      }
      if (str[j] == 'A') {
        counta++;
      } else if (str[j] == 'B') {
        countb++;
      }
    }
    cout << counta << " " << countb << endl;
    counta = 0;
    countb = 0;
  }
  return 0;
}
Problem p00174 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int main() {
  char ss[110];
  int i, j, a, b;

  while (~scanf(" %s ", ss)) {
    if (ss[0] == '0')
      break;
    i = 1;
    a = 0;
    b = 0;
    while (1) {
      if (ss[i] == '\\0')
        break;
      if (ss[i] == 'A')
        ++a;
      if (ss[i] == 'B')
        ++b;
      ++i;
    }
    if (a > b) {
      ++a;
    } else {
      ++b;
    }
    printf("%d %d\\n", a, b);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

int main() {
  char ss[40];
  int i, j, a, b;

  while (~scanf(" %s ", ss)) {
    if (ss[0] == '0')
      break;
    i = 1;
    a = 0;
    b = 0;
    while (1) {
      if (ss[i] == '\\0')
        break;
      if (ss[i] == 'A')
        ++a;
      if (ss[i] == 'B')
        ++b;
      ++i;
    }
    if (a > b) {
      ++a;
    } else {
      ++b;
    }
    printf("%d %d\\n", a, b);
  }
  return 0;
}
Problem p00174 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  string s;
  int a, b;
  while (true) {
    for (int i = 0; i < 3; i++) {
      a = b = 0;
      cin >> s;
      if (s[0] == '0')
        return 0;

      for (int j = 1; j < s.length(); j++) {
        if (s[j] == 'A')
          a++;
        else
          b++;
      }
      if (a > b)
        a++;
      else
        b++;
      cout << a << " " << b << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  string s;
  int a, b;
  while (true) {
    for (int i = 0; i < 3; i++) {
      a = b = 0;
      cin >> s;
      if (s[0] == '0')
        break;

      for (int j = 1; j < s.length(); j++) {
        if (s[j] == 'A')
          a++;
        else
          b++;
      }
      if (a > b)
        a++;
      else
        b++;
      cout << a << " " << b << endl;
    }
  }
}
Problem p00174 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int main() {
  while (1) {
    string s;
    cin >> s;
    if (s == "0") {
      break;
    }
    int a = 0, b = 0;
    for (int i = 1; i < s.size(); i++) {
      if (s[i] == 'A') {
        a++;
      } else {
        b++;
      }
    }
    if (a > b) {
      a++;
    } else {
      b++;
    }
    cout << a << ' ' << b << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int main() {
  for (int x = 0; x >= 0; x++) {
    char r[5];
    char a[5];
    char z[2] = "0";
    cin >> r >> a;
    if ((strcmp(r, z) == 0) && (strcmp(a, z) == 0)) {
      break;
    }
    int h = 0, b = 0;
    for (int i = 0; i <= 3; i++) {
      for (int j = 0; j <= 3; j++) {
        if (r[i] == a[j]) {
          b++;
          if (i == j) {
            h++;
          }
        }
      }
    }
    b = b - h;
    cout << h << ' ' << b << '\\n';
  }
  return 0;
}
Problem p00174 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int a = 0, b = 0;
    string game;
    cin >> game;
    if (game == "0")
      break;
    for (int j = 1; j < game.size(); j++)
      (game[j] == 'A') ? a++ : b++;
    a > b ? a++ : b++;
    cout << a << " " << b << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int h = 0, b = 0;
    string r, a;
    cin >> r >> a;
    if (r == "0" && a == "0")
      break;
    for (int i = 0; i < r.size(); i++) {
      for (int j = 0; j < r.size(); j++) {
        if (r[i] == a[j])
          (i == j) ? h++ : b++;
      }
    }
    cout << h << " " << b << endl;
  }
  return 0;
}
Problem p00175 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int a[100];
  int n;
  int index;

  while (1) {
    scanf("%d", &n);
    if (n == -1)
      break;
    index = 0;
    do {
      a[index++] = (n & 0x3);
      n >>= 2;
    } while (n != 0);

    index--;
    while (index >= 0) {
      printf("%d", a[index--]);
    }

    puts("");
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int a[100];
  int n;
  int index;

  while (1) {
    scanf("%d", &n);
    if (n == -1)
      break;
    index = 0;
    do {
      a[index++] = (n & 0x3);
      n >> 2;
    } while (n != 0);

    index--;
    while (index >= 0) {
      printf("%d", a[index--]);
    }

    puts("");
  }
}
Problem p00175 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j, k, cnt = 1, flg;
  int n;
  int fo[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};
  while (1) {
    cin >> n;
    flg = 0;
    if (n == -1)
      break;
    for (i = 9; i >= 0; i--) {
      if (n == 0 && flg == 0) {
        cout << "0";
        break;
      }
      if (n / fo[i] > 0 && n != 0) {
        cout << n / fo[i];
        n = n % fo[i];
        flg = 1;
      } else if (flg == 1)
        cout << "0";
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j, k, cnt = 1, flg;
  int n;
  int fo[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};
  while (1) {
    cin >> n;
    flg = 0;
    if (n == -1)
      break;
    for (i = 10; i >= 0; i--) {
      if (n == 0 && flg == 0) {
        cout << "0";
        break;
      }
      if (n / fo[i] > 0 && n != 0) {
        cout << n / fo[i];
        n = n % fo[i];
        flg = 1;
      } else if (flg == 1)
        cout << "0";
    }
    cout << endl;
  }
  return 0;
}
Problem p00175 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  long long n;

  while (cin >> n, n != -1) {
    bool f = false;
    string s = "";
    for (int k = 9; k >= 0; k--) {
      int i = int(pow(4, k));
      int count = 0;
      for (int j = 0; j < 3; j++) {
        if (n - i >= 0) {
          n = n - i;
          count++;
        } else
          break;
      }
      if (count != 0 || f)
        s = s + to_string(count);
      if (s != "")
        f = true;
    }
    if (!f)
      s = "0";
    cout << s << endl;
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  int n;

  while (cin >> n, n != -1) {
    string s = "";
    for (int k = 9; k >= 0; k--) {
      int i = int(pow(4, k));
      int count = 0;
      for (int j = 0; j < 3; j++) {
        if (n - i >= 0) {
          n = n - i;
          count++;
        } else
          break;
      }
      s = s + to_string(count);
    }

    cout << stoi(s) << endl;
  }
}
Problem p00175 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, i;
  char c[20];
  while (true) {
    for (i = 0; i < 20; i++)
      c[i] = 0;
    c[0] = '0';
    cin >> n;
    if (n < 0)
      break;
    for (i = 0; n > 0; i++) {
      c[i] = n % 4 + '0';
      n = n / 4;
    }
    reverse(c, c + i);
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, i;
  char c[20];
  while (true) {
    for (i = 0; i < 20; i++)
      c[i] = 0;
    cin >> n;
    if (!n) {
      cout << 0 << endl;
      continue;
    }
    for (i = 0; n > 0; i++) {
      c[i] = n % 4 + '0';
      n = n / 4;
    }
    reverse(c, c + i);
    cout << c << endl;
  }
}
Problem p00175 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int a, b[100] = {}, c = 0;
  scanf("%d", &a);
  while (a != -1) {
    if (a == 0) {
      printf("0");
      goto A;
    }
    while (a > 0) {
      b[c] = a % 4;
      a = a / 4;
      c++;
    }
    for (int j = c - 1; j >= 0; j--) {
      printf("%d", b[j]);
    }
  A:
    printf("\\n");
    int b[100] = {};
    c = 0;
    scanf("%d", &a);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int main() {
  int a, b[100], c = 0;
  scanf("%d", &a);
  while (a != -1) {
    while (a > 0) {
      b[c] = a % 4;
      a = a / 4;
      c++;
    }
    for (int j = c - 1; j >= 0; j--) {
      printf("%d", b[j]);
    }
    printf("\\n");
    scanf("%d", &a);
  }
  return 0;
}
Problem p00176 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
int to_int(string s) {
  int k = 1, res = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    if ('0' <= s[i] && s[i] <= '9') {
      res += k * (s[i] - '0');
    } else {
      res += k * (s[i] - 'a' + 10);
    }
    k *= 16;
  }
  return res;
}

int main() {
  string ans[2][2][2];
  ans[0][0][0] = "black";
  ans[0][0][1] = "blue";
  ans[0][1][0] = "lime";
  ans[0][1][1] = "aqua";
  ans[1][0][0] = "red";
  ans[1][0][1] = "fuchsia";
  ans[1][1][0] = "yellow";
  ans[1][1][1] = "white";
  string s;
  while (cin >> s && s[0] != '0') {
    int a, b, c, dat[3];
    for (int i = 1; i < 6; i += 2) {
      string tmp;
      int ans[3];
      tmp.assign(s, i, 2);
      int d = to_int(tmp);
      if (d <= (to_int("ff") - d))
        dat[i / 2] = 0;
      else
        dat[i / 2] = 1;
    }
    cout << ans[dat[0]][dat[1]][dat[2]] << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
int to_int(string s) {
  int k = 1, res = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    if ('0' <= s[i] && s[i] <= '9') {
      res += k * (s[i] - '0');
    } else {
      res += k * (s[i] - 'a' + 10);
    }
    k *= 16;
  }
  return res;
}

int main() {
  string ans[2][2][2];
  ans[0][0][0] = "black";
  ans[0][0][1] = "blue";
  ans[0][1][0] = "lime";
  ans[0][1][1] = "aqua";
  ans[1][0][0] = "red";
  ans[1][0][1] = "fuchsia";
  ans[1][1][0] = "yellow";
  ans[1][1][1] = "white";
  string s;
  while (cin >> s && s[0] != '0') {
    int a, b, c, dat[3];
    for (int i = 1; i < 6; i += 2) {
      string tmp;
      int ans[3];
      tmp.assign(s, i, 2);
      int d = to_int(tmp);
      if (d <= (to_int("ff") - d))
        ans[i / 2] = 0;
      else
        ans[i / 2] = 1;
    }
    cout << ans[dat[0]][dat[1]][dat[2]] << endl;
  }
}
Problem p00177 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define EPS 1e-10
#define EQ(a, b) (fabs(a - b) < EPS)

int main(void) {
  const long double radius = 6378.1;
  const long double pi = acos(-1);
  long double x[2], y[2];
  long double dx;
  long double d;

  while (1) {
    int i;

    bool f = true;

    for (i = 0; i < 2; i++) {
      cin >> y[i] >> x[i];
      if (y[i] != -1 || x[i] != -1)
        f = false;

      x[i] = x[i] * pi / 180;
      y[i] = y[i] * pi / 180;
    }

    if (f)
      return 0;

    dx = x[1] - x[0];

    d = radius * acos(sin(y[0]) * sin(y[1]) + cos(y[0]) * cos(y[1]) * cos(dx));

    cout << round(d) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  const double radius = 6378.137;
  const double pi = acos(-1);
  double x[2], y[2];
  double dx;
  double d;

  while (1) {
    int i;

    for (i = 0; i < 2; i++) {
      scanf("%lf %lf", &y[i], &x[i]);

      x[i] = x[i] * pi / 180;
      y[i] = y[i] * pi / 180;
    }

    if (x[0] == -1 && x[1] == -1 && y[0] == -1 && y[1] == -1)
      return 0;

    dx = x[1] - x[0];

    d = radius * acos(sin(y[0]) * sin(y[1]) + cos(y[0]) * cos(y[1]) * cos(dx));

    printf("%.0f\\n", d);
  }

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

#include <iostream>
using namespace std;

int main() {
  int i, j, n, a, b, c, d, e, f, g, m[5050];
  while (cin >> n) {
    if (n == 0)
      break;
    for (j = 0; j < 5050; j++)
      m[j] = 0;
    d = 1;
    for (i = 0; i < n; i++) {
      cin >> a >> b >> c;
      f = a > 1 ? 1 << (c - 1) : ((1 << b) - 1) << (c - 1);
      for (j = d; j >= 0; j--)
        if (m[j] + f != (m[j] | f))
          break;
      e = g = j + 1;
      if (a == 1) {
        m[e] += f;
        d++;
      } else
        for (j = 0; j < b; j++) {
          m[e + j] += f;
          d++;
        }
      for (j = g; j < d + 5; j++)
        if (m[j] < 31)
          m[e++] = m[j];
    }
    f = 0;
    for (i = 0; i < 5050; i++)
      for (j = 0; j < 5; j++)
        f += (m[i] & (1 << j)) > 0;
    cout << f << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <math.h>

using namespace std;

int main() {
  int i, j, n, a, b, c, d, e, f, g, m[5002];
  while (cin >> n) {
    if (n == 0)
      break;
    for (j = 0; j < 5002; j++)
      m[j] = 0;
    d = 0;
    for (i = 0; i < n; i++) {
      cin >> a >> b >> c;
      f = a > 1 ? 1 << (c - 1) : ((1 << b) - 1) << (c - 1);
      for (j = d; j >= 0; j--)
        if (m[j] + f != (m[j] | f))
          break;
      e = g = j + 1;
      if (a == 1) {
        m[e] += f;
        d++;
      } else
        for (j = 0; j < b; j++) {
          m[e + j] += f;
          d += b;
        }
      for (j = g; j < g + d + 1; j++)
        if (m[j] < 31)
          m[e++] = m[j];
    }
    f = 0;
    for (i = 0; i < 5000; i++)
      for (j = 0; j < 5; j++)
        f += (m[i] & (1 << j)) > 0;
    cout << f << endl;
  }
  return 0;
}
Problem p00178 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#ifdef _WIN32
#define __builtin_popcount __popcnt
#endif
int n, d, p, q;
int main() {
  while (cin >> n, n) {
    vector<int> bit;
    vector<int> c(5);
    while (n--) {
      cin >> d >> p >> q;
      q--;
      if (d == 1) {
        int r = *max_element(c.begin() + q, c.begin() + q + p);
        int b = (1 << (q + p)) - (1 << q);
        if (bit.size() == r)
          bit.push_back(b);
        else
          bit[r] |= b;
      } else {
        for (int i = c[q]; i < c[q] + p; i++) {
          if (bit.size() == i)
            bit.push_back(1 << q);
          else
            bit[i] |= 1 << q;
        }
      }
      vector<int> bit2;
      for (int i : bit) {
        if (i != 31)
          bit2.push_back(i);
      }
      bit = bit2;
      for (int i = 0; i < 5; i++) {
        c[i] = 0;
        for (int j = bit2.size() - 1; j >= 0; j--) {
          if (bit2[j] & (1 << i)) {
            c[i] = j + 1;
            break;
          }
        }
      }
    }
    int ret = 0;
    for (int i : bit)
      ret += __builtin_popcount(i);
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int n, d, p, q;

int main() {
  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    vector<vector<bool>> c(5000, vector<bool>(5, false));

    for (int i = 0; i < n; i++) {
      cin >> d >> p >> q;

      if (d == 1) {
        int maxh = -1;

        for (int j = p - 1; j < p + q - 1; j++) {
          int r = 5000;

          while (r--) {
            if (c[r][j] == true) {
              break;
            }
          }

          maxh = max(maxh, r + 1);
        }

        for (int j = p - 1; j < p + q - 1; j++) {
          c[maxh][j] = true;
        }
      } else {
        int r = 5000;

        while (r--) {
          if (c[r][p] == true) {
            break;
          }
        }

        for (int j = r + 1; j < r + q + 1; j++) {
          c[j][p] = true;
        }
      }

      for (int j = 0; j < 5000; i++) {
        if (c[j][0] == c[j][1] && c[j][1] == c[j][2] && c[j][2] == c[j][3] &&
            c[j][3] == c[j][4]) {
          for (int k = j + 1; k < 5000; k++) {
            for (int l = 0; l < 5; l++) {
              c[k - 1][l] = c[k][l];
            }
          }
        }
      }
    }

    int sum = 0;

    for (int i = 0; i < 5000; i++) {
      for (int j = 0; j < 5; j++) {
        sum += (c[j][p] ? 1 : 0);
      }
    }

    cout << sum << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

#define chmax(a, x) a = max(a, x)

vector<bool> F[5];

void dump() {
  rep(j, 10) {
    rep(i, 5) {
      if (j >= int(F[i].size()))
        cout << 0;
      else
        cout << F[i][j];
    }
    cout << endl;
  }
}

bool eraser(int idx) {

  rep(i, 5) {
    if ((int)F[i].size() <= idx) {
      return false;
    }
    if (!F[i][idx]) {
      return false;
    }
  }

  rep(i, 5) F[i].erase(F[i].begin() + idx);

  rep(i, 5) {
    while (1) {
      if (!F[i].empty() && !F[i].back()) {
        F[i].pop_back();
      } else {
        break;
      }
    }
  }

  return true;
}

int main() {

  for (int N; cin >> N && N;) {
    rep(i, 5) F[i].clear();
    rep(i, N) {
      int type, len, pos;
      cin >> type >> len >> pos;
      pos--;
      if (type == 1) {
        int mx = -1;
        REP(i, pos, pos + len) { chmax(mx, int(F[i].size())); }
        REP(i, pos, pos + len) {
          REP(j, F[i].size(), mx) { F[i].push_back(false); }
          F[i].push_back(true);
        }
        eraser(mx);
      } else {
        rep(i, len) { F[pos].push_back(true); }
        int cnt = 0;
        rep(i, len) {
          if (!eraser((int)F[pos].size() - 1 - cnt))
            cnt++;
        }
      }
    }
    int sum = 0;
    rep(i, 5) rep(j, F[i].size()) sum += F[i][j];
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

char field[5010][5];

void add_block(bool const isPortrait, int const len, int const pos) {
  if (isPortrait) {
    for (int i = 0; i < len; i++) {
      field[5000 + len - 1 - i][pos] = '*';
    }
  } else {
    for (int i = 0; i < len; i++) {
      field[5000][pos + i] = '*';
    }
  }
}

void simulate() {
  for (int k = 0; k < 5005; k++) {
    for (int j = 0; j < 5; j++) {
      for (int i = 0; i < 5005; i++) {
        if (field[i][j] == '.' && field[i + 1][j] == '*') {
          field[i][j] = '*';
          field[i + 1][j] = '.';
        }
      }
    }
  }
}

void erase_blocks() {
  for (int i = 0; i < 5005; i++) {
    bool ok = true;
    for (int j = 0; j < 5; j++) {
      ok = ok && field[i][j] == '*';
    }
    if (ok) {
      for (int j = 0; j < 5; j++) {
        field[i][j] = '.';
      }
    } else {
      break;
    }
  }
}

int main() {

  int N;

  while (cin >> N && N) {

    fill(field[0], field[0] + 5010 * 5, '.');

    for (int i = 0; i < N; i++) {
      int d, p, q;
      cin >> d >> p >> q;
      d--;
      q--;
      add_block((bool)d, p, q);
      simulate();
      erase_blocks();
      simulate();
    }

    int ans = 0;
    for (int i = 0; i < 5000; i++) {
      for (int j = 0; j < 5; j++) {
        ans += field[i][j] == '*';
      }
    }

    cout << ans << endl;
  }

  return 0;
}