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

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

using namespace std;

int main() {
  int N, M, L, R, C;
  string S;
  char Q[4];

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

  cin >> S;

  L = 0;
  R = 0;
  C = 0;

  vector<vector<int>> F(N + 1, vector<int>());

  for (int i = 0; i < M; i++) {
    scanf("%s", Q);

    if (Q[0] == 'L' && Q[1] == '+') {
      L++;
    }
    if (Q[0] == 'L' && Q[1] == '-') {
      L--;
    }
    if (Q[0] == 'R' && Q[1] == '+') {
      R++;
    }
    if (Q[0] == 'R' && Q[1] == '-') {
      R--;
    }

    F[R - L + 1].push_back(L);

    C = max(C, R - L + 1);
  }

  vector<vector<int>> D(N + 1, vector<int>());

  for (int i = 1; i <= N; i++) {
    sort(F[i].begin(), F[i].end());

    D[i].push_back(F[i][0]);

    for (int j = 1; j < F[i].size(); j++) {
      if (F[i][j - 1] != F[i][j]) {
        D[i].push_back(F[i][j]);
      }
    }
  }

  int Answer = 0;

  for (int i = 1; i <= C; i++) {
    set<string> s;

    for (int j = 0; j < D[i].size(); j++) {
      s.insert(S.substr(D[i][j], i));
    }

    Answer += s.size();
  }

  cout << Answer << endl;

  return 0;
}

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

using namespace std;

int main() {
  int N, M, L, R, C;
  string S;
  char Q[4];

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

  cin >> S;

  L = 0;
  R = 0;
  C = 0;

  vector<vector<int>> F(N + 1, vector<int>());

  for (int i = 0; i < M; i++) {
    scanf("%s", Q);

    if (Q[0] == 'L' && Q[1] == '+') {
      L++;
    }
    if (Q[0] == 'L' && Q[1] == '-') {
      L--;
    }
    if (Q[0] == 'R' && Q[1] == '+') {
      R++;
    }
    if (Q[0] == 'R' && Q[1] == '-') {
      R--;
    }

    F[R - L + 1].push_back(L);

    C = max(C, R - L + 1);
  }

  vector<vector<int>> D(N + 1, vector<int>());

  for (int i = 1; i <= N; i++) {
    sort(F[i].begin(), F[i].end());

    if (F[i].size()) {
      D[i].push_back(F[i][0]);
    }

    for (int j = 1; j < F[i].size(); j++) {
      if (F[i][j - 1] != F[i][j]) {
        D[i].push_back(F[i][j]);
      }
    }
  }

  int Answer = 0;

  for (int i = 1; i <= C; i++) {
    set<string> s;

    for (int j = 0; j < D[i].size(); j++) {
      s.insert(S.substr(D[i][j], i));
    }

    Answer += s.size();
  }

  cout << Answer << endl;

  return 0;
}
Problem p01558 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 100000007;

int main() {

  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  ull hash[100100];
  ull po[100100];
  hash[0] = 0;
  po[0] = 1;
  int l = 0, r = 1;
  for (int i = 1; i <= n; i++) {
    po[i] = po[i - 1] * B;
    hash[i] = hash[i - 1] * B + (ull)(s[i - 1] - 'a' + 1);
  }

  set<ull> S;

  while (m--) {
    string in;
    cin >> in;

    if (in == "L++")
      l++;
    if (in == "R++")
      r++;
    if (in == "L--")
      l--;
    if (in == "R--")
      r--;

    ull H = hash[r] - hash[l] * po[r - l];
    S.insert(H);
  }

  cout << S.size() << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 100000007;

int main() {

  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  ull hash[300010];
  ull po[300010];
  hash[0] = 0;
  po[0] = 1;
  int l = 0, r = 1;
  for (int i = 1; i <= n; i++) {
    po[i] = po[i - 1] * B;
    hash[i] = hash[i - 1] * B + (ull)(s[i - 1] - 'a' + 1);
  }

  set<ull> S;

  while (m--) {
    string in;
    cin >> in;

    if (in == "L++")
      l++;
    if (in == "R++")
      r++;
    if (in == "L--")
      l--;
    if (in == "R--")
      r--;

    ull H = hash[r] - hash[l] * po[r - l];
    S.insert(H);
  }

  cout << S.size() << endl;

  return 0;
}
Problem p01558 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define BASE 1000000007ULL
#define MAXN 100100
using namespace std;

typedef unsigned long long ull;

// Rolling Hash
set<ull> hash;
ull acc_h[MAXN]; // hash value for interval (-1,i)
ull power[MAXN]; // M power of i

void init_roll(string s, int n) {
  acc_h[0] = 0;
  power[0] = 1;
  for (int i = 1; i <= n; i++) {
    power[i] = power[i - 1] * BASE;
    acc_h[i] = acc_h[i - 1] * BASE + s[i - 1];
  }
}

ull rolling_hash(int l, int r) { return acc_h[r] - acc_h[l] * power[r - l]; }

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

  string s;
  cin >> s;

  init_roll(s, n);

  set<ull> res;
  int l = 0, r = 0;
  while (m--) {
    string q;
    cin >> q;
    if (q == "L++")
      l++;
    if (q == "L--")
      l--;
    if (q == "R++")
      r++;
    if (q == "R--")
      r--;

    res.insert(rolling_hash(l, r + 1));
  }
  cout << res.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define BASE 1000000007ULL
#define MAXN 300100
using namespace std;

typedef unsigned long long ull;

// Rolling Hash
set<ull> hash;
ull acc_h[MAXN]; // hash value for interval (-1,i)
ull power[MAXN]; // M power of i

void init_roll(string s, int n) {
  acc_h[0] = 0;
  power[0] = 1;
  for (int i = 1; i <= n; i++) {
    power[i] = power[i - 1] * BASE;
    acc_h[i] = acc_h[i - 1] * BASE + s[i - 1];
  }
}

ull rolling_hash(int l, int r) { return acc_h[r] - acc_h[l] * power[r - l]; }

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

  string s;
  cin >> s;

  init_roll(s, n);

  set<ull> res;
  int l = 0, r = 0;
  while (m--) {
    string q;
    cin >> q;
    if (q == "L++")
      l++;
    if (q == "L--")
      l--;
    if (q == "R++")
      r++;
    if (q == "R--")
      r--;

    res.insert(rolling_hash(l, r + 1));
  }
  cout << res.size() << endl;
}
Problem p01570 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>

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

