prithivMLmods/Coder-Stat huggingface dataset:
Problem p01648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
class DisjointSet {
private:
  vector<int> rank, p;
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }

public:
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      p[i] = i, rank[i] = 0;
  }
  void Union(int x, int y) {
    if (Find(x) != Find(y))
      link(Find(x), Find(y));
  }
  int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); }
};

struct edge {
  int u, v, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<edge> edges;
    for (int i = 0; i < m; i++) {
      int s, t, c;
      cin >> s >> t >> c;
      edges.push_back((edge){s, t, c});
    }
    sort(edges.begin(), edges.end());
    DisjointSet UnionFind(n);
    vector<int> cost;
    for (int i = 0; i < edges.size(); i++) {
      edge &e = edges[i];
      if (UnionFind.Find(e.u) != UnionFind.Find(e.v)) {
        UnionFind.Union(e.u, e.v);
        cost.push_back(e.cost);
      }
    }
    if (cost.size() % 2 == 0) {
      cout << (cost[cost.size() / 2] + cost[cost.size() / 2 + 1]) / 2 << endl;
    } else {
      cout << cost[cost.size() / 2] << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
class DisjointSet {
private:
  vector<int> rank, p;
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }

public:
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      p[i] = i, rank[i] = 0;
  }
  void Union(int x, int y) {
    if (Find(x) != Find(y))
      link(Find(x), Find(y));
  }
  int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); }
};

