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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
typedef long long int Int;

class C {
public:
  Int x, y, b;
  C() {}
  C(Int xx, Int yy, Int bb) {
    x = xx;
    y = yy;
    b = bb;
  }
  bool operator<(const C &c) const { return b < c.b; }
};

int main() {
  Int N, d, ans = 0;
  cin >> N >> d;
  vector<C> cs(N);
  REP(i, N) cin >> cs[i].x >> cs[i].y >> cs[i].b;
  SORT(cs);
  multiset<Int> xs, ys;
  for (Int l = 0, r = 0; l < N; l++) {
    // cout <<cs[l].b <<endl;
    for (; r < N && cs[r].b - cs[l].b <= d; r++) {
      xs.insert(cs[r].x);
      ys.insert(cs[r].y);
      ans =
          max(ans, (*xs.rbegin() - *xs.begin()) * (*ys.rbegin() - *ys.begin()));
      // cout <<cs[l].b <<", " <<cs[r].b <<", " <<ans <<endl;
    }
    // cout <<"end" <<endl;
    xs.erase(xs.lower_bound(cs[l].x));
    ys.erase(ys.lower_bound(cs[l].y));
  }
  cout << ans << endl;
  return 0;
}

// 1, 1, 2, 3, 4, 4, 5, 8, 12,

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

class C {
public:
  int x, y, b;
  C() {}
  C(int xx, int yy, int bb) {
    x = xx;
    y = yy;
    b = bb;
  }
  bool operator<(const C &c) const { return b < c.b; }
};