using namespace std;

typedef long long ll;

int main() {
  int n;
  scanf("%d", &n);
  static vector<int> G[160000], G_rev[160000];
  static int cost[160000];
  rep(u, n) {
    int v;
    scanf("%d%d", cost + u, &v);
    if (v != -1) {
      G[v].push_back(u);
      G_rev[u].push_back(v);
    }
  }

  vector<int> topo; // topological order
  queue<int> Q;
  rep(u, n) if (G_rev[u].empty()) {
    Q.push(u);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      topo.push_back(v);
      rep(i, G[v].size()) Q.push(G[v][i]);
    }
  }

  ll ans = 0;
  for (int i = (int)topo.size() - 1; i >= 0; i--) {
    int u = topo[i];

    vector<int> C(G[u].size()); // 頂点 u の子たちの cost
    rep(j, G[u].size()) C[j] = cost[G[u][j]];
    sort(C.begin(), C.end());

    ll sum = 0;
    rep(j, C.size()) {
      ans += sum;
      sum += C[j];
    }
    cost[u] += sum;
  }

  printf("%lld\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>

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

using namespace std;

typedef long long ll;

int main() {
  int n;
  scanf("%d", &n);
  static vector<int> G[1000000], G_rev[1000000];
  static int cost[1000000];
  rep(u, n) {
    int v;
    scanf("%d%d", cost + u, &v);
    if (v != -1) {
      G[v].push_back(u);
      G_rev[u].push_back(v);
    }
  }

  vector<int> topo; // topological order
  queue<int> Q;
  rep(u, n) if (G_rev[u].empty()) {
    Q.push(u);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      topo.push_back(v);
      rep(i, G[v].size()) Q.push(G[v][i]);
    }
  }

  ll ans = 0;
  for (int i = (int)topo.size() - 1; i >= 0; i--) {
    int u = topo[i];

    vector<int> C(G[u].size()); // 頂点 u の子たちの cost
    rep(j, G[u].size()) C[j] = cost[G[u][j]];
    sort(C.begin(), C.end());

    ll sum = 0;
    rep(j, C.size()) {
      ans += sum;
      sum += C[j];
    }
    cost[u] += sum;
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p01570 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:204800000,204800000");
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, sum, l[1000000], vis[1000000], S[4000000], stage[4000000], top;
long long ans;
vector<int> g[1000000];
int DFS(int v) {
  for (int i = 0; i < g[v].size(); i++)
    l[v] += DFS(g[v][i]);
  return l[v];
}
bool cmp(int a, int b) { return l[a] < l[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d%d", &l[i], &b);
    if (b != -1) {
      g[b].push_back(i);
      vis[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      S[0] = i;
      stage[0] = i;
      top = 0;
      while (top != -1) {
        int cur = S[top];
        if (stage[top] == g[cur].size()) {
          top--;
          if (top != -1) {
            l[S[top]] += l[S[top + 1]];
            // printf("%d-%d\n",S[top],S[top+1]);
          }
        } else {
          S[top + 1] = g[cur][stage[top]];
          stage[top]++;
          stage[++top] = 0;
        }
      }
    }
  for (int i = 0; i < n; i++)
    sort(g[i].begin(), g[i].end(), cmp);
  for (int i = 0; i < n; i++) {
    sum = 0;
    for (int j = 0; j < g[i].size(); j++) {
      ans += sum;
      sum += l[g[i][j]];
    }
  }
  // for (int i=0;i<n;i++) printf("%d ",l[i]);
  // printf("\n");
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#pragma comment(linker, "/STACK:204800000,204800000");
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, sum, l[1000000], vis[1000000], S[4000000], stage[4000000], top;
long long ans;
vector<int> g[1000000];
int DFS(int v) {
  for (int i = 0; i < g[v].size(); i++)
    l[v] += DFS(g[v][i]);
  return l[v];
}
bool cmp(int a, int b) { return l[a] < l[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d%d", &l[i], &b);
    if (b != -1) {
      g[b].push_back(i);
      vis[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      S[0] = i;
      stage[0] = 0;
      top = 0;
      while (top != -1) {
        int cur = S[top];
        if (stage[top] == g[cur].size()) {
          top--;
          if (top != -1) {
            l[S[top]] += l[S[top + 1]];
            // printf("%d-%d\n",S[top],S[top+1]);
          }
        } else {
          S[top + 1] = g[cur][stage[top]];
          stage[top]++;
          stage[++top] = 0;
        }
      }
    }
  for (int i = 0; i < n; i++)
    sort(g[i].begin(), g[i].end(), cmp);
  for (int i = 0; i < n; i++) {
    sum = 0;
    for (int j = 0; j < g[i].size(); j++) {
      ans += sum;
      sum += l[g[i][j]];
    }
  }
  // for (int i=0;i<n;i++) printf("%d ",l[i]);
  // printf("\n");
  printf("%lld\n", ans);
  return 0;
}
Problem p01600 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define x first
#define y second

using namespace std;

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

ll dp[1001][1001], K[1001][1001];

int main(void) {

  int n;
  cin >> n;

  vector<pii> v(n);
  rep(i, n) cin >> v[i].x >> v[i].y;

  fill(dp[0], dp[1001], INT_MAX);
  fill(K[0], K[1001], -1);
  rep(i, 1001) dp[i][i] = 0, K[i][i] = i;

  for (int w = 1; w < n; w++) {
    for (int i = 0; i + w <= n; i++) {
      for (int s = K[i][i + w - 1]; s <= K[i + 1][i + w]; s++) {
        int cost = abs(v[i].x - v[s + 1].x) + abs(v[s].y - v[i + w].y);
        if (dp[i][i + w] > dp[i][s] + dp[s + 1][i + w] + cost) {
          dp[i][i + w] = dp[i][s] + dp[s + 1][i + w] + cost;
          K[i][i + w] = s;
        }
      }
    }
  }

  cout << dp[0][n - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define x first
#define y second

using namespace std;

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

ll dp[1002][1002], K[1002][1002];

int main(void) {

  int n;
  cin >> n;

  vector<pii> v(n);
  rep(i, n) cin >> v[i].x >> v[i].y;

  fill(dp[0], dp[1001], INT_MAX);
  fill(K[0], K[1001], -1);
  rep(i, 1001) dp[i][i] = 0, K[i][i] = i;

  for (int w = 1; w < n; w++) {
    for (int i = 0; i + w <= n; i++) {
      for (int s = K[i][i + w - 1]; s <= K[i + 1][i + w]; s++) {
        int cost = abs(v[i].x - v[s + 1].x) + abs(v[s].y - v[i + w].y);
        if (dp[i][i + w] > dp[i][s] + dp[s + 1][i + w] + cost) {
          dp[i][i + w] = dp[i][s] + dp[s + 1][i + w] + cost;
          K[i][i + w] = s;
        }
      }
    }
  }

  cout << dp[0][n - 1] << endl;

  return 0;
}
Problem p01601 (C++)

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

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

  vector<bool> kai(1001, false);
  for (int i = 1; i < 10; i++) {
    kai[i] = true;
  }
  for (int i = 1; i < 10; i++) {
    kai[10 * i + i] = true;
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      kai[101 * i + 10 * j] = true;
      kai[1001 * i + 110 * j] = true;
    }
  }
  int ans = n;
  int ans2 = n;
  while (1) {
    if (kai[ans]) {
      cout << ans << endl;
      break;
    }
    if (kai[ans2]) {
      cout << ans2 << endl;
      break;
    }
    if (ans2 >= 10001)
      ans2 = n;
    ans--;
    ans2++;
  }

  return 0;
}


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

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

  vector<bool> kai(10001, false);
  for (int i = 1; i < 10; i++) {
    kai[i] = true;
  }
  for (int i = 1; i < 10; i++) {
    kai[10 * i + i] = true;
  }
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      kai[101 * i + 10 * j] = true;
      kai[1001 * i + 110 * j] = true;
    }
  }
  int ans = n;
  int ans2 = n;
  while (1) {
    if (kai[ans]) {
      cout << ans << endl;
      break;
    }
    if (kai[ans2]) {
      cout << ans2 << endl;
      break;
    }
    if (ans2 >= 10001)
      ans2 = n;
    ans--;
    ans2++;
  }

  return 0;
}
Problem p01602 (C++)

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

int main() {
  int n;
  cin >> n;
  char c;
  int x;
  cin >> c >> x;
  bool flag = true;
  if (c == ')' || n == 1) {
    cout << "NO" << endl;
    return 0;
  }

  stack<int> st;
  st.push(x);
  for (int i = 1; i < n; i++) {
    cin >> c >> x;
    int y = st.top();
    st.pop();
    if (c == '(') {
      st.push(x + y);
    } else {
      int z = y - x;
      if (z < 0) {
        flag = false;
        break;
      } else {
        st.push(z);
      }
    }
  }
  if (st.top() >= 1) {
    flag = false;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  char c;
  int x;
  cin >> c >> x;
  bool flag = true;
  if (c == ')' || n == 1) {
    cout << "NO" << endl;
    return 0;
  }

  stack<int> st;
  st.push(x);
  for (int i = 1; i < n; i++) {
    cin >> c >> x;
    int y = st.top();
    st.pop();
    if (c == '(') {
      st.push(x + y);
    } else {
      int z = y - x;
      if (z < 0) {
        flag = false;
        break;
      } else {
        st.push(z);
      }
    }
  }
  if (flag && st.top() >= 1) {
    flag = false;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p01606 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> ans(N + 1, 0);
  ans[0] = W;
  for (int i = 1; i <= W / 2; ++i) {
    ++ans[i + 1];
  }
  for (int i = W / 2 + 1; i < W; ++i) {
    for (int j = 3; j * i <= N; ++j) {
      ++ans[j * i - W + 1];
      --ans[j * i - W + 1 + W - i];
    }
  }
  for (int i = 1; i <= N - W + 2; ++i) {
    ans[i] += ans[i - 1];
  }
  cout << ans[1];
  for (int i = 2; i <= N - W + 1; ++i)
    cout << " " << ans[i];
  cout << endl;
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> ans(N + 2, 0);
  ans[0] = W;
  for (int i = 1; i <= W / 2; ++i) {
    ++ans[i + 1];
  }
  for (int i = W / 2 + 1; i < W; ++i) {
    for (int j = 3; j * i <= N; ++j) {
      ++ans[j * i - W + 1];
      --ans[j * i - W + 1 + W - i];
    }
  }
  for (int i = 1; i <= N - W + 2; ++i) {
    ans[i] += ans[i - 1];
  }
  cout << ans[1];
  for (int i = 2; i <= N - W + 1; ++i)
    cout << " " << ans[i];
  cout << endl;
  return 0;
}
Problem p01606 (C++)

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

using namespace std;

inline vector<int> factorize(int x) {
  vector<int> ret;

  for (int i = 1; i * i <= x; i++) {
    ret.push_back(i);
    if (i * i != x)
      ret.push_back(x / i);
  }

  return (ret);
}

int main() {
  int N, W;
  int cnt = 0;
  bool manage[100001] = {0};

  scanf("%d %d", &N, &W);

  for (int i = 1; i <= W - 1; i++) {
    manage[i] = true;
    cnt++;
  }

  for (int i = W; i <= N; i++) {
    vector<int> v;

    if (i - W) {
      v = factorize(i - W);

      for (int j = 0; j < v.size(); j++) {
        int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
        if (mini == 1 || maxi - mini >= 1)
          continue;
        else {
          cnt -= manage[v[j]];
          manage[v[j]] = false;
        }
      }
    }

    v = factorize(i);

    for (int j = 0; j < v.size(); j++) {
      int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
      if (mini == 1 || maxi - mini >= 1) {
        cnt += !manage[v[j]];
        manage[v[j]] = true;
      }
    }

    printf("%d%c", cnt, " \n"[i == N]);
  }

  return (0);
}

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

using namespace std;

inline vector<int> factorize(int x) {
  vector<int> ret;

  for (int i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      ret.push_back(i);
      if (i * i != x)
        ret.push_back(x / i);
    }
  }

  return (ret);
}

int main() {
  int N, W;
  int cnt = 0;
  bool manage[100001] = {0};

  scanf("%d %d", &N, &W);

  for (int i = 1; i <= W - 1; i++) {
    manage[i] = true;
    cnt++;
  }

  for (int i = W; i <= N; i++) {
    vector<int> v;

    if (i - W) {
      v = factorize(i - W);

      for (int j = 0; j < v.size(); j++) {
        int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
        if (mini == 1 || maxi - mini >= 1)
          continue;
        else {
          cnt -= manage[v[j]];
          manage[v[j]] = false;
        }
      }
    }

    v = factorize(i);

    for (int j = 0; j < v.size(); j++) {
      int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
      if (mini == 1 || maxi - mini >= 1) {
        cnt += !manage[v[j]];
        manage[v[j]] = true;
      }
    }

    printf("%d%c", cnt, " \n"[i == N]);
  }

  return (0);
}
Problem p01606 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  vector<int> a(n + 2, 0);
  for (int i = 1; i <= w / 2; ++i)
    ++a[i + 1];
  for (int i = w / 2 + 1; i < w; ++i) {
    for (int j = 3 * i; j <= n; j += i) {
      ++a[j - w + 1];
      --a[j - i + 1];
    }
  }

  int ret = w;
  for (int i = 1; i <= n - w + 1; ++i) {
    ret += a[i];
    cout << ret;
    if (i < n - w + 1)
      cout << ' ';
    else
      cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  vector<int> a(2 * n, 0);
  for (int i = 1; i <= w / 2; ++i)
    ++a[i + 1];
  for (int i = w / 2 + 1; i < w; ++i) {
    for (int j = 3 * i; j <= n; j += i) {
      ++a[j - w + 1];
      --a[j - i + 1];
    }
  }

  int ret = w;
  for (int i = 1; i <= n - w + 1; ++i) {
    ret += a[i];
    cout << ret;
    if (i < n - w + 1)
      cout << ' ';
    else
      cout << endl;
  }

  return 0;
}
Problem p01606 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> ans(N + W, 0);
  ans[0] = W;
  for (int i = 1; i <= W / 2; ++i) {
    ++ans[i + 1];
  }
  for (int i = W / 2 + 1; i < W; ++i) {
    for (int j = 3; j * i <= N; ++j) {
      ++ans[j * i - W + 1];
      --ans[j * i - W + 1 + W - i];
    }
  }
  for (int i = 1; i <= N - W + 2; ++i) {
    ans[i] += ans[i - 1];
  }
  cout << ans[1];
  for (int i = 2; i <= N - W + 1; ++i)
    cout << " " << ans[i];
  cout << endl;
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> ans(2 * (N + W), 0);
  ans[0] = W;
  for (int i = 1; i <= W / 2; ++i) {
    ++ans[i + 1];
  }
  for (int i = W / 2 + 1; i < W; ++i) {
    for (int j = 3; j * i <= N; ++j) {
      ++ans[j * i - W + 1];
      --ans[j * i - W + 1 + W - i];
    }
  }
  for (int i = 1; i <= N - W + 2; ++i) {
    ans[i] += ans[i - 1];
  }
  cout << ans[1];
  for (int i = 2; i <= N - W + 1; ++i)
    cout << " " << ans[i];
  cout << endl;
  return 0;
}
Problem p01620 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<char> cmap;
char getrev(char c, int kagi) {
  int rst;
  if (c >= 'a')
    rst = c - 'a';
  else
    rst = c - 'A' + ('Z' - 'A' + 1);
  if (rst - kagi < 0)
    return cmap[rst - kagi + cmap.size()];
  else
    return cmap[rst - kagi];
}
int main() {
  for (int i = 'a'; i < 'z' + 1; i++)
    cmap.pb(i);
  for (int i = 'A'; i < 'Z' + 1; i++)
    cmap.pb(i);

  while (1) {
    int n;
    cin >> n;
    int klist[100];
    REP(i, n) { cin >> klist[i]; }
    string ango;
    cin >> ango;
    int nowkagi = 0;
    REP(i, ango.size()) {
      cout << getrev(ango[i], klist[nowkagi]);
      if (nowkagi == n - 1)
        nowkagi = 0;
      else
        nowkagi++;
    }
    cout << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<char> cmap;
char getrev(char c, int kagi) {
  int rst;
  if (c >= 'a')
    rst = c - 'a';
  else
    rst = c - 'A' + ('Z' - 'A' + 1);
  if (rst - kagi < 0)
    return cmap[rst - kagi + cmap.size()];
  else
    return cmap[rst - kagi];
}
int main() {
  for (int i = 'a'; i < 'z' + 1; i++)
    cmap.pb(i);
  for (int i = 'A'; i < 'Z' + 1; i++)
    cmap.pb(i);

  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int klist[100];
    REP(i, n) { cin >> klist[i]; }
    string ango;
    cin >> ango;
    int nowkagi = 0;
    REP(i, ango.size()) {
      cout << getrev(ango[i], klist[nowkagi]);
      if (nowkagi == n - 1)
        nowkagi = 0;
      else
        nowkagi++;
    }
    cout << endl;
  }
}
Problem p01622 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

int N;
struct Book {
  int page;
  int write;
};
Book book[1000];
int dp[1000 * 1000 * 30];
int sum = 0;
int deal = 0;
int solve() {
  memset(dp, 0, sizeof(dp));
  int s = book[N - 1].page * 2 - sum;
  // cout << endl<<s << endl<<endl;
  dp[0] = -1;
  rep(i, 0, N - 1) {
    rep(j, 1, s + 1) {
      if (j - book[i].write >= 0 && dp[j - book[i].write] != 0 &&
          dp[j - book[i].write] <= i && dp[j] == 0)
        dp[j] = i + 1;
      // cout << dp[j] << "/";
    }
    //	cout << endl << endl;
  }
  for (int i = s; i >= 0; i--)
    if (dp[i] != 0) {
      return (s - i);
    }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      return (0);
    int max_n = 0, max_i = 0;
    sum = 0;
    deal = 0;
    rep(i, 0, N) {
      cin >> book[i].page >> book[i].write;
      sum += book[i].page;
      deal += book[i].write;
      if (book[i].page > max_n)
        max_n = book[i].page, max_i = i;
    }
    if (sum >= max_n * 2)
      cout << sum + deal << endl;
    else {
      Book tmp = book[max_i];
      book[max_i] = book[N - 1];
      book[N - 1] = tmp;
      cout << sum + deal + solve() << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

int N;
struct Book {
  int page;
  int write;
};
Book book[1000];
int dp[1000 * 1000 * 3];
int sum = 0;
int deal = 0;
int solve() {
  memset(dp, 0, sizeof(dp));
  int s = book[N - 1].page * 2 - sum;
  // cout << endl<<s << endl<<endl;
  dp[0] = -1;
  rep(i, 0, N - 1) {
    rep(j, 1, s + 1) {
      if (j - book[i].write >= 0 && dp[j - book[i].write] != 0 &&
          dp[j - book[i].write] <= i && dp[j] == 0)
        dp[j] = i + 1;
      // cout << dp[j] << "/";
    }
    //	cout << endl << endl;
  }
  for (int i = s; i >= 0; i--)
    if (dp[i] != 0) {
      return (s - i);
    }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      return (0);
    int max_n = 0, max_i = 0;
    sum = 0;
    deal = 0;
    rep(i, 0, N) {
      cin >> book[i].page >> book[i].write;
      sum += book[i].page;
      deal += book[i].write;
      if (book[i].page > max_n)
        max_n = book[i].page, max_i = i;
    }
    if (sum >= max_n * 2)
      cout << sum + deal << endl;
    else {
      Book tmp = book[max_i];
      book[max_i] = book[N - 1];
      book[N - 1] = tmp;
      cout << sum + deal + solve() << endl;
    }
  }
}
Problem p01622 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
pair<int, int> x[1000];
int n, a, b, s1, s2, dp[1100000];
int main() {
  while (true) {
    cin >> n;
    s1 = 0;
    s2 = 0;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      x[i] = make_pair(a, b);
      s1 += a;
      s2 += b;
    }
    sort(x, x + n);
    if (x[n - 1].first * 2 <= s1) {
      cout << s1 + s2 << endl;
    } else {
      for (int i = 0; i <= s2; i++)
        dp[i] = 0;
      dp[0] = 1;
      int nokori = x[n - 1].first - (s1 - x[n - 1].first);
      vector<int> f;
      f.push_back(0);
      for (int i = 0; i < n - 1; i++) {
        vector<int> g;
        for (int a1 : f) {
          if (dp[a1 + x[i].second] == 0) {
            g.push_back(a1 + x[i].second);
            dp[a1 + x[i].second] = 1;
          }
        }
        for (int i = 0; i < g.size(); i++)
          f.push_back(g[i]);
      }
      int g = 0;
      for (int i = nokori; i >= 0; i--) {
        if (dp[i] == 1) {
          g = i;
          break;
        }
      }
      cout << (nokori - g) + s1 + s2 << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
pair<int, int> x[1000];
int n, a, b, s1, s2, dp[1100000];
int main() {
  while (true) {
    cin >> n;
    s1 = 0;
    s2 = 0;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      x[i] = make_pair(a, b);
      s1 += a;
      s2 += b;
    }
    sort(x, x + n);
    if (x[n - 1].first * 2 <= s1) {
      cout << s1 + s2 << endl;
    } else {
      for (int i = 0; i < 1100000; i++)
        dp[i] = 0;
      dp[0] = 1;
      int nokori = x[n - 1].first - (s1 - x[n - 1].first);
      vector<int> f;
      f.push_back(0);
      for (int i = 0; i < n - 1; i++) {
        vector<int> g;
        for (int a1 : f) {
          if (dp[a1 + x[i].second] == 0) {
            g.push_back(a1 + x[i].second);
            dp[a1 + x[i].second] = 1;
          }
        }
        for (int i = 0; i < g.size(); i++)
          f.push_back(g[i]);
      }
      int g = 0;
      for (int i = nokori; i >= 0; i--) {
        if (dp[i] == 1) {
          g = i;
          break;
        }
      }
      cout << (nokori - g) + s1 + s2 << endl;
    }
  }
  return 0;
}
Problem p01637 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll pay[2][1000];
ll lost[2][1000];
int main() {
  ll m, r[2], c[2];
  cin >> m >> r[0] >> r[1] >> c[0] >> c[1];

  rep(j, 2) rep(i, 1000) pay[j][i] = lost[j][i] = 1ll << 60;

  rep(i, 2) {
    ll Min = 100 * c[i] / r[i];
    for (int x = Min; x < Min + 1000; x++) {
      ll D = x * r[i] / 100;
      D -= c[i];
      if (D < 0)
        continue;
      ll rest = D * 100 / r[i];
      pay[i][x - Min] = x;
      lost[i][x - Min] = x - rest;
    }
  }

  ll ans = 1ll << 60;
  rep(i, 1000) rep(j, 1000) {
    if (pay[0][i] + pay[1][j] > m)
      continue;
    ans = min(ans, lost[0][i] + lost[1][j]);
  }
  if (m < ans)
    cout << -1 << endl;
  else
    cout << m - ans << endl;

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll pay[2][1000];
ll lost[2][1000];
int main() {
  ll m, r[2], c[2];
  cin >> m >> r[0] >> r[1] >> c[0] >> c[1];

  rep(j, 2) rep(i, 1000) pay[j][i] = lost[j][i] = 1ll << 60;

  rep(i, 2) {
    ll Min = 100 * c[i] / r[i];
    for (ll x = Min; x < Min + 1000; x++) {
      ll D = x * r[i] / 100;
      D -= c[i];
      if (D < 0)
        continue;
      ll rest = D * 100 / r[i];
      pay[i][x - Min] = x;
      lost[i][x - Min] = x - rest;
    }
  }

  ll ans = 1ll << 60;
  rep(i, 1000) rep(j, 1000) {
    if (pay[0][i] + pay[1][j] > m)
      continue;
    ans = min(ans, lost[0][i] + lost[1][j]);
  }
  if (m < ans)
    cout << -1 << endl;
  else
    cout << m - ans << endl;

  return 0;
}
Problem p01639 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int cnt[200000];
vector<long long> v;
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  b--;
  unsigned long long x = c;
  for (int i = 0; i < a; i++) {
    cnt[((long long)x) / (1LL << 47) + 100000]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  int at = 0;
  for (int i = 0; i < 200000; i++) {
    if (b < cnt[i]) {
      at = i;
      break;
    } else
      b -= cnt[i];
  }
  // printf("%d\n",at);
  x = c;
  for (int i = 0; i < a; i++) {
    if (((long long)x) / (1LL << 47) + 100000 == at) {
      v.push_back((long long)x);
    }
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  std::sort(v.begin(), v.end());
  printf("%lld\n", v[b]);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int cnt[200000];
vector<long long> v;
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  if (c == 0) {
    printf("0\n");
    return 0;
  }
  b--;
  unsigned long long x = c;
  for (int i = 0; i < a; i++) {
    cnt[((long long)x) / (1LL << 47) + 100000]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  int at = 0;
  for (int i = 0; i < 200000; i++) {
    if (b < cnt[i]) {
      at = i;
      break;
    } else
      b -= cnt[i];
  }
  // printf("%d\n",at);
  x = c;
  for (int i = 0; i < a; i++) {
    if (((long long)x) / (1LL << 47) + 100000 == at) {
      v.push_back((long long)x);
    }
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  std::sort(v.begin(), v.end());
  printf("%lld\n", v[b]);
}
Problem p01639 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 9223372036854775807
#define DIVISOR 10000000000000000

int main() {

  ull count[2000];

  for (int i = 0; i < 2000; i++)
    count[i] = 0;

  ull N, K;
  ull x_0;
  scanf("%lld %lld %lld", &N, &K, &x_0);

  ull x = x_0;
  for (int i = 0; i < N; i++) {
    count[(ull)(NUM + (ll)x) / DIVISOR]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }

  int sum = 0;
  ull loc;

  for (ull i = 0; i < 2000; i++) {
    if (sum + count[i] >= K) {
      loc = i;
      break;
    }
    sum += count[i];
  }

  vector<ll> V;

  x = x_0;
  for (int i = 0; i < N; i++) {

    if (loc == (ull)(NUM + (ll)x) / DIVISOR) {
      V.push_back(x);
    }
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }

  sort(V.begin(), V.end());

  printf("%lld\n", V[(K - sum) - 1]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 9223372036854775807
#define DIVISOR 10000000000000000

int main() {

  ull count[2000];

  for (int i = 0; i < 2000; i++)
    count[i] = 0;

  ull N, K;
  ull x_0;
  scanf("%lld %lld %lld", &N, &K, &x_0);
  if (x_0 == 0) {
    printf("0\n");
    return 0;
  }

  ull x = x_0;
  for (int i = 0; i < N; i++) {
    count[(ull)(NUM + (ll)x) / DIVISOR]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }

  int sum = 0;
  ull loc;

  for (ull i = 0; i < 2000; i++) {
    if (sum + count[i] >= K) {
      loc = i;
      break;
    }
    sum += count[i];
  }

  vector<ll> V;

  x = x_0;
  for (int i = 0; i < N; i++) {

    if (loc == (ull)(NUM + (ll)x) / DIVISOR) {
      V.push_back(x);
    }
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }

  sort(V.begin(), V.end());

  printf("%lld\n", V[(K - sum) - 1]);

  return 0;
}
Problem p01639 (C++)

Buggy submission (status: Memory Limit Exceeded):
// #define NDEBUG

#include <algorithm>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
ull mask = 1LL << 63;
int bm[1 << 16];
int main() {
  ll x0, n, k;
  cin >> n >> k >> x0;
  k--;
  ull x = x0;
  for (int i = 0; i < n; i++) {
    bm[(x + mask) >> 48]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  ll sm = 0;
  ll u;
  for (u = 0; u < 1 << 16; u++) {
    if (k < sm + bm[u])
      break;
    sm += bm[u];
  }
  k -= sm;
  vector<ll> v;
  x = x0;
  for (int i = 0; i < n; i++) {
    if ((x + mask) >> 48 == u)
      v.push_back((ll)x);
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  sort(v.begin(), v.end());
  cout << v[k] << endl;
  return 0;
}

Fixed submission:
// #define NDEBUG

#include <algorithm>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
ull mask = 1LL << 63;
int bm[1 << 16];
int main() {
  ll x0, n, k;
  cin >> n >> k >> x0;
  k--;
  if (!x0) {
    cout << "0" << endl;
    return 0;
  }
  ull x = x0;
  for (int i = 0; i < n; i++) {
    bm[(x + mask) >> 48]++;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  ll sm = 0;
  ll u;
  for (u = 0; u < 1 << 16; u++) {
    if (k < sm + bm[u])
      break;
    sm += bm[u];
  }
  k -= sm;
  vector<ll> v;
  x = x0;
  for (int i = 0; i < n; i++) {
    if ((x + mask) >> 48 == u)
      v.push_back((ll)x);
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
  }
  sort(v.begin(), v.end());
  cout << v[k] << endl;
  return 0;
}
Problem p01639 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ull;
int x, n, k, cnt[1050000];
int main() {
  cin >> n >> k >> x;
  k--;
  ull z1 = x;
  for (int i = 0; i < n; i++) {
    long long w = z1;
    cnt[(w >> 44) + 524288]++;
    z1 ^= (z1 << 13);
    z1 ^= (z1 >> 7);
    z1 ^= (z1 << 17);
  }
  int sum = 0, r = 0;
  for (int i = 0; i < 1048576; i++) {
    sum += cnt[i];
    if (sum > k) {
      r = i;
      break;
    }
  }
  int c = k - (sum - cnt[r]);
  ull z2 = x;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    long long w = z2;
    if ((w >> 44) + 524288 == r)
      v.push_back(z2);
    z2 ^= (z2 << 13);
    z2 ^= (z2 >> 7);
    z2 ^= (z2 << 17);
  }
  sort(v.begin(), v.end());
  cout << v[c] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ull;
int x, n, k, cnt[1050000];
int main() {
  cin >> n >> k >> x;
  k--;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  ull z1 = x;
  for (int i = 0; i < n; i++) {
    long long w = z1;
    cnt[(w >> 44) + 524288]++;
    z1 ^= (z1 << 13);
    z1 ^= (z1 >> 7);
    z1 ^= (z1 << 17);
  }
  int sum = 0, r = 0;
  for (int i = 0; i < 1048576; i++) {
    sum += cnt[i];
    if (sum > k) {
      r = i;
      break;
    }
  }
  int c = k - (sum - cnt[r]);
  ull z2 = x;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    long long w = z2;
    if ((w >> 44) + 524288 == r)
      v.push_back(z2);
    z2 ^= (z2 << 13);
    z2 ^= (z2 >> 7);
    z2 ^= (z2 << 17);
  }
  sort(v.begin(), v.end());
  cout << v[c] << endl;
  return 0;
}
Problem p01646 (C++)

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

using namespace std;

bool dfs(int pos, int start, const vector<vector<int>> &graph,
         vector<bool> &visited) {
  if (visited[pos])
    return false;

  visited[pos] = true;
  for (int v : graph[pos]) {
    if (v == start)
      return true;
    if (dfs(v, start, graph, visited))
      return true;
  }
  return false;
}

bool solve() {
  int N;
  if (!(cin >> N))
    return false;
  if (!N)
    return false;

  vector<string> v(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
  }
  vector<vector<int>> graph(26);
  for (int i = 0; i < N - 1; ++i) {
    const string &s1 = v[i];
    const string &s2 = v[i + 1];
    if (s1.size() > s2.size() && s1.substr(0, s2.size()) == s2) {
      cout << "no" << endl;
      return true;
    }
    for (int j = 0; j < s1.size() && j < s2.size(); ++j) {
      if (s1[j] != s2[j]) {
        graph[s1[j] - 'a'].push_back(s2[j] - 'a');
        break;
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    vector<bool> visited(26, false);
    if (dfs(i, i, graph, visited)) {
      cout << "no" << endl;
      return true;
    }
  }
  cout << "yes" << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  while (solve())
    ;
  return 0;
}

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

using namespace std;

bool dfs(int pos, int start, const vector<vector<int>> &graph,
         vector<bool> &visited) {
  if (visited[pos])
    return false;

  visited[pos] = true;
  for (int v : graph[pos]) {
    if (v == start)
      return true;
    if (dfs(v, start, graph, visited))
      return true;
  }
  return false;
}

bool solve() {
  int N;
  if (!(cin >> N))
    return false;
  if (!N)
    return false;

  vector<string> v(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
  }
  vector<vector<int>> graph(26);
  for (int i = 0; i < N - 1; ++i) {
    const string &s1 = v[i];
    const string &s2 = v[i + 1];
    if (s1.size() > s2.size() && s1.substr(0, s2.size()) == s2) {
      cout << "no" << endl;
      return true;
    }
    for (int j = 0; j < s1.size() && j < s2.size(); ++j) {
      if (s1[j] != s2[j]) {
        graph[s1[j] - 'a'].push_back(s2[j] - 'a');
        break;
      }
    }
  }
  for (int i = 0; i < 26; ++i) {
    vector<bool> visited(26, false);
    if (dfs(i, i, graph, visited)) {
      cout << "no" << endl;
      return true;
    }
  }
  cout << "yes" << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  while (solve())
    ;
  return 0;
}
Problem p01646 (C++)

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

using namespace std;

typedef pair<char, char> P;
typedef pair<int, int> Pii;

const int MAX = 30;
vector<Pii> E;

P comp(string a, string b) {
  for (int i = 0; i < (int)min(a.length(), b.length()); i++)
    if (a[i] != b[i])
      return P(a[i], b[i]);

  if (a.length() > b.length())
    return P(a[b.length()], '?');
  if (a.length() < b.length())
    return P('?', b[a.length()]);
  return P('?', '?');
}

bool add_edge(const vector<string> &v) {
  E.clear();

  for (int i = 0; i + 1 < (int)v.size(); i++) {
    P p = comp(v[i], v[i + 1]);
    if (p == P('?', '?'))
      continue;
    if (p.second == '?')
      return false;
    if (p.first != '?')
      E.push_back(P(p.first - 'a', p.second - 'a'));
  }
  return true;
}

bool hasCircle(int N) {

  vector<int> d(N, 0);
  for (int i = 0; i <= N; i++) {
    bool update = false;
    for (int j = 0; j < (int)E.size(); j++)
      if (d[E[j].second] < d[E[j].first] + 1) {
        d[E[j].second] = d[E[j].first] + 1;
        update = true;
      }
    if (i == N && update)
      return true;
    if (!update)
      break;
  }
  return false;
}

bool solve(const vector<string> &v) {
  if (!add_edge(v))
    return false;
  return !hasCircle(v.size());
}

int main() {

  int N;
  while (cin >> N && N) {
    vector<string> v(N);
    for (int i = 0; i < N; i++)
      cin >> v[i];
    cout << (solve(v) ? "yes" : "no") << endl;
  }
  return 0;
}

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

using namespace std;

typedef pair<char, char> P;
typedef pair<int, int> Pii;

const int MAX = 30;
vector<Pii> E;

P comp(string a, string b) {
  for (int i = 0; i < (int)min(a.length(), b.length()); i++)
    if (a[i] != b[i])
      return P(a[i], b[i]);

  if (a.length() > b.length())
    return P(a[b.length()], '?');
  if (a.length() < b.length())
    return P('?', b[a.length()]);
  return P('?', '?');
}

bool add_edge(const vector<string> &v) {
  E.clear();

  for (int i = 0; i + 1 < (int)v.size(); i++) {
    P p = comp(v[i], v[i + 1]);
    if (p == P('?', '?'))
      continue;
    if (p.second == '?')
      return false;
    if (p.first != '?')
      E.push_back(P(p.first - 'a', p.second - 'a'));
  }
  return true;
}

bool hasCircle(int N) {

  vector<int> d(MAX, 0);
  for (int i = 0; i <= N; i++) {
    bool update = false;
    for (int j = 0; j < (int)E.size(); j++)
      if (d[E[j].second] < d[E[j].first] + 1) {
        d[E[j].second] = d[E[j].first] + 1;
        update = true;
      }
    if (i == N && update)
      return true;
    if (!update)
      break;
  }
  return false;
}

bool solve(const vector<string> &v) {
  if (!add_edge(v))
    return false;
  return !hasCircle(v.size());
}

int main() {

  int N;
  while (cin >> N && N) {
    vector<string> v(N);
    for (int i = 0; i < N; i++)
      cin >> v[i];
    cout << (solve(v) ? "yes" : "no") << endl;
  }
  return 0;
}
Problem p01646 (C++)

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second

typedef long long ll;

int main() {
  int n;
  char s[256][256];
  while (~scanf("%d", &n) && n) {
    rep(i, n) scanf("%s", s[i]);
    set<char> st[256];
    bool ans = true;
    rep(i, n) {
      REP(j, i + 1, n) {
        bool f = true;
        int li = strlen(s[i]), lj = strlen(s[j]);
        int l = min(li, lj);
        rep(k, l) {
          if (s[i][k] != s[j][k]) {
            f = false;
            if (st[s[j][k]].count(s[i][k])) {
              ans = false;
              break;
            }
            st[s[i][k]].insert(s[j][k]);
            break;
          }
        }
        if (f && li > lj)
          ans = false;
      }
    }
    printf("%s\n", (ans ? "yes" : "no"));
  }
}

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second

typedef long long ll;

int main() {
  int n;
  char s[1024][256];
  while (~scanf("%d", &n) && n) {
    rep(i, n) scanf("%s", s[i]);
    set<char> st[256];
    bool ans = true;
    rep(i, n) {
      REP(j, i + 1, n) {
        bool f = true;
        int li = strlen(s[i]), lj = strlen(s[j]);
        int l = min(li, lj);
        rep(k, l) {
          if (s[i][k] != s[j][k]) {
            f = false;
            if (st[s[j][k]].count(s[i][k])) {
              ans = false;
              break;
            }
            st[s[i][k]].insert(s[j][k]);
            break;
          }
        }
        if (f && li > lj)
          ans = false;
      }
    }
    printf("%s\n", (ans ? "yes" : "no"));
  }
}