struct edge {
  int u, v, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<edge> edges;
    for (int i = 0; i < m; i++) {
      int s, t, c;
      cin >> s >> t >> c;
      edges.push_back((edge){s - 1, t - 1, c});
    }
    sort(edges.begin(), edges.end());
    DisjointSet UnionFind(n);
    vector<int> cost;
    for (int i = 0; i < edges.size(); i++) {
      edge &e = edges[i];
      if (UnionFind.Find(e.u) != UnionFind.Find(e.v)) {
        UnionFind.Union(e.u, e.v);
        cost.push_back(e.cost);
      }
    }
    if (cost.size() % 2 == 0) {
      cout << (cost[cost.size() / 2] + cost[cost.size() / 2 + 1]) / 2 << endl;
    } else {
      cout << cost[cost.size() / 2] << endl;
    }
  }
}
Problem p01656 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  map<int, string> nm;
  nm[0] = "kogakubu10gokan";
  for (int i = 0; i < n; i++) {
    int num;
    string s;
    cin >> num >> s;
    nm[num] = s;
  }
  bool ju = true;
  map<int, string>::iterator itr = ++nm.begin();
  for (; itr != nm.end(); itr++) {
    if (itr->first > q) {
      cout << (--itr)->second << endl;
      ju = false;
      break;
    }
  }
  if (ju)
    cout << nm.end()->second << endl;
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  map<int, string> nm;
  nm[0] = "kogakubu10gokan";
  for (int i = 0; i < n; i++) {
    int num;
    string s;
    cin >> num >> s;
    nm[num] = s;
  }
  bool ju = true;
  map<int, string>::iterator itr = ++nm.begin();
  for (; itr != nm.end(); itr++) {
    if (itr->first > q) {
      cout << (--itr)->second << endl;
      ju = false;
      break;
    }
  }
  if (ju)
    cout << (--nm.end())->second << endl;
}
Problem p01656 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (ll i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (ll i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

int main() {
  int n, m;
  cin >> n >> m;
  map<int, string> mp;
  int t;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> t >> s;
    mp[t] = s;
  }
  auto it = mp.upper_bound(m);
  it--;
  cout << it->se << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (ll i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (ll i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

int main() {
  int n, m;
  cin >> n >> m;
  map<int, string> mp;
  int t;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> t >> s;
    mp[t] = s;
  }
  mp[0] = "kogakubu10gokan";
  auto it = mp.upper_bound(m);
  it--;
  cout << it->se << endl;
}
Problem p01657 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)

typedef vector<int> vi;

pair<vi, int> solve(int n, int x, int m, vi const &l, vi const &r,
                    vi const &s) {
  vi ans;
  int ans_sum = -1;
  int end = x + 1;
  rep(i, n - 1) end *= x + 1;
  rep(q, end) {
    int mask = q;
    vi v(n);
    rep(i, n) {
      v[i] = mask % (x + 1);
      mask /= (x + 1);
    }
    vi sum(n);
    sum[0] = v[0];
    loop(i, 1, n) sum[i] = sum[i - 1] + v[i];

    bool ok = true;
    rep(i, m) {
      int a = sum[r[i]];
      if (l[i] != 0)
        a -= sum[l[i] - 1];
      if (a == s[i])
        continue;
      ok = false;
      break;
    }
    if (ok && sum.back() > ans_sum) {
      ans_sum = sum.back();
      swap(ans, v);
    }
  }
  return make_pair(ans, ans_sum);
}

int main() {
  int n, x, m;
  while (cin >> n >> x >> m) {
    vi l(n), r(n), s(n);
    rep(i, m) {
      cin >> l[i] >> r[i] >> s[i];
      l[i]--;
      r[i]--;
    }
    auto t = solve(n, x, m, l, r, s);
    if (t.second != -1) {
      rep(i, t.first.size() - 1) cout << t.first[i] << " ";
      cout << t.first.back() << endl;
    } else {
      cout << -1 << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)

typedef vector<int> vi;

pair<vi, int> solve(int n, int x, int m, vi const &l, vi const &r,
                    vi const &s) {
  vi ans;
  int ans_sum = -1;
  int end = x + 1;
  rep(i, n - 1) end *= x + 1;
  rep(q, end) {
    int mask = q;
    vi v(n);
    rep(i, n) {
      v[i] = mask % (x + 1);
      mask /= (x + 1);
    }
    vi sum(n);
    sum[0] = v[0];
    loop(i, 1, n) sum[i] = sum[i - 1] + v[i];

    bool ok = true;
    rep(i, m) {
      int a = sum[r[i]];
      if (l[i] != 0)
        a -= sum[l[i] - 1];
      if (a == s[i])
        continue;
      ok = false;
      break;
    }
    if (ok && sum.back() > ans_sum) {
      ans_sum = sum.back();
      swap(ans, v);
    }
  }
  return make_pair(ans, ans_sum);
}

int main() {
  int n, x, m;
  while (cin >> n >> x >> m) {
    vi l(m), r(m), s(m);
    rep(i, m) {
      cin >> l[i] >> r[i] >> s[i];
      l[i]--;
      r[i]--;
    }
    auto t = solve(n, x, m, l, r, s);
    if (t.second != -1) {
      rep(i, t.first.size() - 1) cout << t.first[i] << " ";
      cout << t.first.back() << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
Problem p01657 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int n, m, x;
  int l[10], r[10], s[10];
  while (cin >> n >> x >> m) {
    for (int i = 0; i < n; i++) {
      cin >> l[i] >> r[i] >> s[i];
    }
    x++;
    int end = 1;
    for (int i = 0; i < n; i++)
      end *= x;
    int maxSum = -1;
    int ans[10];
    int res[10];
    for (int i = 0; i < end; i++) {
      int t = i;
      for (int j = 0; j < n; j++) {
        ans[j] = t % x;
        t /= x;
      }
      bool check = true;
      for (int j = 0; j < m; j++) {
        int sum = 0;
        for (int k = l[j]; k <= r[j]; k++) {
          sum += ans[k - 1];
        }
        if (sum != s[j])
          check = false;
      }
      if (check) {
        int allSum = 0;
        for (int j = 0; j < n; j++)
          allSum += ans[j];
        if (allSum > maxSum) {
          maxSum = allSum;
          for (int j = 0; j < n; j++)
            res[j] = ans[j];
        }
      }
    }
    if (maxSum >= 0) {
      for (int i = 0; i < n - 1; i++)
        cout << res[i] << " ";
      cout << res[n - 1] << endl;
    } else {
      cout << -1 << endl;
    }
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n, m, x;
  int l[10], r[10], s[10];
  while (cin >> n >> x >> m) {
    for (int i = 0; i < m; i++) {
      cin >> l[i] >> r[i] >> s[i];
    }
    x++;
    int end = 1;
    for (int i = 0; i < n; i++)
      end *= x;
    int maxSum = -1;
    int ans[10];
    int res[10];
    for (int i = 0; i < end; i++) {
      int t = i;
      for (int j = 0; j < n; j++) {
        ans[j] = t % x;
        t /= x;
      }
      bool check = true;
      for (int j = 0; j < m; j++) {
        int sum = 0;
        for (int k = l[j]; k <= r[j]; k++) {
          sum += ans[k - 1];
        }
        if (sum != s[j])
          check = false;
      }
      if (check) {
        int allSum = 0;
        for (int j = 0; j < n; j++)
          allSum += ans[j];
        if (allSum > maxSum) {
          maxSum = allSum;
          for (int j = 0; j < n; j++)
            res[j] = ans[j];
        }
      }
    }
    if (maxSum >= 0) {
      for (int i = 0; i < n - 1; i++)
        cout << res[i] << " ";
      cout << res[n - 1] << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
Problem p01659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

int N;
int a[200010];

int main() {
  cin >> N;
  for (int j = 0; j < N; ++j) {
    cin >> a[j];
  }

  stack<int> s;

  int cnt = 1;

  s.push(a[0]);
  for (int j = 1; j < N; ++j) {
    int t = s.top();
    while (t > a[j]) {
      s.pop();
      t = s.top();
    }
    if (t < a[j]) {
      ++cnt;
      s.push(a[j]);
    }
  }

  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

int N;
int a[200010];

int main() {
  cin >> N;
  for (int j = 0; j < N; ++j) {
    cin >> a[j];
  }

  stack<int> s;

  int cnt = 1;

  s.push(0);
  s.push(a[0]);
  for (int j = 1; j < N; ++j) {
    int t = s.top();
    while (t > a[j]) {
      s.pop();
      t = s.top();
    }
    if (t < a[j]) {
      ++cnt;
      s.push(a[j]);
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p01659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int n, res, a[21600];
set<int> nums;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    nums.insert(a[i]);
    while (*nums.rbegin() > a[i]) {
      res++;
      nums.erase(*nums.rbegin());
    }
  }
  cout << res + nums.size() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int n, res, a[216000];
set<int> nums;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    nums.insert(a[i]);
    while (*nums.rbegin() > a[i]) {
      res++;
      nums.erase(*nums.rbegin());
    }
  }
  cout << res + nums.size() << endl;
  return 0;
}
Problem p01667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>

using namespace std;
char str[3];
pair<int, int> v[100][100];
int g[100][100];
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 0; i < a; i++) {
    int c;
    scanf("%d", &c);
    for (int j = 0; j < b; j++)
      v[i][j] = make_pair(0, 100);
    for (int j = 0; j < c; j++) {
      int d, e;
      scanf("%d%s%d", &d, str, &e);
      d--;
      if (str[0] == '>') {
        v[i][d].first = max(v[i][d].first, e);
      } else
        v[i][d].second = min(v[i][d].second, e);
    }
  }
  bool ok = true;
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++)
      if (v[i][j].first > v[i][j].second)
        ok = false;
  for (int i = 0; i < a; i++)
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < b; k++)
        if (v[i][k].second < v[j][k].first)
          g[i][j] = 1;
    }
  for (int k = 0; k < a; k++)
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        g[i][j] |= (g[i][k] & g[k][j]);
  for (int i = 0; i < a; i++)
    if (g[i][i])
      ok = false;
  for (int i = 0; ok && i < a; i++) {
    for (int j = 0; ok && j < a; j++) {
      for (int k = 0; ok && k < b; k++) {
        for (int l = 0; ok && l < b; l++) {
          if (v[i][k].second < v[j][k].first && v[j][l].second < v[i][l].first)
            ok = false;
          if (v[j][k].second < v[i][k].first && v[i][l].second < v[j][l].first)
            ok = false;
        }
      }
    }
  }
  if (ok)
    printf("Yes\n");
  else
    printf("No\n");
}

Fixed submission:
#include <algorithm>
#include <stdio.h>

using namespace std;
char str[10];
pair<int, int> v[110][110];
int g[110][110];
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 0; i < a; i++) {
    int c;
    scanf("%d", &c);
    for (int j = 0; j < b; j++)
      v[i][j] = make_pair(0, 100);
    for (int j = 0; j < c; j++) {
      int d, e;
      scanf("%d%s%d", &d, str, &e);
      d--;
      if (str[0] == '>') {
        v[i][d].first = max(v[i][d].first, e);
      } else
        v[i][d].second = min(v[i][d].second, e);
    }
  }
  bool ok = true;
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++)
      if (v[i][j].first > v[i][j].second)
        ok = false;
  for (int i = 0; i < a; i++)
    for (int j = 0; j < a; j++) {
      for (int k = 0; k < b; k++)
        if (v[i][k].second < v[j][k].first)
          g[i][j] = 1;
    }
  for (int k = 0; k < a; k++)
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        g[i][j] |= (g[i][k] & g[k][j]);
  for (int i = 0; i < a; i++)
    if (g[i][i])
      ok = false;
  for (int i = 0; ok && i < a; i++) {
    for (int j = 0; ok && j < a; j++) {
      for (int k = 0; ok && k < b; k++) {
        for (int l = 0; ok && l < b; l++) {
          if (v[i][k].second < v[j][k].first && v[j][l].second < v[i][l].first)
            ok = false;
          if (v[j][k].second < v[i][k].first && v[i][l].second < v[j][l].first)
            ok = false;
        }
      }
    }
  }
  if (ok)
    printf("Yes\n");
  else
    printf("No\n");
}
Problem p01667 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

bool f = 1, e[100][100];
int n;

void dfs(int now, int par, VI visit) {
  if (visit[now]) {
    if (now == par)
      f = 0;
    return;
  }
  visit[now] = 1;
  REP(i, n) {
    if (e[now][i])
      dfs(i, par, visit);
  }
}

int main() {
  int m;
  cin >> n >> m;
  VVI h(n, VI(m, 1000)), l(n, VI(m));
  REP(i, n) {
    int k;
    cin >> k;
    while (k--) {
      int j, x;
      string s;
      cin >> j >> s >> x;
      if (s == ">=")
        l[i][j - 1] = max(l[i][j - 1], x);
      else
        h[i][j - 1] = min(h[i][j - 1], x);
    }
  }
  REP(i, n) REP(j, m) if (l[i][j] > h[i][j]) f = 0;
  REP(i, n) REP(j, n) {
    bool g = 0;
    REP(k, m) if (l[i][k] > h[j][k]) g = 1;
    e[i][j] = g;
  }

  REP(i, n) {
    VI visit(n);
    dfs(i, i, visit);
  }

  cout << (f ? "Yes" : "No") << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

bool f = 1, e[100][100];
int n;

void dfs(int now, int par, VI &visit) {
  if (visit[now]) {
    if (now == par)
      f = 0;
    return;
  }
  visit[now] = 1;
  REP(i, n) {
    if (e[now][i])
      dfs(i, par, visit);
  }
}

int main() {
  int m;
  cin >> n >> m;
  VVI h(n, VI(m, 1000)), l(n, VI(m));
  REP(i, n) {
    int k;
    cin >> k;
    while (k--) {
      int j, x;
      string s;
      cin >> j >> s >> x;
      if (s == ">=")
        l[i][j - 1] = max(l[i][j - 1], x);
      else
        h[i][j - 1] = min(h[i][j - 1], x);
    }
  }
  REP(i, n) REP(j, m) if (l[i][j] > h[i][j]) f = 0;
  REP(i, n) REP(j, n) {
    bool g = 0;
    REP(k, m) if (l[i][k] > h[j][k]) g = 1;
    e[i][j] = g;
  }

  REP(i, n) {
    VI visit(n);
    dfs(i, i, visit);
  }

  cout << (f ? "Yes" : "No") << endl;

  return 0;
}
Problem p01667 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
int ans = 1, m, n, used[101], memo[101][101];
P cmd[101][101];

int check(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  for (int i = 0; i < n; i++)
    if (cmd[a][i].l > cmd[b][i].r)
      return memo[a][b] = 0;
  return memo[a][b] = 1;
}

int solve() {
  memset(memo, -1, sizeof(memo));
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    int j = 0;
    while (j < m && (used[j] || check(i, j)))
      j++;
    if (j == m)
      used[i] = 1, cnt++, i = -1;
  }
  return cnt / m;
}

int main() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      cmd[i][j] = P(-INF, INF);

  cin >> m >> n;
  for (int i = 0, k; i < m; i++) {
    cin >> k;
    while (k--) {
      string cond;
      int s, t;
      cin >> s >> cond >> t;
      s--;
      if (cond == "<=")
        cmd[i][s].r = min(cmd[i][s].r, t);
      if (cond == ">=")
        cmd[i][s].l = max(cmd[i][s].l, t);
      if (cmd[i][s].l > cmd[i][s].r)
        ans = 0;
    }
  }

  cout << (ans * solve() ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
int ans = 1, m, n, used[101], memo[101][101];
P cmd[101][101];

int check(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  for (int i = 0; i < n; i++)
    if (cmd[a][i].l > cmd[b][i].r)
      return memo[a][b] = 0;
  return memo[a][b] = 1;
}

int solve() {
  memset(memo, -1, sizeof(memo));
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    int j = 0;
    while (!used[i] && j < m && (used[j] || check(i, j)))
      j++;
    if (j == m)
      used[i] = 1, cnt++, i = -1;
  }
  return cnt / m;
}

int main() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      cmd[i][j] = P(-INF, INF);

  cin >> m >> n;
  for (int i = 0, k; i < m; i++) {
    cin >> k;
    while (k--) {
      string cond;
      int s, t;
      cin >> s >> cond >> t;
      s--;
      if (cond == "<=")
        cmd[i][s].r = min(cmd[i][s].r, t);
      if (cond == ">=")
        cmd[i][s].l = max(cmd[i][s].l, t);
      if (cmd[i][s].l > cmd[i][s].r)
        ans = 0;
    }
  }

  cout << (ans * solve() ? "Yes" : "No") << endl;
  return 0;
}
Problem p01670 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

typedef bitset<300000> BS;

BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];

int rec(const BS &bs, int nth, int r) {
  if (r < 0)
    return r;
  if (bs == absbs)
    return r;
  while ((is[idx[nth]] & ~bs).none()) {
    nth++;
  }
  BS n = is[idx[nth]] & ~bs;
  if (n.count() == 1) {
    for (int i = 0;; i++) {
      if (n[i]) {
        return rec(bs | is[a[i] ^ b[i] ^ idx[nth]], nth + 1, r - 1);
      }
    }
  } else {
    int rv = rec(bs | is[idx[nth]], nth + 1, r - 1);
    BS o = bs;
    int c = 0;
    for (int i = 0; n.any(); i++) {
      if (n[i]) {
        o |= bs | is[a[i] ^ b[i] ^ idx[nth]];
        c++;
        n[i] = false;
      }
    }
    return max(rv, rec(o, nth + 1, r - c));
  }
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int pop[32345] = {};
  for (int i = 0; i < M; i++) {
    absbs[i] = true;
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    pop[a[i]]++;
    pop[b[i]]++;
    is[a[i]][i] = true;
    is[b[i]][i] = true;
  }
  idx.resize(N);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
  int r = rec(BS(), 0, K);
  if (r < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << K - r << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

typedef bitset<300000 / 10> BS;

BS absbs;
BS is[3000];
vector<int> idx;
int a[32345], b[32345];

int rec(const BS &bs, int nth, int r) {
  if (r < 0)
    return r;
  if (bs == absbs)
    return r;
  while ((is[idx[nth]] & ~bs).none()) {
    nth++;
  }
  BS n = is[idx[nth]] & ~bs;
  if (n.count() == 1) {
    for (int i = 0;; i++) {
      if (n[i]) {
        return rec(bs | is[a[i] ^ b[i] ^ idx[nth]], nth + 1, r - 1);
      }
    }
  } else {
    int rv = rec(bs | is[idx[nth]], nth + 1, r - 1);
    BS o = bs;
    int c = 0;
    for (int i = 0; n.any(); i++) {
      if (n[i]) {
        o |= bs | is[a[i] ^ b[i] ^ idx[nth]];
        c++;
        n[i] = false;
      }
    }
    return max(rv, rec(o, nth + 1, r - c));
  }
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  int pop[32345] = {};
  for (int i = 0; i < M; i++) {
    absbs[i] = true;
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    pop[a[i]]++;
    pop[b[i]]++;
    is[a[i]][i] = true;
    is[b[i]][i] = true;
  }
  idx.resize(N);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [pop](int a, int b) { return pop[a] > pop[b]; });
  int r = rec(BS(), 0, K);
  if (r < 0) {
    cout << "Impossible" << endl;
  } else {
    cout << K - r << endl;
  }
}
Problem p01676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct edge {
  int to, from;
  edge(int to, int from) : to(to), from(from) {}
  edge() {}
};

int N, M;
bool used[5000];
set<int> G[555];
vector<edge> E;

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    E.push_back(edge(t, s));
    G[s].insert(i);
    G[t].insert(i);
  }
  bool ed = true;
  int res = 0;
  while (ed) {
    int minid = 0;
    while (G[minid].empty())
      minid++;
    for (int i = 0; i < N; i++) {
      if (G[i].empty())
        continue;
      if (G[minid].size() > G[i].size())
        minid = i;
    }
    //   cout << G[minid].size() << endl;
    res += G[minid].size() - 1;
    for (auto it = G[minid].begin(); it != G[minid].end(); it++) {
      used[*it] = true;
      // cout << "new " << *it << endl;
      for (int i = 0; i < N; i++) {
        if (i == minid)
          continue;
        G[i].erase(*it);
      }
    }
    G[minid].clear();
    ed = false;
    for (int i = 0; i < M; i++)
      if (!used[i])
        ed = true;
  }
  cout << res << endl;
}

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