int main() {
  int N, d;
  cin >> N >> d;
  vector<C> cs(N);
  REP(i, N) cin >> cs[i].x >> cs[i].y >> cs[i].b;
  SORT(cs);
  int h = 0, t = 0, ans = 0;
  multiset<int> xs{cs[0].x}, ys{cs[0].y};
  while (h < N - 1 || t < N - 1) {
    if (cs[h].b - cs[t].b <= d)
      ans = max(ans, (*max_element(xs.begin(), xs.end()) -
                      *min_element(xs.begin(), xs.end())) *
                         (*max_element(ys.begin(), ys.end()) -
                          *min_element(ys.begin(), ys.end())));
    if ((h + 1 < N && cs[h + 1].b - cs[t].b <= d) || !(h - t)) {
      xs.insert(cs[++h].x);
      ys.insert(cs[h].y);
    } else {
      xs.erase(xs.lower_bound(cs[t].x));
      ys.erase(ys.lower_bound(cs[t++].y));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00298 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

ll N, C[2000], W[2000];
ll sum[2000], memo[2000][2000];

struct edge {
  ll v, dist;
};

struct state {
  ll v, cost;

  bool operator>(const state s) const { return cost > s.cost; }
};

vector<ll> dijkstra(vector<vector<edge>> &E, ll S) {
  vector<ll> dp(E.size(), INF);
  priority_queue<state, vector<state>, greater<state>> q;
  q.push((state){S, 0});

  while (!q.empty()) {
    ll v = q.top().v, cost = q.top().cost;
    q.pop();

    if (dp[v] <= cost)
      continue;
    dp[v] = cost;

    REP(i, 0, E[v].size()) {
      ll nv = E[v][i].v, ncost = cost + E[v][i].dist;
      if (dp[nv] > ncost)
        q.push((state){nv, ncost});
    }
  }

  return dp;
}

ll dfs(ll l, ll r) {
  ll &ret = memo[l][r];
  if (ret >= 0)
    return ret;

  if (r - l <= 1)
    return ret = 1;

  ret = 0;
  if (dfs(l + 1, r) && (sum[r] - sum[l + 1] <= C[l]))
    ret = 1;
  if (dfs(l, r - 1) && (sum[r - 1] - sum[l] <= C[r - 1]))
    ret = 1;
  return ret;
}

int main(void) {
  cin >> N;
  REP(i, 0, N) cin >> C[i] >> W[i];

  sum[0] = 0;
  REP(i, 0, N) sum[i + 1] = sum[i] + W[i];

  REP(i, 0, N) REP(j, 0, N + 1) memo[i][j] = -1;

  vector<vector<edge>> E(N + 1);
  REP(i, 0, N) REP(j, i, N + 1) if (dfs(i, j)) E[i].push_back((edge){j, 1});
  cout << dijkstra(E, 0)[N] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

ll N, C[2000], W[2000];
ll sum[2000], memo[2000][2000];

struct edge {
  ll v, dist;
};

struct state {
  ll v, cost;

  bool operator>(const state s) const { return cost > s.cost; }
};

vector<ll> dijkstra(vector<vector<edge>> &E, ll S) {
  vector<ll> dp(E.size(), INF);
  priority_queue<state, vector<state>, greater<state>> q;
  q.push((state){S, 0});

  while (!q.empty()) {
    ll v = q.top().v, cost = q.top().cost;
    q.pop();

    if (dp[v] <= cost)
      continue;
    dp[v] = cost;

    REP(i, 0, E[v].size()) {
      ll nv = E[v][i].v, ncost = cost + E[v][i].dist;
      if (dp[nv] > ncost)
        q.push((state){nv, ncost});
    }
  }

  return dp;
}

ll dfs(ll l, ll r) {
  ll &ret = memo[l][r];
  if (ret >= 0)
    return ret;

  if (r - l <= 1)
    return ret = 1;

  ret = 0;
  if (dfs(l + 1, r) && (sum[r] - sum[l + 1] <= C[l]))
    ret = 1;
  if (dfs(l, r - 1) && (sum[r - 1] - sum[l] <= C[r - 1]))
    ret = 1;
  return ret;
}

int main(void) {
  cin >> N;
  REP(i, 0, N) cin >> C[i] >> W[i];

  sum[0] = 0;
  REP(i, 0, N) sum[i + 1] = sum[i] + W[i];

  REP(i, 0, N) REP(j, 0, N + 1) memo[i][j] = -1;

  vector<vector<edge>> E(N);
  REP(i, 0, N) REP(j, i + 1, N + 1) if (dfs(i, j)) E[i].push_back((edge){j, 1});
  cout << dijkstra(E, 0)[N - 1] << endl;
}
Problem p00298 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 1000000007
#define INF 1000000000
#define SIZE 1000
int n, c[SIZE], w[SIZE];
bool memo[SIZE][SIZE];

void dfs(int l, int r, int now, int weight) {
  if (l < 0 || n <= r)
    return;
  if (memo[l][r])
    return;

  if (c[now] < weight)
    return;

  memo[l][r] = true;

  // cerr << "(" << l << "," << r << ")" << endl;

  weight += w[now];

  dfs(l, r + 1, r + 1, weight);
  dfs(l - 1, r, l - 1, weight);
}

int main() {
  int l[SIZE], r[SIZE];
  int dp[SIZE + 10];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d%d", &c[i], &w[i]);
  }

  for (int i = 0; i < n; i++) {
    dfs(i, i, i, 0);
  }

  for (int i = 0; i <= n; i++) {
    dp[i] = INF;
  }

  dp[0] = 0;

  for (int i = 0; i < n; i++) {

    for (int j = i; j < n; j++) {
      if (memo[i][j])
        dp[j + 1] = min(dp[j + 1], dp[i] + 1);
    }
  }

  cout << dp[n] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <vector>

using namespace std;
typedef long long ll;

#define mod 1000000007
#define INF 1000000000
#define SIZE 1000
int n, c[SIZE], w[SIZE];
bool visit[SIZE][SIZE];
bool memo[SIZE][SIZE];

void dfs(int l, int r, int now, int weight) {
  if (visit[l][r])
    return;

  if (c[now] < weight)
    return;

  visit[l][r] = true;
  memo[l][r] = true;

  // cerr << "(" << l << "," << r << ")" << endl;

  weight += w[now];

  dfs(l, r + 1, r + 1, weight);
  dfs(l - 1, r, l - 1, weight);
}

int main() {
  int l[SIZE], r[SIZE];
  int dp[SIZE + 10];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d%d", &c[i], &w[i]);
  }

  for (int i = 0; i < n; i++) {
    dfs(i, i, i, 0);
  }

  for (int i = 0; i <= n; i++) {
    dp[i] = INF;
  }

  dp[0] = 0;

  for (int i = 0; i < n; i++) {

    for (int j = i; j < n; j++) {
      if (memo[i][j])
        dp[j + 1] = min(dp[j + 1], dp[i] + 1);
    }
  }

  cout << dp[n] << endl;

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

#include "bits/stdc++.h"
#define REP(i, n, N) for (int i = (n); i < (N); i++)
#define RREP(i, n, N) for (ll i = (N - 1); i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const int inf = 1e9;

int dp[1010][1010];
int N;
int C[1010], W[1010];
int sum[1010];
int main() {
  cin >> N;
  REP(i, 0, N) { cin >> C[i] >> W[i]; }
  REP(i, 0, N) REP(j, i, N) dp[i][j] = j - i + 1;
  REP(i, 0, N) sum[i + 1] = sum[i] + W[i];
  REP(k, 1, N) {
    REP(L, 0, N - k) {
      int R = L + k;
      if (dp[L + 1][R] == 1 && sum[R + 1] - sum[L + 1] <= C[L]) {
        dp[L][R] = min(dp[L][R], dp[L + 1][R]);
      }
      if (dp[L][R - 1] == 1 && sum[R] - sum[L] <= C[R]) {
        dp[L][R] = min(dp[L][R], dp[L][R - 1]);
      }
      REP(i, L, R) { dp[L][R] = min(dp[L][R], dp[L][i] + dp[i + 1][R]); }
    }
  }
  p(dp[0][N - 1]);

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define REP(i, n, N) for (int i = (n); i < (N); i++)
#define RREP(i, n, N) for (ll i = (N - 1); i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const int inf = 1e9;

int dp[310][310];
int N;
int C[1010], W[1010];
int sum[1010];
int main() {
  cin >> N;
  REP(i, 0, N) { cin >> C[i] >> W[i]; }
  REP(i, 0, N) REP(j, i, N) dp[i][j] = j - i + 1;
  REP(i, 0, N) sum[i + 1] = sum[i] + W[i];
  REP(k, 1, N) {
    REP(L, 0, N - k) {
      int R = L + k;
      if (dp[L + 1][R] == 1 && sum[R + 1] - sum[L + 1] <= C[L]) {
        dp[L][R] = min(dp[L][R], dp[L + 1][R]);
      }
      if (dp[L][R - 1] == 1 && sum[R] - sum[L] <= C[R]) {
        dp[L][R] = min(dp[L][R], dp[L][R - 1]);
      }
      REP(i, L, R) { dp[L][R] = min(dp[L][R], dp[L][i] + dp[i + 1][R]); }
    }
  }
  p(dp[0][N - 1]);

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

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

int n;
int v[15];
int sum;
char T[4] = "-0+";

int main() {
  cin >> n;
  for (int i = 0; i < 531441; i++) {
    sum = 0;
    for (int j = 0; j < 12; j++) {
      v[j] = (i / (int)(pow(3, j))) % 3;
      sum += (v[j] - 1) * (int)(pow(3, j));
    }
    bool ok = false;
    if (sum == n) {
      for (int j = 11; j >= 0; j--) {
        if (v[j] >= 2) {
          ok = true;
        }
        if (ok == true) {
          cout << T[v[j]];
        }
      }
    }
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int n;
int v[15];
int sum;
char T[4] = "-0+";

int main() {
  cin >> n;
  for (int i = 0; i < 4782969; i++) {
    for (int j = 0; j < 14; j++) {
      v[j] = (i / (int)(pow(3, j))) % 3;
      sum += (v[j] - 1) * (int)(pow(3, j));
    }
    bool ok = false;
    for (int j = 13; j >= 0; j--) {
      if (v[j] >= 2) {
        ok = true;
      }
      if (ok == true) {
        cout << T[v[j]];
      }
    }
  }
  cout << endl;
  return 0;
}
Problem p00301 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n, power[14];
void solve(int x, int v, string S) {
  if (v == n) {
    reverse(S.begin(), S.end());
    printf("%s\\n", S.c_str());
    return;
  }
  if (x == 14)
    return;
  solve(x + 1, v - power[x], S + '-');
  solve(x + 1, v, S + '0');
  solve(x + 1, v + power[x], S + '+');
}
int main() {
  power[0] = 1;
  for (int i = 1; i < 14; i++)
    power[i] = power[i - 1] * 3;
  scanf("%d", &n);
  solve(0, 0, "");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
int n, power[15];
void solve(int x, int v, string S) {
  if (v == n) {
    reverse(S.begin(), S.end());
    printf("%s\\n", S.c_str());
    return;
  }
  if (x == 15)
    return;
  solve(x + 1, v - power[x], S + '-');
  solve(x + 1, v, S + '0');
  solve(x + 1, v + power[x], S + '+');
}
int main() {
  power[0] = 1;
  for (int i = 1; i < 15; i++)
    power[i] = power[i - 1] * 3;
  scanf("%d", &n);
  solve(0, 0, "");
  return 0;
}
Problem p00301 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

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

struct Data {
  string str;
  int w;
  Data() {}
  Data(string str, int w) : str(str), w(w) {}
};

bool done[30][600000];

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

  vector<int> table;
  int tmp = 1;
  table.push_back(tmp);
  while (tmp <= W) {
    tmp *= 3;
    table.push_back(tmp);
  }

  queue<Data> que;
  que.push(Data("", 0));

  while (!que.empty()) {
    Data now = que.front();
    que.pop();

    string str = now.str;
    int w = now.w;
    int len = str.size();

    if (w == W) {
      reverse(str.begin(), str.end());
      cout << str << endl;
      break;
    }

    if (w + 300000 >= 600000 || w + 300000 < 0)
      continue;
    if (done[len][w + 300000])
      continue;
    done[len][w + 300000] = true;

    if (len >= table.size())
      continue;
    que.push(Data(str + "+", w + table[len]));
    que.push(Data(str + "-", w - table[len]));
    que.push(Data(str + "0", w));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

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

struct Data {
  string str;
  int w;
  Data() {}
  Data(string str, int w) : str(str), w(w) {}
};

bool done[20][200002];

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

  vector<int> table;
  int tmp = 1;
  while (tmp <= 1000000) {
    table.push_back(tmp);
    tmp *= 3;
  }

  queue<Data> que;
  que.push(Data("+", 1));
  que.push(Data("-", -1));

  while (!que.empty()) {
    Data now = que.front();
    que.pop();

    string str = now.str;
    int w = now.w;

    if (w == W) {
      reverse(str.begin(), str.end());
      cout << str << endl;
      break;
    }

    if (w < -W || w > W || done[str.size()][w + W])
      continue;
    done[str.size()][w + W] = true;

    if (w + table[str.size()] <= W && w + table[str.size()] >= -W &&
        !done[str.size() + 1][w + table[str.size()] + W])
      que.push(Data(str + "+", w + table[str.size()]));
    if (w - table[str.size()] <= W && w - table[str.size()] >= -W &&
        !done[str.size() + 1][w - table[str.size()] + W])
      que.push(Data(str + "-", w - table[str.size()]));
    if (!done[str.size() + 1][w])
      que.push(Data(str + "0", w));
  }

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

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, R, T;
int P[100];

int pos[100];
int f[1000], e[1000];

signed main() {
  cin >> N >> R >> T;
  rep(i, N) cin >> P[i];

  rep(t, T) {
    rep(i, N) {
      pos[i] = (pos[i] + P[i]) % R;
      if (f[pos[i]])
        f[pos[i]]--;
      if (t)
        e[pos[i]]++;
    }
    rep(i, R) {
      f[i] += e[i];
      e[i] = 0;
    }
  }

  int ans = 0;
  rep(i, R) ans += f[i];
  cout << ans + N << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, R, T;
int P[100];

int pos[100];
int f[100], e[100];

signed main() {
  cin >> N >> R >> T;
  rep(i, N) cin >> P[i];

  rep(t, T) {
    rep(i, N) {
      pos[i] = (pos[i] + P[i]) % R;
      if (f[pos[i]])
        f[pos[i]]--;
      if (t)
        e[pos[i]]++;
    }
    rep(i, R) {
      f[i] += e[i];
      e[i] = 0;
    }
  }

  int ans = 0;
  rep(i, R) ans += f[i];
  cout << ans + N << endl;
  return 0;
}
Problem p00302 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n, r, t;

  long long int ans = 0;

  cin >> n >> r >> t;

  vector<int> em(1000);
  vector<int> fu(1000);
  vector<int> member(100);
  vector<int> pos(100);

  for (int i = 0; i < 1000; i++) {
    em[i] = fu[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> member[i];
    pos[i] = 0;
  }

  for (int k = 0; k < t; k++) {
    for (int i = 0; i < n; i++) {
      pos[i] += member[i];
      pos[i] = pos[i] % r;
      // cout << pos[i] << " " << member[i] << " " << i << endl;
      // cout << pos[i] << endl;
      if (k != 0) {
        // cout << t << endl;
        em[pos[i]]++;
      }
      // cout << fu[pos[i]] << " " << pos[i] << " " << member[i] << " " << i <<
      // endl;
      if (fu[pos[i]] == 0) {
        ans++;
      } else {
        fu[pos[i]]--;
      }
    }
    for (int i = 0; i < r; i++) {
      if (em[i] > 0) {
        fu[i] += em[i];
        em[i] = 0;
      }
      // cout << i << " " << fu[i] << " " << em[i] << endl;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  int n, r, t;

  long long int ans = 0;

  cin >> n >> r >> t;

  vector<int> em(r);
  vector<int> fu(r);
  vector<int> member(n);
  vector<int> pos(n);

  for (int i = 0; i < r; i++) {
    em[i] = fu[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> member[i];
    pos[i] = 0;
  }

  int maxt = t;

  while (t--) {
    for (int i = 0; i < n; i++) {
      pos[i] += member[i];
      if (pos[i] >= r) {
        pos[i] -= r;
      }
      // cout << pos[i] << " " << member[i] << " " << i << endl;
      // cout << pos[i] << endl;
      if (maxt != t + 1) {
        // cout << t << endl;
        em[pos[i]]++;
      }
      // cout << fu[pos[i]] << " " << pos[i] << " " << member[i] << " " << i <<
      // endl;
      if (fu[pos[i]] == 0) {
        ans++;
        fu[pos[i]]++;
      }
      fu[pos[i]]--;
    }
    for (int i = 0; i < r; i++) {
      fu[i] += em[i];
      em[i] = 0;
      // cout << i << " " << fu[i] << " " << em[i] << endl;
    }
  }

  cout << ans << endl;

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

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

bool dfs(int v, int const s, vector<vector<int>> const &es,
         vector<bool> &visited) {
  visited[v] = true;
  bool res = false;
  for (auto &u : es[v]) {
    if (!visited[u]) {
      res |= dfs(u, s, es, visited);
    } else if (visited[v] && u == s) {
      res = true;
      break;
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> es(210);
  for (int i = 0; i < n; ++i) {
    int u, d;
    string s;
    cin >> u >> s >> d;
    d += 100;
    if (s == "lock") {
      es[d].push_back(u);
    } else {
      es[u].push_back(d);
    }
  }
  bool res = false;
  for (int i = 0; i < 101; ++i) {
    vector<bool> visited(210);
    res |= dfs(i, i, es, visited);
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

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

bool dfs(int v, int const s, vector<vector<int>> const &es,
         vector<bool> &visited) {
  visited[v] = true;
  bool res = false;
  for (auto &u : es[v]) {
    if (!visited[u]) {
      res |= dfs(u, s, es, visited);
    } else if (visited[v] && u == s) {
      res = true;
      break;
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> es(200);
  for (int i = 0; i < n; ++i) {
    int u, d;
    string s;
    cin >> u >> s >> d;
    d += 100;
    if (s == "lock") {
      es[d].push_back(u);
    } else {
      es[u].push_back(d);
    }
  }
  bool res = false;
  for (int i = 0; i < 101; ++i) {
    vector<bool> visited(210);
    res |= dfs(i, i, es, visited);
  }
  cout << res << endl;
}
Problem p00303 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef vector<int> vec;
typedef vector<vec> mat;

void add(vector<deque<int>> &v, int x, int y) {
  int next_y = -1;
  if (v[x].size()) {
    next_y = v[x].front();
    v[x].pop_front();
  }
  v[x].push_back(y);
  if (next_y > -1) {
    if (v[next_y].size() == 0)
      return;
    int next_x = v[next_y].front();
    v[next_y].pop_front();
    add(v, next_x, next_y);
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> u(N), d(N), q(N);
  for (int i = 0; i < N; i++) {
    string str;
    cin >> u[i] >> str >> d[i];
    if (str == "wait")
      q[i] = 1;
    u[i] += 99;
    d[i]--;
  }

  vector<deque<int>> v(200);
  bool ans = false;
  for (int i = 0; i < N; i++) {
    if (q[i]) {
      v[d[i]].push_back(u[i]);
    } else {
      add(v, u[i], d[i]);
    }

    for (int j = 100; j < 200; j++) {
      if (v[j].size() == 0)
        continue;
      vector<bool> used(200, false);
      queue<int> que;
      que.push(v[j].front());
      used[j] = true;
      while (que.size()) {
        int now = que.front();
        que.pop();
        if (now == j) {
          ans = true;
          goto FAILURE;
        }
        for (auto k : v[now])
          que.push(k);
        if (used[now])
          break;
        used[now] = true;
      }
    }
  }

FAILURE:

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef vector<int> vec;
typedef vector<vec> mat;

void add(vector<deque<int>> &v, int x, int y) {
  int next_y = -1;
  if (v[x].size()) {
    next_y = v[x].front();
    v[x].pop_front();
  }
  v[x].push_back(y);
  if (next_y > -1) {
    if (v[next_y].size() == 0)
      return;
    int next_x = v[next_y].front();
    v[next_y].pop_front();
    add(v, next_x, next_y);
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> u(N), d(N), q(N);
  for (int i = 0; i < N; i++) {
    string str;
    cin >> u[i] >> str >> d[i];
    if (str == "wait")
      q[i] = 1;
    u[i] += 99;
    d[i]--;
  }

  vector<deque<int>> v(200);
  bool ans = false;
  for (int i = 0; i < N; i++) {
    if (q[i]) {
      v[d[i]].push_back(u[i]);
    } else {
      add(v, u[i], d[i]);
    }

    for (int j = 100; j < 200; j++) {
      if (v[j].size() == 0)
        continue;
      vector<bool> used(200, false);
      queue<int> que;
      que.push(v[j].front());
      used[j] = true;
      while (que.size()) {
        int now = que.front();
        que.pop();
        if (used[now]) {
          ans = true;
          goto FAILURE;
        }
        for (auto k : v[now])
          que.push(k);
      }
    }
  }

FAILURE:

  cout << ans << endl;

  return 0;
}