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

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

struct Guy {
  int m;
  int L;
};

Guy guys[100];
int N, M;

long long ans(int a, int b) {

  if (a == N)
    return 0;
  int ret;
  if ((guys[a].m & b) == 0) {
    ret = max(ans(a + 1, b), ans(a + 1, b | guys[a].m) + guys[a].L);
  } else
    ret = ans(a + 1, b);

  return ret;
}

int main() {
  int L, s, e;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> M >> L;
      guys[i].L = L;
      guys[i].m = 0;
      for (int j = 0; j < M; j++) {
        cin >> s >> e;
        s -= 6;
        e -= 6;
        for (int k = s; k < e; k++) {
          guys[i].m += (1 << k);
        }
      }
    }
    cout << ans(0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

struct Guys {
  int L;
  vector<pair<int, int>> V;
};

bool Sc[24];
int N, M, s, e;
Guys Guy[100];
long long ans;

bool check(int a) {
  for (int i = 0; i < Guy[a].V.size(); i++) {
    for (int j = 0; j <= 23; j++) {
      if (Guy[a].V[i].first <= j && j < Guy[a].V[i].second) {
        if (Sc[j] == false)
          return false;
      }
    }
  }
  return true;
}

void fill(int a) {
  for (int i = 0; i < Guy[a].V.size(); i++) {
    for (int j = Guy[a].V[i].first; j < Guy[a].V[i].second; j++)
      Sc[j] = false;
  }
}

void erase(int a) {
  for (int i = 0; i < Guy[a].V.size(); i++) {
    for (int j = Guy[a].V[i].first; j < Guy[a].V[i].second; j++)
      Sc[j] = true;
  }
}

void dfs(int a, long long sum) {
  if (a == N)
    return;
  if (!check(a)) {
    for (int i = a + 1; i < N; i++)
      dfs(i, sum);
  } else {
    sum += Guy[a].L;
    ans = max(sum, ans);
    fill(a);
    for (int i = a + 1; i < N; i++)
      dfs(i, sum);
    erase(a);
  }
}

int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < 100; i++) {
      Guy[i].V.clear();
    }
    for (int i = 0; i < N; i++) {
      cin >> M >> Guy[i].L;
      for (int j = 0; j < M; j++) {
        cin >> s >> e;
        Guy[i].V.push_back(make_pair(s, e));
      }
    }
    for (int i = 0; i <= 23; i++)
      Sc[i] = true;
    ans = 0;
    for (int i = 0; i < N; i++)
      dfs(i, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p01438 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long lint;

lint dp[1 << 16];

int main() {
  int N;

  while (scanf("%d", &N) && N) {
    memset(dp, 0, sizeof(dp));
    lint ans = 0;

    for (int i = 0; i < N; i++) {
      int M, b = 0;
      lint l;
      scanf("%d %lld", &M, &l);
      for (int j = 0; j < M; j++) {
        int x, y;
        scanf("%d %d", &x, &y);
        for (int k = x - 6; k < y - 6; k++)
          b |= (1 << k);
      }

      for (int j = (1 << 16) - 1; j >= 0; j--) {
        if ((j & b) == 0) {
          dp[j] = max(dp[j], dp[j ^ b] + l);
          ans = max(ans, dp[j]);
        }
      }
    }
    printf("%lld\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long lint;

int b[128];
lint l[128];
lint memo[128][1 << 17];

lint getMax(int pos, int bits) {
  if (pos < 0)
    return (0);
  if (memo[pos][bits] >= 0)
    return (memo[pos][bits]);

  lint &res = memo[pos][bits];

  res = getMax(pos - 1, bits);
  if ((bits & b[pos]) == 0)
    res = max(res, getMax(pos - 1, bits ^ b[pos]) + l[pos]);

  return (res);
}

int main() {
  int N;

  while (scanf("%d", &N) && N) {
    memset(b, 0, sizeof(b));
    memset(memo, -1, sizeof(memo));

    for (int i = 0; i < N; i++) {
      int M;
      scanf("%d %lld", &M, l + i);
      for (int j = 0; j < M; j++) {
        int x, y;
        scanf("%d %d", &x, &y);
        for (int k = x - 6; k < y - 6; k++)
          b[i] |= (1 << k);
      }
    }

    printf("%lld\\n", getMax(N - 1, 0));
  }
}
Problem p01438 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define allof(a) a.begin(), a.end()
#define minit(a, b) memset(a, b, sizeof(a))

typedef long long lint;
typedef pair<int, int> pii;

int N;
int M[110], L[110];
int S[110][20], E[110][20];

int T[110];
lint dp[2][(1 << 17)];

void solve() {
  for_(i, 0, N) {
    int t = 0;
    for_(j, 0, M[i]) {
      int s = S[i][j], e = E[i][j];
      s -= 6;
      e -= 6;
      for_(k, s, e) t |= 1 << k;
    }
    T[i] = t;
  }

  minit(dp, -1);
  dp[0][0] = 0;

  for_(i, 0, N) {
    int nw = i % 2, nx = (i + 1) % 2;
    int t = T[i];

    for_(j, 0, (1 << 17)) {
      if (dp[nw][j] < 0)
        continue;

      dp[nx][j] = max(dp[nx][j], dp[nw][j]);

      int ands = j & t;
      if (ands != 0)
        continue;

      int ors = j | t;
      dp[nx][ors] = max(dp[nx][ors], dp[nw][j] + (lint)L[i]);
    }
  }

  lint ans = 0;
  for_(i, 0, (1 << 17)) ans = max(ans, dp[N % 2][i]);
  cout << ans << endl;
}

int main() {
  while (cin >> N, N) {
    for_(i, 0, N) {
      cin >> M[i] >> L[i];
      for_(j, 0, M[i]) { cin >> S[i][j] >> E[i][j]; }
    }
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define allof(a) a.begin(), a.end()
#define minit(a, b) memset(a, b, sizeof(a))

typedef long long lint;
typedef pair<int, int> pii;

int N;
int M[110], L[110];
int S[110][20], E[110][20];

int T[110];
lint dp[110][(1 << 17)];

void solve() {
  for_(i, 0, N) {
    int t = 0;
    for_(j, 0, M[i]) {
      int s = S[i][j], e = E[i][j];
      s -= 6;
      e -= 6;
      for_(k, s, e) t |= 1 << k;
    }
    T[i] = t;
  }

  minit(dp, -1);
  dp[0][0] = 0;

  for_(i, 0, N) {
    int t = T[i];

    for_(j, 0, (1 << 17)) {
      if (dp[i][j] < 0)
        continue;

      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

      int ands = j & t;
      if (ands != 0)
        continue;

      int ors = j | t;
      dp[i + 1][ors] = max(dp[i + 1][ors], dp[i][j] + (lint)L[i]);
    }
  }

  lint ans = 0;
  for_(i, 0, (1 << 17)) ans = max(ans, dp[N][i]);
  cout << ans << endl;
}

int main() {
  while (cin >> N, N) {
    for_(i, 0, N) {
      cin >> M[i] >> L[i];
      for_(j, 0, M[i]) { cin >> S[i][j] >> E[i][j]; }
    }
    solve();
  }
  return 0;
}
Problem p01439 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

pair<string, int> split(string s) {
  char t[99];
  int d;
  sscanf(s.c_str(), "%[a-z]%d", t, &d);
  return make_pair(string(t), d);
}

int main() {
  for (int N; cin >> N, N;) {
    int cnt = 0;
    stack<int> s;
    string m[10001];
    map<pair<string, int>, vector<int>> mmap;
    for (int i = 1; i <= N; i++) {
      cin >> m[i];
      if (m[i].back() == 'v') {
      } else if (m[i].size() > 1) {
        auto p = split(m[i]);
        if (p.second == 1) {
          s.push(i);
        } else {
          mmap[p].push_back(i);
        }
      } else {
        s.push(i);
      }
      while (!s.empty()) {
        int x = s.top();
        s.pop();
        if (x > 0) {
          cout << x << endl;
          cnt++;
          if (m[x].size() > 1) {
            s.push(-x);
          }
          if (x && m[x - 1].back() == 'v') {
            s.push(x - 1);
          }
        } else {
          x = -x;
          auto p = split(m[x]);
          p.second++;
          if (!mmap[p].empty()) {
            auto it = upper_bound(begin(mmap[p]), end(mmap[p]), x);
            if (it != begin(mmap[p])) {
              s.push(*--it);
              mmap[p].erase(it);
            }
          }
        }
      }
    }
    //    cerr<<cnt<<' '<<N<<endl;
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <stack>
#include <utility>

using namespace std;

pair<string, int> split(string s) {
  char t[99];
  int d;
  sscanf(s.c_str(), "%[a-z]%d", t, &d);
  return make_pair(string(t), d);
}

int main() {
  for (int N; cin >> N, N;) {
    stack<int> s;
    string m[10001];
    map<pair<string, int>, int> mmap;
    for (int i = 1; i <= N; i++) {
      cin >> m[i];
      if (m[i].back() == 'v') {
      } else if (m[i].size() > 1) {
        auto p = split(m[i]);
        if (p.second == 1) {
          s.push(i);
        } else {
          if (mmap.count(p))
            return 9;
          mmap[p] = i;
        }
      } else {
        s.push(i);
      }
      while (!s.empty()) {
        int x = s.top();
        s.pop();
        cout << x << endl;
        if (m[x].size() > 1) {
          auto p = split(m[x]);
          mmap.erase(p);
          p.second++;
          if (mmap.count(p)) {
            s.push(mmap[p]);
          }
        }
        if (m[x - 1].back() == 'v') {
          s.push(x - 1);
        }
      }
    }
  }
}
Problem p01439 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

struct Letter {
  bool re;
  int num;
  string s;
};

Letter L[100000];
bool used[100000];
map<pair<string, int>, vector<int>> Pos;
map<pair<string, int>, vector<int>>::iterator it;
vector<int>::iterator jt;

void dfs(int i) {
  used[i] = true;
  printf("%d\\n", i + 1);

  if (i > 0 && L[i - 1].re && !used[i - 1])
    dfs(i - 1);

  pair<string, int> next(L[i].s, L[i].num + 1);
  if ((it = Pos.find(next)) != Pos.end()) {
    jt = lower_bound(it->second.begin(), it->second.end(), i);
    if (jt != it->second.begin()) {
      int j = *--jt;
      it->second.erase(jt);
      dfs(j);
      return;
    }
  }
}

int main() {
  for (int n; cin >> n, n;) {
    Pos.clear();
    rep(i, n) {
      used[i] = false;
      string s;
      cin >> s;

      bool re;
      if (*s.rbegin() == 'v') {
        re = true;
        s.erase(--s.end());
      } else
        re = false;

      int j;
      for (j = (int)s.length() - 1; j >= 0 && isdigit(s[j]); j--)
        ;
      int num = -1;
      if (j < (int)s.length() - 1)
        num = atoi(s.c_str() + j + 1);
      s = s.substr(0, j + 1);

      L[i] = (Letter){re, num, s};

      if (L[i].num > 1)
        Pos[make_pair(L[i].s, L[i].num)].push_back(i);
    }

    rep(i, n) if (!L[i].re && L[i].num <= 1 && !used[i]) dfs(i);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <string>

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

using namespace std;

struct Letter {
  bool re;
  int num;
  string s;
};

Letter L[100000];
map<pair<string, int>, int> Pos;

void dfs(int i) {
  printf("%d\\n", i + 1);

  pair<string, int> next(L[i].s, L[i].num + 1);
  if (Pos.count(next)) {
    dfs(Pos[next]);
  } else if (i > 0 && L[i - 1].re) {
    dfs(i - 1);
  }
}

int main() {
  for (int n; cin >> n, n;) {
    Pos.clear();
    rep(i, n) {
      string s;
      cin >> s;

      bool re;
      if (*s.rbegin() == 'v') {
        re = true;
        s.erase(--s.end());
      } else
        re = false;

      int j;
      for (j = (int)s.length() - 1; j >= 0; j--)
        if (!isdigit(s[j]))
          break;
      int num = -1;
      if (j < (int)s.length() - 1)
        sscanf(s.c_str() + j + 1, "%d", &num);
      s = s.substr(0, j + 1);

      L[i] = (Letter){re, num, s};
    }

    rep(i, n) {
      if (L[i].re)
        continue;
      if (L[i].num > 1) {
        Pos[make_pair(L[i].s, L[i].num)] = i;
        continue;
      }
      dfs(i);
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

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

using namespace std;

int M;

template <class T> class FenwickTree {
  vector<T> a;

public:
  FenwickTree(int n) : a(n) {}
  T sum(int i, int j) const {
    if (i == 0) {
      T s = 0;
      for (; j >= 0; j = (j & (j + 1)) - 1)
        s = (s + a[j]) % M;
      return s;
    }
    return (sum(0, j) - sum(0, i - 1) + M) % M;
  }
  void add(int i, T v) {
    for (; i < a.size(); i |= i + 1)
      a[i] = (a[i] + v) % M;
  }
};

int ofset;
char s[100001][16];
bool lex(int a, int b) { return strcmp(s[a - ofset], s[b - ofset]) < 0; }

int main() {
  for (int a, b; scanf("%d%d%d", &a, &b, &M), a;) {
    int n = b - a + 1, seq[100000];
    ofset = a;
    rep(i, n) {
      seq[i] = a + i;
      sprintf(s[i], "%d", seq[i]);
    }
    sort(seq, seq + n, lex);

    FenwickTree<int> F(b - a + 1);
    rep(i, n) F.add(seq[i] - a, F.sum(0, seq[i] - a) + 1);

    printf("%d\\n", F.sum(0, n - 1));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

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

using namespace std;

int M;

template <class T> class FenwickTree {
  vector<T> a;

public:
  FenwickTree(int n) : a(n) {}
  T sum(int i, int j) const {
    if (i == 0) {
      T s = 0;
      for (; j >= 0; j = (j & (j + 1)) - 1)
        s = (s + a[j]) % M;
      return s;
    }
    return ((sum(0, j) - sum(0, i - 1)) % M + M) % M;
  }
  void add(int i, T v) {
    for (; i < a.size(); i |= i + 1)
      a[i] = (a[i] + v) % M;
  }
  void clear() { rep(i, a.size()) a[i] = 0; }
};

bool lex(int a, int b) {
  char s[16], t[16];
  sprintf(s, "%d", a);
  sprintf(t, "%d", b);
  return strcmp(s, t) < 0;
}

int main() {
  int n, seq[100000];
  FenwickTree<int> F(100001);

  for (int a, b; scanf("%d%d%d", &a, &b, &M), a;) {
    n = b - a + 1;
    rep(i, n) seq[i] = a + i;
    sort(seq, seq + n, lex);

    F.clear();
    rep(i, n) F.add(seq[i] - a, F.sum(0, seq[i] - a) + 1);

    printf("%d\\n", F.sum(0, n - 1));
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int bit[100100];
int N, P;

void add(int k, int x) {
  for (; k <= N + 1; k += k & -k) {
    bit[k] = (bit[k] + x) % P;
  }
}

int sum(int k) {
  int ret = 0;
  for (; k; k &= (k - 1)) {
    ret = (ret + bit[k]) % P;
  }
  return (ret);
}

bool radix(int a, int b) {
  if ((int)log10(a) == (int)log10(b))
    return (a < b);
  return (to_string(a) < to_string(b));
}

int main() {
  int A, B;
  int v[100000];

  while (scanf("%d %d %d", &A, &B, &P) && A) {
    memset(bit, 0, sizeof(bit));
    N = B - A + 1;
    add(N + 1, 1);

    for (int i = 0; i < N; i++) {
      v[i] = A + i;
    }

    sort(v, v + N, radix);

    for (int i = N - 1; i >= 0; i--) {
      int nv = (sum(N + 1) - sum(v[i] - A) + P) % P;
      add(v[i] - A + 1, nv);
    }

    printf("%d\\n", sum(N));
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

int bit[100100];
int N, P;

void add(int k, int x) {
  for (; k <= N + 1; k += k & -k) {
    bit[k] = (bit[k] + x) % P;
  }
}

int sum(int k) {
  int ret = 0;
  for (; k; k &= (k - 1)) {
    ret = (ret + bit[k]) % P;
  }
  return (ret);
}

bool radix(int a, int b) { return (to_string(a) < to_string(b)); }

int main() {
  int A, B;
  int v[100000];

  while (scanf("%d %d %d", &A, &B, &P) && A) {
    memset(bit, 0, sizeof(bit));
    N = B - A + 1;
    add(N + 1, 1);

    for (int i = 0; i < N; i++) {
      v[i] = A + i;
    }

    sort(v, v + N, radix);

    for (int i = N - 1; i >= 0; i--) {
      int nv = (sum(N + 1) - sum(v[i] - A) + P) % P;
      add(v[i] - A + 1, nv);
    }

    printf("%d\\n", sum(N));
  }

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

// Earth Invasion Diary of Miyabi-sensei
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll N;
  cin >> N;
  cout << ceil(log((double)N) / log(3.0)) << endl;
  return 0;
}

Error: TLE

Fixed submission:

// Earth Invasion Diary of Miyabi-sensei
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  ll N;
  while (cin >> N, N != 0)
    cout << ceil(log((double)N) / log(3.0)) << endl;
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define min(a, b) a > b ? b : a

int main(void) {
  int n, count = 0;
  scanf("%d", &n);
  while (n != 1) {
    n = n % 3 == 0 ? n / 3 : n / 3 + 1;
    count++;
  }
  printf("%d\\n", count);
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#define min(a, b) a > b ? b : a

int solve(int n) {
  int m = 0x3FFFFFFF;
  static int count = 0;
  if (n == 1)
    return count;
  count++;
  if (n != 4 && n != 5)
    m = min(solve(n / 3), m);
  if (n % 3 != 0)
    m = min(solve(n / 3 + 1), m);
  count--;
  return m;
}

int main(void) {
  int n;
  scanf("%d", &n);
  printf("%d\\n", solve(n));
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

//????¬????dp

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

  int ans = 0;
  while (n != 1) {
    int r = n % 3;
    n = n / 3;
    if (r > 0)
      n++;

    ans++;
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

//????¬????dp

int main() {
  int n;
  cin >> n;
  vector<int> dp(n + 1);

  // dp?????????
  dp[1] = 0;
  dp[2] = 1;

  // dp
  for (int i = 3; i <= n; i++) {
    int b = i / 3;
    int r = i % 3;
    if (r > 0)
      b++;
    dp[i] = dp[b] + 1;
  }

  cout << dp[n] << endl;
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int solve(int n) {
  if (n < 2) {
    return 0;
  }
  return 1 + solve((n + 2) / 3);
}

int main() {
  int N;
  cin >> N;
  int ans = solve(N);
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int solve(int n) {
  if (n == 0) {
    return 0;
  }
  return 1 + solve(n / 3);
}

int main() {
  while (true) {
    int N;
    cin >> N;
    if (N == 0) {
      break;
    }
    int ans = solve(N);
    cout << ans << endl;
  }
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {

  int n;

  cin >> n;
  int c = 0;
  while (n > 3) {
    if (n % 3 == 0)
      n = n / 3;
    else {
      n = n / 3 + 1;
    }
    c++;
  }
  if (n > 1)
    c++;
  cout << c << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int c = 0;
    while (n > 3) {
      if (n % 3 == 0)
        n = n / 3;
      else {
        n = n / 3 + 1;
      }
      c++;
    }
    if (n > 1)
      c++;
    cout << c << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  while (n > 1)
    n = n / 3 + min(1, n % 3), ans++;
  cout << ans << endl;
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  while (n > 1)
    n = n / 3 + min(1, n % (n / 3)), ans++;
  cout << ans << endl;
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, i = 0;
  cin >> n;
  while (1) {
    if (n == 2) {
      i++;
      break;
    }
    if (n % 3 == 1) {
      n = n / 3;
      n++;
    } else if (n % 3 == 2)
      n = (n + 1) / 3;
    else
      n = n / 3;
    i++;
    if (n == 1)
      break;
  }
  cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, i = 0;
  cin >> n;
  while (1) {
    n = n / 3;
    i++;
    if (n == 1)
      break;
    if (n % 3 == 1)
      n++;
  }
  cout << i << endl;
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int calc(int n) {
  return n <= 1 ? 0 : (n % 3 ? calc(n / 3 + 1) : calc(n / 3)) + 1;
}

int main() {
  for (int N; cin >> N; cout << calc(N) << '\\n')
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int calc(int n) {
  return n <= 1 ? 0 : (n % 3 ? calc(n / 3 + 1) : calc(n / 3)) + 1;
}

int main() {
  for (int N; cin >> N, N; cout << calc(N) << '\\n')
    ;
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int ret = 0;
    while (n != 1) {
      if (n % 3 == 0) {
        n /= 3;
      } else {
        n /= 3;
        n++;
      }
      ret++;
    }
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ret = 0;
    while (n != 1) {
      if (n % 3 == 0) {
        n /= 3;
      } else {
        n /= 3;
        n++;
      }
      ret++;
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p01447 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  ll x = 3;
  int ans = 1;
  while (n > x) {
    ans++;
    x *= 3;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main() {
  int n;
  while (cin >> n, n) {
    ll x = 3;
    int ans = 1;
    while (n > x) {
      ans++;
      x *= 3;
    }
    cout << ans << endl;
  }
}
Problem p01448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  static int num[100010];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    ++num[a];
    --num[b + 1];
  }
  int ans = 0;
  int acc = 0;
  for (int i = 0; i <= N + 1; i++) {
    acc += num[i];
    if (acc >= i - 1) {
      ans = i - 1;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  int num[100001];

  int sum;

  cin >> N;

  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = a; j <= b; j++) {
      num[j]++;
    }
  }

  for (int i = N + 1; i > 0; i--) {
    if (num[i] >= i - 1) {
      cout << i - 1 << endl;
      break;
    }
  }

  return 0;
}