struct edge {
  int to, from;
  edge(int to, int from) : to(to), from(from) {}
  edge() {}
};

int N, M;
bool used[5000];
set<int> G[555];
vector<edge> E;

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    E.push_back(edge(t, s));
    G[s].insert(i);
    G[t].insert(i);
  }
  if (M == 0) {
    cout << 0 << endl;
    return 0;
  }
  bool ed = true;
  int res = 0;
  while (ed) {
    int minid = 0;
    while (G[minid].empty())
      minid++;
    for (int i = 0; i < N; i++) {
      if (G[i].empty())
        continue;
      if (G[minid].size() > G[i].size())
        minid = i;
    }
    //   cout << G[minid].size() << endl;
    res += G[minid].size() - 1;
    for (auto it = G[minid].begin(); it != G[minid].end(); it++) {
      used[*it] = true;
      // cout << "new " << *it << endl;
      for (int i = 0; i < N; i++) {
        if (i == minid)
          continue;
        G[i].erase(*it);
      }
    }
    G[minid].clear();
    ed = false;
    for (int i = 0; i < M; i++)
      if (!used[i])
        ed = true;
  }
  cout << res << endl;
}
Problem p01680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 1e9 + 7;

struct UnionFindTree {
  vector<int> v;
  vector<int> rank;
  int numgroup;

  UnionFindTree(int n) : v(n, -1), rank(n, 0), numgroup(n) {}
  int Find(int x) {
    if (v[x] < 0)
      return x;
    return v[x] = Find(v[x]);
  }
  void Union(int a, int b) {
    a = Find(a);
    b = Find(b);
    if (a == b)
      return;
    if (rank[a] < rank[b])
      swap(a, b);
    if (rank[a] == rank[b])
      rank[a]++;
    v[b] = a;
    numgroup--;
  }
};

lli modpow(lli n, lli p, lli mod) {
  lli res = 1;
  for (int i = 63; i >= 0; i--) {
    res = res * res % mod;
    if ((p & 1LL << i) != 0)
      res = res * n % mod;
  }
  return res;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    UnionFindTree uft(n);
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      uft.Union(a, b);
    }
    if (m == 0) {
      cout << modpow(2, n, mod) << endl;
    } else {
      cout << (modpow(2, uft.numgroup, mod) + 1) % mod << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;
const lli mod = 1e9 + 7;

struct UnionFindTree {
  vector<int> v;
  vector<int> rank;
  int numgroup;

  UnionFindTree(int n) : v(n, -1), rank(n, 0), numgroup(n) {}
  int Find(int x) {
    if (v[x] < 0)
      return x;
    return v[x] = Find(v[x]);
  }
  void Union(int a, int b) {
    a = Find(a);
    b = Find(b);
    if (a == b)
      return;
    if (rank[a] < rank[b])
      swap(a, b);
    if (rank[a] == rank[b])
      rank[a]++;
    v[b] = a;
    numgroup--;
  }
};

lli modpow(lli n, lli p, lli mod) {
  lli res = 1;
  for (int i = 63; i >= 0; i--) {
    res = res * res % mod;
    if ((p & 1LL << i) != 0)
      res = res * n % mod;
  }
  return res;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    UnionFindTree uft(n);
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      uft.Union(a, b);
    }
    if (m == 0) {
      cout << modpow(2, n, mod) << endl;
    } else {
      cout << (modpow(2, uft.numgroup, mod) + 1) % mod << endl;
    }
  }
  return 0;
}
Problem p01682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).*begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int INF = 150000000;
const long long M = 1000000007;

typedef string::const_iterator State;
int number(int &);
int term(int &);
int expression(int &);

string s;

// ??°??????????????????????????????????????°????????????
int number(int &i) {
  int ret = 0;

  while (isdigit(s[i])) {
    ret *= 10;
    ret += s[i] - '0';
    i++;
  }

  return ret;
}

// ?????????????????????????????????????????????????????????????????????
int term(int &i) {
  if (s[i] == 'S') {
    i += 2;
    long long ret = expression(i);
    i++;
    return ret * ret % M;
  } else {
    return number(i);
  }
  return INF;
}

// ?????????????????????????????????????????????????????????????????????
int expression(int &i) {
  int ret = term(i);

  while (i < s.size() - 2) {
    if (s[i] == '>' && s[i + 1] == '>' && s[i + 2] != '>') {
      i += 2;
      ret = ret >> min(term(i), 31);
    } else {
      return ret;
    }
  }

  return ret;
}

inline void deleteSpace(string &a) {
  size_t pos;
  while ((pos = a.find_first_of(" ")) != string::npos) {
    a.erase(pos, 1);
  }
}

int main() {
  std::ifstream ifs("/Users/noy/Downloads/2570_in1.txt.html");
  while (getline(cin, s), s != "#") {
    deleteSpace(s);
    int i = 0;
    cout << expression(i) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).*begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int INF = 150000000;
const long long M = 1000000007;

typedef string::const_iterator State;
int number(int &);
int term(int &);
int expression(int &);

string s;

// ??°??????????????????????????????????????°????????????
int number(int &i) {
  int ret = 0;

  while (isdigit(s[i])) {
    ret *= 10;
    ret += s[i] - '0';
    i++;
  }

  return ret;
}

// ?????????????????????????????????????????????????????????????????????
int term(int &i) {
  if (s[i] == 'S') {
    i += 2;
    long long ret = expression(i);
    i++;
    return ret * ret % M;
  } else {
    return number(i);
  }
  return INF;
}

// ?????????????????????????????????????????????????????????????????????
int expression(int &i) {
  int ret = term(i);

  while (i < s.size() - 2) {
    if (s[i] == '>' && s[i + 1] == '>' && s[i + 2] != '>') {
      i += 2;
      ret = ret >> min(term(i), 31);
    } else {
      return ret;
    }
  }

  return ret;
}

inline void deleteSpace(string &a) {
  size_t pos;
  while ((pos = a.find_first_of(" ")) != string::npos) {
    a.erase(pos, 1);
  }
}

int main() {
  std::ifstream ifs("/Users/noy/Downloads/2570_in1.txt.html");
  while (getline(cin, s), s != "#") {
    // deleteSpace(s);
    string tmp;
    for (auto it : s) {
      if (it != ' ')
        tmp += it;
    }
    s = tmp;
    int i = 0;
    cout << expression(i) << endl;
  }
}
Problem p01682 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int MOD = 1000000007LL;
string s;
int expr(int &p);
int term(int &p);
void sp(int &p);
int number(int &p);
int expr(int &p) {
  int res = term(p);
  sp(p);
  int q = p;
  while (q + 1 < (int)s.size() && s[q] == '>' && s[q + 1] == '>') {
    q += 2;
    sp(q);
    if (q >= (int)s.size() || !isdigit(s[q]))
      break;
    p += 2;
    sp(p);
    int y = term(p);
    if (y > 31)
      res = 0;
    else
      res >>= y;
    sp(p);
    q = p;
  }
  return res;
}
int term(int &p) {
  int res;
  if (s[p] == 'S') {
    p++;
    sp(p);
    assert(s[p] == '<');
    p++;
    sp(p);
    int x = expr(p);
    sp(p);
    assert(s[p] == '>');
    p++;
    res = (x * x) % MOD;
  } else {
    res = number(p);
  }
  return res;
}
void sp(int &p) {
  while (p < (int)s.size() && s[p] == ' ')
    p++;
}
int number(int &p) {
  int res = 0;
  while (p < (int)s.size() && isdigit(s[p]))
    res = (res * 10 + s[p++] - '0') % MOD;
  return res;
}
signed main() {
  while (getline(cin, s), s != "#") {
    int p = 0;
    cout << expr(p) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int MOD = 1000000007LL;
string s;
int expr(int &p);
int term(int &p);
void sp(int &p);
int number(int &p);
int expr(int &p) {
  int res = term(p);
  sp(p);
  int q = p;
  while (q + 1 < (int)s.size() && s[q] == '>' && s[q + 1] == '>') {
    q += 2;
    sp(q);
    if (q >= (int)s.size() || s[q] == '>')
      break;
    p += 2;
    sp(p);
    int y = term(p);
    if (y > 31)
      res = 0;
    else
      res >>= y;
    sp(p);
    q = p;
  }
  return res;
}
int term(int &p) {
  int res;
  if (s[p] == 'S') {
    p++;
    sp(p);
    assert(s[p] == '<');
    p++;
    sp(p);
    int x = expr(p);
    sp(p);
    assert(s[p] == '>');
    p++;
    res = (x * x) % MOD;
  } else {
    res = number(p);
  }
  return res;
}
void sp(int &p) {
  while (p < (int)s.size() && s[p] == ' ')
    p++;
}
int number(int &p) {
  int res = 0;
  while (p < (int)s.size() && isdigit(s[p]))
    res = (res * 10 + s[p++] - '0') % MOD;
  return res;
}
signed main() {
  while (getline(cin, s), s != "#") {
    int p = 0;
    cout << expr(p) << endl;
  }
  return 0;
}
Problem p01687 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

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

string s;
string t = "AIZUNYAN", u = "AIDUNYAN";
vi w(26, 0);

bool calc(int n) {
  vi v(26, 0);
  for (int i = 0; i < 8 && i < s.size(); i++)
    v[s[i + n] - 'A']++;
  for (int i = 0; i < 26; i++) {
    if (v[i] != w[i])
      return false;
    ;
  }
  for (int i = 0; i < 8; i++) {
    s[i + n] = t[i];
  }
  return true;
}

int main() {
  for (int i = 0; i < u.size(); i++)
    w[u[i] - 'A']++;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    bool f = calc(i);
    if (f)
      i += 7;
  }
  cout << s << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

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

string s;
string t = "AIZUNYAN", u = "AIDUNYAN";
vi w(26, 0);

bool calc(int n) {
  vi v(26, 0);
  for (int i = 0; i < 8 && i + n < s.size(); i++)
    v[s[i + n] - 'A']++;
  for (int i = 0; i < 26; i++) {
    if (v[i] != w[i])
      return false;
    ;
  }
  for (int i = 0; i < 8; i++) {
    s[i + n] = t[i];
  }
  return true;
}

int main() {
  for (int i = 0; i < u.size(); i++)
    w[u[i] - 'A']++;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    bool f = calc(i);
    if (f)
      i += 7;
  }
  cout << s << endl;
}
Problem p01694 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int n;

void solve() {
  vector<string> f(n);
  for (int i = 0; i < n; i++)
    cin >> f[i];

  int l = 0, r = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (f[i] == "lu" and f[i + 1] == "ru")
      ans++;
    else if (f[i] == "ru" and f[i + 1] == "lu")
      ans++;
    else if (f[i] == "ld" and f[i + 1] == "rd")
      ans++;
    else if (f[i] == "rd" and f[i + 1] == "ld")
      ans++;
  }
  cout << ans << endl;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int n;

void solve() {
  vector<string> f(n);
  for (int i = 0; i < n; i++)
    cin >> f[i];

  int l = 0, r = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 >= n)
      break;
    if (f[i] == "lu" and f[i + 1] == "ru")
      ans++;
    else if (f[i] == "ru" and f[i + 1] == "lu")
      ans++;
    else if (f[i] == "ld" and f[i + 1] == "rd")
      ans++;
    else if (f[i] == "rd" and f[i + 1] == "ld")
      ans++;
  }
  cout << ans << endl;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }
}
Problem p01695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    for (auto &e : s) {
      cin >> e;
    }
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; s[i][j] == '.'; j++) {
        if (j + 1 < s[i].size() && s[i][j + 1] != '.') {
          s[i][j] = '+';
        } else if (i + 1 < n && s[i + 1][j] == '+' || s[i + 1][j] == '|') {
          s[i][j] = '|';
        } else {
          s[i][j] = ' ';
        }
      }
    }
    for (auto e : s) {
      cout << e << endl;
    }
  }
}


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

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    for (auto &e : s) {
      cin >> e;
    }
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; s[i][j] == '.'; j++) {
        if (j + 1 < s[i].size() && s[i][j + 1] != '.') {
          s[i][j] = '+';
        } else if (i + 1 < n && j < s[i + 1].size() &&
                   (s[i + 1][j] == '+' || s[i + 1][j] == '|')) {
          s[i][j] = '|';
        } else {
          s[i][j] = ' ';
        }
      }
    }
    for (auto e : s) {
      cout << e << endl;
    }
  }
}
Problem p01695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define all(c) c.begin(), c.end()
#define sz(c) ((int)c.size())
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

signed main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> s(n);
    rep(i, 0, n) { cin >> s[i]; }
    rep(i, 0, n) {
      if (s[i][0] != '.')
        continue;
      rep(j, 0, sz(s[i]) - 1) {
        if (s[i][j + 1] != '.') {
          int r = i - 1;
          while (r >= 0 && s[r][j] == '.') {
            s[r][j] = '|';
            r--;
          }
          s[i][j] = '+';
          break;
        }
      }
    }
    rep(i, 0, n) {
      rep(j, 0, sz(s)) {
        if (s[i][j] == '.')
          s[i][j] = ' ';
      }
      cout << s[i] << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define all(c) c.begin(), c.end()
#define sz(c) ((int)c.size())
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

signed main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> s(n);
    rep(i, 0, n) { cin >> s[i]; }
    rep(i, 0, n) {
      if (s[i][0] != '.')
        continue;
      rep(j, 0, sz(s[i]) - 1) {
        if (s[i][j + 1] != '.') {
          int r = i - 1;
          while (r >= 0 && s[r][j] == '.') {
            s[r][j] = '|';
            r--;
          }
          s[i][j] = '+';
          break;
        }
      }
    }
    rep(i, 0, n) {
      rep(j, 0, sz(s[i])) {
        if (s[i][j] == '.')
          s[i][j] = ' ';
      }
      cout << s[i] << endl;
    }
  }
}
Problem p01695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define SZ(a) ((int)(a.size()))
int n;

string in[100];

void solve() {
  rep(i, n) cin >> in[i];
  rep(i, n) {
    int idx = -1;
    rep(j, SZ(in[i])) {
      if (in[i][j] != '.') {
        if (j) {
          in[i][j - 1] = '+';
          idx = j - 1;
        }
        break;
      }
    }
    if (idx >= 0)
      for (int k = i - 1; k; --k) {
        if (isalpha(in[k][idx]) || in[k][idx] == '+')
          break;
        in[k][idx] = '|';
      }
  }
  rep(i, n) for (auto &c : in[i]) if (c == '.') c = ' ';
  rep(i, n) cout << in[i] << endl;
}

int main(int argc, char *argv[]) {

  while (cin >> n && n)
    solve();
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define SZ(a) ((int)(a.size()))
int n;

string in[10000];

void solve() {
  rep(i, n) cin >> in[i];
  rep(i, n) {
    int idx = -1;
    rep(j, SZ(in[i])) {
      if (in[i][j] != '.') {
        if (j) {
          in[i][j - 1] = '+';
          idx = j - 1;
        }
        break;
      }
    }
    if (idx >= 0)
      for (int k = i - 1; k; --k) {
        if (isalpha(in[k][idx]) || in[k][idx] == '+')
          break;
        in[k][idx] = '|';
      }
  }
  rep(i, n) for (auto &c : in[i]) if (c == '.') c = ' ';
  rep(i, n) cout << in[i] << endl;
}

int main(int argc, char *argv[]) {

  while (cin >> n && n)
    solve();
  return 0;
}
Problem p01703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

double ans[100010];

void MAIN(int n) {
  std::vector<double> prob;
  for (int i = 0; i < n; ++i) {
    double p;
    scanf("%lf", &p);
    if (p < 0.995) {
      prob.push_back(p);
    } else if (prob.empty() || prob.back() < 0.995) {
      prob.push_back(p);
    } else {
      prob[prob.size() - 1] += 1;
    }
    ans[i] = 1e100;
  }
  n = prob.size();
  ans[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    double now = 0;
    int upper = std::min(n, i + 1000);
    for (int j = i + 1; j <= upper; ++j) {
      if (prob[j - 1] >= 0.995) {
        now += prob[j - 1];
      } else {
        now = (now + 1) / prob[j - 1];
      }
      ans[i] = std::min(ans[i], now + ans[j] + 1);
    }
  }
  printf("%.11f\n", ans[0] - 1);
}

int main() {
  int n;
  while (scanf("%d", &n) == 1 && n > 0) {
    MAIN(n);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

double ans[100010];

void MAIN(int n) {
  std::vector<double> prob;
  for (int i = 0; i < n; ++i) {
    double p;
    scanf("%lf", &p);
    if (p < 0.995) {
      prob.push_back(p);
    } else if (prob.empty() || prob.back() < 0.995) {
      prob.push_back(p);
    } else {
      prob[prob.size() - 1] += 1;
    }
    ans[i] = 1e100;
  }
  n = prob.size();
  ans[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    double now = 0;
    int upper = std::min(n, i + 700);
    for (int j = i + 1; j <= upper; ++j) {
      if (prob[j - 1] >= 0.995) {
        now += prob[j - 1];
      } else {
        now = (now + 1) / prob[j - 1];
      }
      ans[i] = std::min(ans[i], now + ans[j] + 1);
    }
  }
  printf("%.11f\n", ans[0] - 1);
}

int main() {
  int n;
  while (scanf("%d", &n) == 1 && n > 0) {
    MAIN(n);
  }
  return 0;
}
Problem p01704 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    double pw;
    cin >> pw;
    vector<double> vw(n);
    vector<double> pf(n);
    vector<double> vf(n);
    vector<double> th(n);
    for (int i = 0; i < n; i++) {
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
    }

    double ans;
    double l = 0.0, r = 1000.0;

    while (r - l >= 0.000000000000001) {

      double p1 = (r + 2 * l) / 3;
      double p2 = (2 * r + l) / 3;

      // cout << l << " " << r << " " << p1 << " " << p2 << endl;

      double cost1 = (double)(p1 * pw);
      double cost2 = (double)(p2 * pw);

      for (int i = 0; i < n; i++) {
        double F1 = (double)((th[i] - p1 * vw[i]) / vf[i]);
        double F2 = (double)((th[i] - p2 * vw[i]) / vf[i]);
        F1 = max(F1, 0.0);
        F2 = max(F2, 0.0);

        cost1 += (double)(F1 * pf[i]);
        cost2 += (double)(F2 * pf[i]);
      }

      if (cost1 < cost2)
        r = p2;
      else
        l = p1;

      ans = cost1;
    }

    printf("%.4f\n", ans);
  }

  return 0;
}


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

int main() {

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    double pw;
    cin >> pw;
    vector<double> vw(n);
    vector<double> pf(n);
    vector<double> vf(n);
    vector<double> th(n);
    for (int i = 0; i < n; i++) {
      cin >> vw[i] >> pf[i] >> vf[i] >> th[i];
    }

    double ans;
    double l = 0.0, r = 1000.0;

    while (r - l >= 0.00000000001) {

      double p1 = (r + 2 * l) / 3;
      double p2 = (2 * r + l) / 3;

      // cout << l << " " << r << " " << p1 << " " << p2 << endl;

      double cost1 = (double)(p1 * pw);
      double cost2 = (double)(p2 * pw);

      for (int i = 0; i < n; i++) {
        double F1 = (double)((th[i] - p1 * vw[i]) / vf[i]);
        double F2 = (double)((th[i] - p2 * vw[i]) / vf[i]);
        F1 = max(F1, 0.0);
        F2 = max(F2, 0.0);

        cost1 += (double)(F1 * pf[i]);
        cost2 += (double)(F2 * pf[i]);
      }

      if (cost1 < cost2)
        r = p2;
      else
        l = p1;

      ans = cost1;
    }

    printf("%.4f\n", ans);
  }

  return 0;
}