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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, l, q, x[110001], go[110001][19];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", x + i);
  scanf("%d%d", &l, &q);
  for (int i = 1; i <= n; i++)
    go[i][0] = upper_bound(x + 1, x + n + 1, x[i] + l) - x - 1;
  for (int i = 1; i < 19; i++)
    for (int j = 1; j <= n; j++)
      go[j][i] = go[go[j][i - 1]][i - 1];
  while (q--) {
    int a, b, k = 18, ans = 0;
    scanf("%d%d", &a, &b);
    if (a > b)
      swap(a, b);
    while (a != b) {
      while (k && go[a][k] >= b)
        k--;
      a = go[a][k];
      ans += 1 << k;
    }
    printf("%d\n", ans);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, l, q, x[110001], go[110001][19];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", x + i);
  scanf("%d%d", &l, &q);
  for (int i = 1; i <= n; i++)
    go[i][0] = upper_bound(x + 1, x + n + 1, x[i] + l) - x - 1;
  for (int i = 1; i < 19; i++)
    for (int j = 1; j <= n; j++)
      go[j][i] = go[go[j][i - 1]][i - 1];
  while (q--) {
    int a, b, k = 18, ans = 0;
    scanf("%d%d", &a, &b);
    if (a > b)
      swap(a, b);
    while (a < b) {
      while (k && go[a][k] >= b)
        k--;
      a = go[a][k];
      ans += 1 << k;
    }
    printf("%d\n", ans);
  }
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
int s[maxn], n, L, Q;
int cnt[maxn], c[36][maxn];

int main() {
  s[0] = 0;
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++)
      scanf("%d", &s[i]);
    scanf("%d", &L);
    for (int i = 1; i < n; i++) {
      int tmp = cnt[i - 1];
      while (s[tmp] - s[i] <= L && tmp <= n)
        tmp++;
      cnt[i] = tmp - 1;
      c[0][i] = tmp - 1;
    }
    c[0][n] = n;
    for (int i = 1; i < 25; i++)
      for (int j = 1; j < n; j++)
        c[i][j] = c[i - 1][c[i - 1][j]];
    scanf("%d", &Q);
    while (Q--) {
      int a, b, ans = 0;
      scanf("%d%d", &a, &b);
      if (a > b)
        swap(a, b);
      int tmp = a, sum = 0;

      while (a < b) {
        int ss = 0;
        for (ss = 0; ss < 25; ss++) {
          if (c[ss][a] >= b)
            break;
        }
        if (ss > 0)
          ss--;
        a = c[ss][a];
        ans += 1 << ss;
        //	cout<<a<<endl;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
/*
4
1 3 5 7
2
4
1 2
1 4
2 3
2 4
*/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
int s[maxn], n, L, Q;
int cnt[maxn], c[36][maxn];

int main() {
  s[0] = 0;
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++)
      scanf("%d", &s[i]);
    scanf("%d", &L);
    for (int i = 1; i < n; i++) {
      int tmp = cnt[i - 1];
      while (s[tmp] - s[i] <= L && tmp <= n)
        tmp++;
      cnt[i] = tmp - 1;
      c[0][i] = tmp - 1;
    }
    c[0][n] = n;
    for (int i = 1; i < 25; i++)
      for (int j = 1; j <= n; j++)
        c[i][j] = c[i - 1][c[i - 1][j]];
    scanf("%d", &Q);
    while (Q--) {
      int a, b, ans = 0;
      scanf("%d%d", &a, &b);
      if (a > b)
        swap(a, b);
      int tmp = a, sum = 0;

      while (a < b) {
        int ss = 0;
        for (ss = 0; ss < 25; ss++) {
          if (c[ss][a] >= b)
            break;
        }
        if (ss > 0)
          ss--;
        a = c[ss][a];
        ans += 1 << ss;
        //	cout<<a<<endl;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
/*
4
1 3 5 7
2
4
1 2
1 4
2 3
2 4
*/
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 20000
int ai[N];
int nx[20][N], q, s, n, L;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> ai[i];
  cin >> L;
  for (int i = 1, j = i; i <= n; ++i) {
    while (j < n && ai[j + 1] - ai[i] <= L)
      j++;
    nx[0][i] = j;
  }
  for (s = 1; (1 << s) <= n; ++s)
    for (int j = 1; j <= n; ++j)
      nx[s][j] = nx[s - 1][nx[s - 1][j]];
  s--;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int l, r, ans = 0;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    for (int i = s; ~i; --i)
      if (nx[i][l] < r)
        l = nx[i][l], ans += 1 << i;
    cout << ans + 1 << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 200000
int ai[N];
int nx[20][N], q, s, n, L;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> ai[i];
  cin >> L;
  for (int i = 1, j = i; i <= n; ++i) {
    while (j < n && ai[j + 1] - ai[i] <= L)
      j++;
    nx[0][i] = j;
  }
  for (s = 1; (1 << s) <= n; ++s)
    for (int j = 1; j <= n; ++j)
      nx[s][j] = nx[s - 1][nx[s - 1][j]];
  s--;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int l, r, ans = 0;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    for (int i = s; ~i; --i)
      if (nx[i][l] < r)
        l = nx[i][l], ans += 1 << i;
    cout << ans + 1 << "\n";
  }
}
Problem p04017 (C++)

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define RFOR(i, l, r) for (int i = (int)(r); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n;
  cin >> n;
  vi x(n);
  REP(i, n) {
    int a;
    cin >> x[i];
  }
  int l, q;
  cin >> l >> q;

  vvi dp(30, vi(100005, 0));
  REP(i, n) {
    auto it = upper_bound(all(x), x[i] + l);
    it--;
    dp[0][i] = it - x.begin();
    // cout << dp[0][i] << " ";
  }
  // int low = 1, high = 30;cout << endl;

  FOR(i, 1, 30) {
    // cout << " day " << i << " : ";
    REP(j, n) {
      dp[i][j] = dp[i - 1][dp[i - 1][j]];
      //    cout << dp[i][j] << " ";
    }
    // cout << endl;
  }

  REP(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    RREP(i, n) {
      if (dp[i][a] < b) {
        a = dp[i][a];
        ans += 1 << i;
      }
    }

    cout << ans + 1 << endl;
  }

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define RFOR(i, l, r) for (int i = (int)(r); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n;
  cin >> n;
  vi x(n);
  REP(i, n) {
    int a;
    cin >> x[i];
  }
  int l, q;
  cin >> l >> q;

  vvi dp(30, vi(100005, 0));
  REP(i, n) {
    auto it = upper_bound(all(x), x[i] + l);
    it--;
    dp[0][i] = it - x.begin();
    // cout << dp[0][i] << " ";
  }
  // int low = 1, high = 30;cout << endl;

  FOR(i, 1, 30) {
    // cout << " day " << i << " : ";
    REP(j, n) {
      dp[i][j] = dp[i - 1][dp[i - 1][j]];
      //    cout << dp[i][j] << " ";
    }
    // cout << endl;
  }

  REP(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b)
      swap(a, b);
    int ans = 0;
    RREP(i, 30) {
      if (dp[i][a] < b) {
        a = dp[i][a];
        ans += 1 << i;
      }
    }

    cout << ans + 1 << endl;
  }

  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

int N;
vector<ll> X;
ll L;
int Q;

int nxt[20][100010];
const ll INF = 1e15;

int main() {
  cin >> N;
  X.resize(N);
  rep(i, N) cin >> X[i];
  cin >> L;

  X.push_back(INF);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 20; j++)
      nxt[i][j] = -1;
  for (int i = 0; i < N; i++) {
    int z = upper_bound(X.begin(), X.end(), X[i] + L) - X.begin();
    z--;
    nxt[0][i] = z;
  }
  nxt[0][N - 1] = -1;
  for (int k = 0; k + 1 < 20; k++) {
    for (int i = 0; i < N; i++) {
      if (nxt[k][i] == -1)
        continue;
      nxt[k + 1][i] = nxt[k][nxt[k][i]];
    }
  }

  cin >> Q;
  vector<int> ans(Q, 0);
  for (int q = 0; q < Q; q++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    if (a > b)
      swap(a, b);

    int ok = N, ng = 0;
    while (ok - ng > 1) {
      int mid = (ok + ng) / 2;
      int now = a;
      for (int k = 0; k < 20; k++) {
        if (mid & (1 << k)) {
          now = nxt[k][now];
          if (now == -1)
            break;
        }
      }

      if (now >= b || now == -1)
        ok = mid;
      else
        ng = mid;
    }
    ans[q] = ok;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

int N;
vector<ll> X;
ll L;
int Q;

int nxt[20][100010];
const ll INF = 1e15;

int main() {
  cin >> N;
  X.resize(N);
  rep(i, N) cin >> X[i];
  cin >> L;

  X.push_back(INF);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 20; j++)
      nxt[j][i] = -1;
  for (int i = 0; i < N; i++) {
    int z = upper_bound(X.begin(), X.end(), X[i] + L) - X.begin();
    z--;
    nxt[0][i] = z;
  }
  nxt[0][N - 1] = -1;
  for (int k = 0; k + 1 < 20; k++) {
    for (int i = 0; i < N; i++) {
      if (nxt[k][i] == -1)
        continue;
      nxt[k + 1][i] = nxt[k][nxt[k][i]];
    }
  }

  cin >> Q;
  vector<int> ans(Q, 0);
  for (int q = 0; q < Q; q++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    if (a > b)
      swap(a, b);

    int ok = N, ng = 0;
    while (ok - ng > 1) {
      int mid = (ok + ng) / 2;
      int now = a;
      for (int k = 0; k < 20; k++) {
        if (mid & (1 << k)) {
          now = nxt[k][now];
          if (now == -1)
            break;
        }
      }

      if (now >= b || now == -1)
        ok = mid;
      else
        ng = mid;
    }
    ans[q] = ok;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;

  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, l, q, a[100005] = {}, h[18][100005] = {}, x, y;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cin >> l >> q;
  for (int i = 1; i <= n; i++)
    h[0][i] = (int)(upper_bound(a, a + n + 1, a[i] + l) - a) - 1;
  for (int i = 1; i <= 17; i++)
    for (int j = 1; j <= n; j++)
      h[i][j] = h[i - 1][h[i - 1][j]];
  while (q--) {
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    int z = 0;
    while (h[0][x] != y) {
      for (int i = 1; i <= 17; i++)
        if (h[i][x] >= y) {
          z += (1 << (i - 1));
          x = h[i - 1][x];
          break;
        }
    }
    cout << z + 1 << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  int n, l, q, a[100005] = {}, h[18][100005] = {}, x, y;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cin >> l >> q;
  for (int i = 1; i <= n; i++)
    h[0][i] = (int)(upper_bound(a, a + n + 1, a[i] + l) - a) - 1;
  for (int i = 1; i <= 17; i++)
    for (int j = 1; j <= n; j++)
      h[i][j] = h[i - 1][h[i - 1][j]];
  while (q--) {
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    int z = 0;
    while (h[0][x] < y) {
      for (int i = 1; i <= 17; i++)
        if (h[i][x] >= y) {
          z += (1 << (i - 1));
          x = h[i - 1][x];
          break;
        }
    }
    cout << z + 1 << '\n';
  }
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#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)
#define pi M_PI

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;

int bk[17][100001];

int main() {
  int n, l, q;
  cin >> n;
  VI x(n);
  REP(i, n) scanf("%d", &x[i]);
  cin >> l >> q;

  int r = 0;
  REP(i, n) {
    while (r < n - 1 && x[r + 1] - x[i] <= l)
      r++;
    bk[0][i] = r;
  }

  FOR(i, 1, 17) {
    REP(j, n) { bk[i][j] = bk[i - 1][bk[i - 1][j]]; }
  }

  while (q--) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    if (a > b)
      swap(a, b);

    int ans = 0;
    FORR(i, 16, 0) {
      if (bk[i][a] < b) {
        ans += 1 << i;
        a = bk[i][a];
      }
    }
    printf("%d\n", ans + 1);
  }

  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)
#define pi M_PI

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;

int bk[17][100001];

int main() {
  int n, l, q;
  cin >> n;
  VI x(n);
  REP(i, n) scanf("%d", &x[i]);
  cin >> l >> q;

  int r = 0;
  REP(i, n) {
    while (r < n - 1 && x[r + 1] - x[i] <= l)
      r++;
    bk[0][i] = r;
  }

  FOR(i, 1, 16) {
    REP(j, n) { bk[i][j] = bk[i - 1][bk[i - 1][j]]; }
  }

  while (q--) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    if (a > b)
      swap(a, b);

    int ans = 0;
    FORR(i, 16, 0) {
      if (bk[i][a] < b) {
        ans += 1 << i;
        a = bk[i][a];
      }
    }
    printf("%d\n", ans + 1);
  }

  return 0;
}
Problem p04017 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>

using namespace std;
int mod = 1000000007;

int N;
int main() {
  cin >> N;
  vector<int> x(N);
  int y[18][100005];
  rep(i, N) { cin >> x.at(i); }
  int L;
  cin >> L;

  int pos = 1;
  rep(i, N) {
    while (pos < N && x.at(pos) <= x.at(i) + L) {
      pos++;
    }
    y[0][i + 1] = pos;
  }

  for (int i = 0; i < 18; i++) {
    for (int j = 1; j <= N; j++) {
      y[i + 1][j] = y[i][y[i][j]];
    }
  }

  int Q;
  cin >> Q;

  while (Q > 0) {
    Q--;
    int ans = 0;
    int a, b;
    cin >> a >> b;
    if (a > b) {
      int tmp = b;
      b = a;
      a = tmp;
    }

    int now = a;
    while (now < b) {
      int base = 1;
      int i = 0;

      while (y[i][now] < b) {
        i++;
        base *= 2;
      }
      if (i >= 1) {
        now = y[i - 1][now];
        ans += base / 2;
      } else {
        now = b;
        ans += base;
      }
    }

    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>

using namespace std;
int mod = 1000000007;

int N;
int main() {
  cin >> N;
  vector<int> x(N);
  int y[20][100005];
  rep(i, N) { cin >> x.at(i); }
  int L;
  cin >> L;

  int pos = 1;
  rep(i, N) {
    while (pos < N && x.at(pos) <= x.at(i) + L) {
      pos++;
    }
    y[0][i + 1] = pos;
  }

  for (int i = 0; i < 18; i++) {
    for (int j = 1; j <= N; j++) {
      y[i + 1][j] = y[i][y[i][j]];
    }
  }

  int Q;
  cin >> Q;

  while (Q > 0) {
    Q--;
    int ans = 0;
    int a, b;
    cin >> a >> b;
    if (a > b) {
      int tmp = b;
      b = a;
      a = tmp;
    }

    int now = a;
    while (now < b) {
      int base = 1;
      int i = 0;

      while (y[i][now] < b) {
        i++;
        base *= 2;
      }
      if (i >= 1) {
        now = y[i - 1][now];
        ans += base / 2;
      } else {
        now = b;
        ans += base;
      }
    }

    cout << ans << endl;
  }
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
int a[MAXN], n, L, go[19][MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  scanf("%d", &L);
  memset(go, 0x3f, sizeof(go));
  for (int i = 1; i <= n; ++i) {
    int p = upper_bound(a + 1, a + n + 1, a[i] + L) - a;
    if (p != n + 1)
      go[0][i] = p - 1;
  }
  for (int i = 1; i <= 18; ++i)
    for (int j = 1; j <= n; ++j)
      if (go[i - 1][j] != 0x3f3f3f3f)
        go[i][j] = go[i - 1][go[i - 1][j]];
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y, ans = 0;
    scanf("%d%d", &x, &y);
    if (x > y)
      swap(x, y);
    for (int i = 18; i >= 0; --i)
      if (a[go[i][x]] <= a[y])
        x = go[i][x], ans += (1 << i);
    if (x == y)
      printf("%d\n", ans);
    else
      printf("%d\n", ans + 1);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100005;
int a[MAXN], n, L, go[19][MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  scanf("%d", &L);
  memset(go, 0x3f, sizeof(go));
  for (int i = 1; i <= n; ++i) {
    int p = upper_bound(a + 1, a + n + 1, a[i] + L) - a;
    if (p != n + 1)
      go[0][i] = p - 1;
  }
  for (int i = 1; i <= 18; ++i)
    for (int j = 1; j <= n; ++j)
      if (go[i - 1][j] != 0x3f3f3f3f)
        go[i][j] = go[i - 1][go[i - 1][j]];
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y, ans = 0;
    scanf("%d%d", &x, &y);
    if (x > y)
      swap(x, y);
    for (int i = 18; i >= 0; --i)
      if (go[i][x] != 0x3f3f3f3f && a[go[i][x]] <= a[y])
        x = go[i][x], ans += (1 << i);
    if (x == y)
      printf("%d\n", ans);
    else
      printf("%d\n", ans + 1);
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define eps 1e-6
#define mp make_pair
const long long INF = 1e18 + 2;
const int MX = 100004;
int N, M, k;
int X[MX];
int dp[MX][18];
int get(int u, int D) {
  if (D == 0)
    return u;
  for (int i = 17; i >= 0; i--) {
    if (u == -1)
      return u;
    if (D & (1 << i))
      u = dp[u][i];
  }
  return u;
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &X[i]);
  scanf("%d", &k);
  for (int i = 2; i <= N; i++) {
    dp[i][0] = lower_bound(X + 1, X + 1 + N, X[i] - k) - X;
    for (int j = 1; j <= 17; j++) {
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a > b)
      swap(a, b);
    int lo = 0, hi = b - a, R = 0, mid;
    while (lo <= hi) {
      mid = (lo + hi) >> 1;
      int res = get(b, mid);
      if (res <= a) {
        R = mid;
        hi = mid - 1;
      } else
        lo = mid + 1;
    }
    cout << R << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define eps 1e-6
#define mp make_pair
const long long INF = 1e18 + 2;
const int MX = 100004;
int N, M, k;
int X[MX];
int dp[MX][18];
int get(int u, int D) {
  if (D == 0)
    return u;
  for (int i = 17; i >= 0; i--) {
    if (u == -1)
      return u;
    if (D & (1 << i))
      u = dp[u][i];
  }
  return u;
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &X[i]);
  scanf("%d", &k);
  for (int i = 2; i <= N; i++) {
    dp[i][0] = lower_bound(X + 1, X + 1 + N, X[i] - k) - X;
    for (int j = 1; j <= 17; j++) {
      if (dp[i][j - 1] == -1)
        break;
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a > b)
      swap(a, b);
    int lo = 0, hi = b - a, R = 0, mid;
    while (lo <= hi) {
      mid = (lo + hi) >> 1;
      int res = get(b, mid);
      if (res <= a) {
        R = mid;
        hi = mid - 1;
      } else
        lo = mid + 1;
    }
    cout << R << "\n";
  }
  return 0;
}
Problem p04017 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;

int main() {
  int N, L, Q;
  cin >> N;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  x.push_back(2e9);
  cin >> L >> Q;
  vector<int> a(Q), b(Q);
  rep(i, Q) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    if (a[i] > b[i])
      swap(a[i], b[i]);
  }
  vector<vector<int>> next(N, vector<int>(17));
  rep(i, N) {
    next[i][0] = upper_bound(x.begin(), x.end(), x[i] + L) - x.begin() - 1;
    // cout << "next[" << i << "][0]=" << next[i][0] << "\n";
  }
  rep(j, 16) {
    rep(i, N) {
      next[i][j + 1] = next[next[i][j]][j];
      // cout << "next[" << i << "][" << j + 1 << "]=" << next[i][j + 1] <<
      // "\n";
    }
  }
  rep(i, Q) {
    int ans = 1;
    for (int j = 16; j >= 0; j--) {
      if (next[a[i]][j] >= b[i])
        continue;
      ans += 1 << j;
      a[i] = next[a[i]][i];
    }
    cout << ans << "\n";
  }
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;

int main() {
  int N, L, Q;
  cin >> N;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  x.push_back(2e9);
  cin >> L >> Q;
  vector<int> a(Q), b(Q);
  rep(i, Q) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    if (a[i] > b[i])
      swap(a[i], b[i]);
  }
  vector<vector<int>> next(N, vector<int>(17));
  rep(i, N) {
    next[i][0] = upper_bound(x.begin(), x.end(), x[i] + L) - x.begin() - 1;
    // cout << "next[" << i << "][0]=" << next[i][0] << "\n";
  }
  rep(j, 16) {
    rep(i, N) {
      next[i][j + 1] = next[next[i][j]][j];
      // cout << "next[" << i << "][" << j + 1 << "]=" << next[i][j + 1] <<
      // "\n";
    }
  }
  rep(i, Q) {
    int ans = 1;
    for (int j = 16; j >= 0; j--) {
      if (next[a[i]][j] >= b[i])
        continue;
      ans += 1 << j;
      a[i] = next[a[i]][j];
    }
    cout << ans << "\n";
  }
}
Problem p04017 (C++)

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

using namespace std;
int n, k, q;
int hotel[200005];
int jump[200005][20];

/* Phan chuan bi LCA */
void pre_process() {
  for (int i = 1; i <= n; i++) {
    int p = upper_bound(hotel, hotel + n + 2, hotel[i] + k) - hotel;
    jump[i][0] = p - 1;
  }

  for (int j = 1; j < 20; j++) {
    for (int i = 1; i <= n; i++) {
      if (jump[i][j - 1] != -1)
        jump[i][j] = jump[jump[i][j - 1]][j - 1];
    }
  }
}

/* Phan tinh LCA */
int solve(int u, int v) {
  if (u == v)
    return 0;
  int ans = 0;
  for (int i = 17; i >= 0; i--) {
    if (jump[u][i] < v) {
      u = jump[u][i];
      ans += (1 << i);
    }
  }

  return ans + 1;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;

  hotel[n + 1] = 2e9;
  memset(jump, -1, sizeof jump);

  for (int i = 1; i <= n; i++) {
    cin >> hotel[i];
  }
  cin >> k;
  cin >> q;

  while (q--) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);

    cout << solve(u, v) << '\n';
  }
}


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

using namespace std;
int n, k, q;
int hotel[200005];
int jump[200005][20];

/* Phan chuan bi LCA */
void pre_process() {
  for (int i = 1; i <= n; i++) {
    int p = upper_bound(hotel, hotel + n + 2, hotel[i] + k) - hotel;
    jump[i][0] = p - 1;
  }

  for (int j = 1; j < 20; j++) {
    for (int i = 1; i <= n; i++) {
      if (jump[i][j - 1] != -1)
        jump[i][j] = jump[jump[i][j - 1]][j - 1];
    }
  }
}

/* Phan tinh LCA */
int solve(int u, int v) {
  if (u == v)
    return 0;
  int ans = 0;
  for (int i = 17; i >= 0; i--) {
    if (jump[u][i] < v) {
      u = jump[u][i];
      ans += (1 << i);
    }
  }

  return ans + 1;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;

  hotel[n + 1] = 2e9;
  memset(jump, -1, sizeof jump);

  for (int i = 1; i <= n; i++) {
    cin >> hotel[i];
  }
  cin >> k;
  pre_process();
  cin >> q;

  while (q--) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);

    cout << solve(u, v) << '\n';
  }
}
Problem p04018 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[100005];
int n, nxt[100005];
bool f[3][100005];
void KMP(int x) {
  int j = 0;
  nxt[1] = 0;
  for (int i = 2; i <= n; i++) {
    while (j != 0 && s[i] != s[j + 1])
      j = nxt[j];
    if (s[i] == s[j + 1])
      j++;
    nxt[i] = j;
  }

  for (int i = 1; i <= n; i++)
    if (nxt[i] == 0 || i % (i - nxt[i]) != 0)
      f[x][i] = true;
}
bool check() {
  bool flag = false;
  for (int i = 2; i <= n; i++)
    if (s[i] != s[i - 1])
      flag = true;
  if (!flag)
    return true;
  else
    return false;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  KMP(1);
  if (f[1][n] == true) {
    printf("1\n1\n");
    return 0;
  }
  if (check()) {
    printf("%d\n1\n", n);
    return 0;
  }
  reverse(s + 1, s + 1 + n);
  KMP(2);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (f[1][i] && f[2][n - i])
      ans++;
  printf("2\n%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[1000005];
int n, nxt[1000005];
bool f[3][1000005];
void KMP(int x) {
  int j = 0;
  nxt[1] = 0;
  for (int i = 2; i <= n; i++) {
    while (j != 0 && s[i] != s[j + 1])
      j = nxt[j];
    if (s[i] == s[j + 1])
      j++;
    nxt[i] = j;
  }

  for (int i = 1; i <= n; i++)
    if (nxt[i] == 0 || i % (i - nxt[i]) != 0)
      f[x][i] = true;
}
bool check() {
  bool flag = false;
  for (int i = 2; i <= n; i++)
    if (s[i] != s[i - 1])
      flag = true;
  if (!flag)
    return true;
  else
    return false;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  KMP(1);
  if (f[1][n] == true) {
    printf("1\n1\n");
    return 0;
  }
  if (check()) {
    printf("%d\n1\n", n);
    return 0;
  }
  reverse(s + 1, s + 1 + n);
  KMP(2);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (f[1][i] && f[2][n - i])
      ans++;
  printf("2\n%d\n", ans);
  return 0;
}
Problem p04018 (C++)

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

const int MN = 500010;

void getPi(string &S, int *pi) {
  for (int i = 1, j = 0; i < S.size(); i++) {
    while (j && S[i] != S[j])
      j = pi[j - 1];
    if (S[i] == S[j]) {
      pi[i] = ++j;
    }
  }
}

int N;
string S;
int pi[MN], rpi[MN];

int main() {
  cin >> S;
  getPi(S, pi);
  reverse(S.begin(), S.end());
  getPi(S, rpi);
  N = S.size();

  if (pi[N - 1] == 0 || N % (N - pi[N - 1])) {
    printf("1\n1");
    return 0;
  }
  if (pi[N - 1] == N - 1) {
    printf("%d\n1", N);
    return 0;
  }
  printf("2\n");

  int ans = 0;
  for (int i = 0; i < N - 1; i++) {
    bool ok = true;
    if (pi[i] && (i + 1) % (i + 1 - pi[i]) == 0)
      ok = false;
    if (rpi[N - 2 - i] && (N - 1 - i) % (N - 1 - i - rpi[N - 2 - 2]))
      ok = false;
    if (ok)
      ans++;
  }
  printf("%d", ans);
}


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

const int MN = 500010;

void getPi(string &S, int *pi) {
  for (int i = 1, j = 0; i < S.size(); i++) {
    while (j && S[i] != S[j])
      j = pi[j - 1];
    if (S[i] == S[j]) {
      pi[i] = ++j;
    }
  }
}

int N;
string S;
int pi[MN], rpi[MN];

int main() {
  cin >> S;
  getPi(S, pi);
  reverse(S.begin(), S.end());
  getPi(S, rpi);
  N = S.size();

  if (pi[N - 1] == 0 || N % (N - pi[N - 1])) {
    printf("1\n1");
    return 0;
  }
  if (pi[N - 1] == N - 1) {
    printf("%d\n1", N);
    return 0;
  }
  printf("2\n");

  int ans = 0;
  for (int i = 0; i < N - 1; i++) {
    bool ok = true;
    if (pi[i] && (i + 1) % (i + 1 - pi[i]) == 0)
      ok = false;
    if (rpi[N - 2 - i] && (N - 1 - i) % (N - 1 - i - rpi[N - 2 - i]) == 0)
      ok = false;
    if (ok)
      ans++;
  }
  printf("%d", ans);
}
Problem p04018 (C++)

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

int n, f1[500005], f2[500005], Ans;
char s[500005];

void KMP(int f[]) {
  for (int i = 2, j = 0; i <= n; ++i) {
    while (j && s[i] != s[j + 1])
      j = f[j];
    if (s[i] == s[j + 1])
      f[i] = ++j;
  }
}

inline bool Pre(const int l) { return !f1[l] || l % (l - f1[l]); }
inline bool Suf(const int l) { return !f2[l] || l % (l - f2[l]); }

int main() {
  // freopen("in.txt","r",stdin);
  scanf("%s", s + 1), n = strlen(s + 1);
  if (n == 1)
    return puts("1\n1"), 0;
  for (int i = 2; i <= n; ++i) {
    if (s[i] != s[1])
      break;
    if (i == n)
      return printf("%d\n1\n", n);
  }
  KMP(f1), std::reverse(s + 1, s + n + 1), KMP(f2);
  if (Pre(n))
    return puts("1\n1"), 0;
  for (int i = 1; i < n; ++i)
    if (Pre(i) && Suf(n - i))
      ++Ans;
  printf("2\n%d\n", Ans);
  return 0;
}

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

int n, f1[500005], f2[500005], Ans;
char s[500005];

void KMP(int f[]) {
  for (int i = 2, j = 0; i <= n; ++i) {
    while (j && s[i] != s[j + 1])
      j = f[j];
    if (s[i] == s[j + 1])
      f[i] = ++j;
  }
}

inline bool Pre(const int l) { return !f1[l] || l % (l - f1[l]); }
inline bool Suf(const int l) { return !f2[l] || l % (l - f2[l]); }

int main() {
  // freopen("in.txt","r",stdin);
  scanf("%s", s + 1), n = strlen(s + 1);
  if (n == 1)
    return puts("1\n1"), 0;
  for (int i = 2; i <= n; ++i) {
    if (s[i] != s[1])
      break;
    if (i == n)
      return printf("%d\n1\n", n), 0;
  }
  KMP(f1), std::reverse(s + 1, s + n + 1), KMP(f2);
  if (Pre(n))
    return puts("1\n1"), 0;
  for (int i = 1; i < n; ++i)
    if (Pre(i) && Suf(n - i))
      ++Ans;
  printf("2\n%d\n", Ans);
  return 0;
}
Problem p04018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500005;
int n, fail[maxn];
char s[maxn];
bool vis[maxn];
inline void getfail() {
  for (int i = 2, j = 0; i <= n; i++) {
    while (j && s[j + 1] != s[i])
      j = fail[j];
    if (s[j + 1] == s[i])
      j++;
    fail[i] = j;
  }
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  getfail();
  if (n % (n - fail[n]) != 0 || !fail[n])
    return printf("1\n1\n"), 0;
  if (fail[n] == n - 1)
    return printf("%d\n1\n", n);
  for (int i = 1; i <= n; i++)
    vis[i] |= i % (i - fail[i]) == 0 && fail[i];
  reverse(s + 1, s + n + 1);
  getfail();
  for (int i = 1; i <= n; i++)
    vis[n - i] |= i % (i - fail[i]) == 0 && fail[i];
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += !vis[i];
  printf("2\n%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500005;
int n, fail[maxn];
char s[maxn];
bool vis[maxn];
inline void getfail() {
  for (int i = 2, j = 0; i <= n; i++) {
    while (j && s[j + 1] != s[i])
      j = fail[j];
    if (s[j + 1] == s[i])
      j++;
    fail[i] = j;
  }
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  getfail();
  if (n % (n - fail[n]) != 0 || !fail[n])
    return printf("1\n1\n"), 0;
  if (fail[n] == n - 1)
    return printf("%d\n1\n", n), 0;
  for (int i = 1; i <= n; i++)
    vis[i] |= i % (i - fail[i]) == 0 && fail[i];
  reverse(s + 1, s + n + 1);
  getfail();
  for (int i = 1; i <= n; i++)
    vis[n - i] |= i % (i - fail[i]) == 0 && fail[i];
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += !vis[i];
  printf("2\n%d\n", ans);
  return 0;
}
Problem p04018 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define ls (t << 1)
#define rs ((t << 1) + 1)
#define mid ((l + r) >> 1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 500005
#define M 200005
#define seed 23333
#define mo 998244353
#define Mo 1000000007

using namespace std;
int i, j, m, n, p, k, fail[N], Fail[N], ans;
char c[N], s[N];
int main() {
  scanf("%s", c + 1);
  n = strlen(c + 1);
  for (i = 2; i <= n; ++i) {
    while (j && c[j + 1] != c[i])
      j = fail[j];
    if (c[j + 1] == c[i])
      ++j;
    fail[i] = j;
  }
  if (n % (n - fail[n]) != 0 || !fail[n])
    printf("1\n1\n");
  else if (n - fail[n] == 1)
    printf("%d\n1\n", n);
  else {
    for (i = 1; i <= n; ++i)
      s[n - i + 1] = c[i];
    for (i = 2; i <= n; ++i) {
      while (j && s[j + 1] != s[i])
        j = Fail[j];
      if (s[j + 1] == s[i])
        ++j;
      Fail[i] = j;
    }
    for (i = 1; i < n; ++i)
      if ((!fail[i] || i % (i - fail[i]) != 0) &&
          (!Fail[n - i] || (n - i) % (n - i - Fail[n - i]) != 0))
        ++ans;
    printf("2\n%d\n", ans);
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define ls (t << 1)
#define rs ((t << 1) + 1)
#define mid ((l + r) >> 1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 500005
#define M 200005
#define seed 23333
#define mo 998244353
#define Mo 1000000007

using namespace std;
int i, j, m, n, p, k, fail[N], Fail[N], ans;
char c[N], s[N];
int main() {
  scanf("%s", c + 1);
  n = strlen(c + 1);
  for (i = 2; i <= n; ++i) {
    while (j && c[j + 1] != c[i])
      j = fail[j];
    if (c[j + 1] == c[i])
      ++j;
    fail[i] = j;
  }
  if (n % (n - fail[n]) != 0 || !fail[n])
    printf("1\n1\n");
  else if (n - fail[n] == 1)
    printf("%d\n1\n", n);
  else {
    for (i = 1; i <= n; ++i)
      s[n - i + 1] = c[i];
    for (i = 2, j = 0; i <= n; ++i) {
      while (j && s[j + 1] != s[i])
        j = Fail[j];
      if (s[j + 1] == s[i])
        ++j;
      Fail[i] = j;
    }
    for (i = 1; i < n; ++i)
      if ((!fail[i] || i % (i - fail[i]) != 0) &&
          (!Fail[n - i] || (n - i) % (n - i - Fail[n - i]) != 0))
        ++ans;
    printf("2\n%d\n", ans);
  }
}
Problem p04019 (C++)

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

using namespace std;

char str[1005];
int state[256];

int main() {
  int n;
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < n; i++)
    state[str[i]] = true;
  if (state['N'] ^ state['S'])
    return printf("No\n"), 0;
  if (state['E'] ^ state['W'])
    return printf("No\n"), 0;
  return printf("Yes\n");
  return 0;
}

// 15:58:30

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

using namespace std;

char str[1005];
int state[256];

int main() {
  int n;
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < n; i++)
    state[str[i]] = true;
  if (state['N'] ^ state['S'])
    return printf("No\n"), 0;
  if (state['E'] ^ state['W'])
    return printf("No\n"), 0;
  printf("Yes\n");
  return 0;
}

// 15:58:30
Problem p04019 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define END(x) (*(x.end() - 1))
#define INF (INT_MAX / 2)

using namespace std;
typedef long long ll;

template <typename T> T gcd(T a, T b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main(void) {
  string s;
  cin >> s;
  map<char, bool> m;
  m['W'] = false;
  m['N'] = false;
  m['E'] = false;
  m['S'] = false;
  for (char c : s) {
    m[c] = true;
  }
  if (m['W'] == m['E'] && m['N'] == m['S']) {
    cout << "Yes";
  } else {
    cout << "No";
  }
  return 9;
}
/*

*/


Fixed submission:
#include "bits/stdc++.h"
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define END(x) (*(x.end() - 1))
#define INF (INT_MAX / 2)

using namespace std;
typedef long long ll;

template <typename T> T gcd(T a, T b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main(void) {
  string s;
  cin >> s;
  map<char, bool> m;
  m['W'] = false;
  m['N'] = false;
  m['E'] = false;
  m['S'] = false;
  for (char c : s) {
    m[c] = true;
  }
  if (m['W'] == m['E'] && m['N'] == m['S']) {
    cout << "Yes";
  } else {
    cout << "No";
  }
}
/*

*/
Problem p04019 (C++)

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

typedef long long ll;
#define fi first
#define se second

using namespace std;

int main() {

  int JN = 1;
  int JE = 1;
  int JS = 1;
  int JW = 1;

  char S[1123];

  fill(S, S + 1123, '\0');

  scanf("%S", S);

  int i;

  for (i = 0; i < 1123; i++) {
    if (S[i] == 'S') {
      JS = 0;
    } else if (S[i] == 'N') {
      JN = 0;
    } else if (S[i] == 'W') {
      JW = 0;
    } else if (S[i] == 'E') {
      JE = 0;
    }
  }

  if ((JS == JN) && (JW == JE))
    puts("Yes");
  else {
    puts("No");
  }

  return 0;
}

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

typedef long long ll;
#define fi first
#define se second

using namespace std;

int main() {

  int JN = 1;
  int JE = 1;
  int JS = 1;
  int JW = 1;

  char S[1123];

  fill(S, S + 1123, '\0');

  scanf("%s", S);

  int i;

  for (i = 0; i < 1123; i++) {
    if (S[i] == 'S') {
      JS = 0;
    } else if (S[i] == 'N') {
      JN = 0;
    } else if (S[i] == 'W') {
      JW = 0;
    } else if (S[i] == 'E') {
      JE = 0;
    }
  }

  if ((JS == JN) && (JW == JE))
    puts("Yes");
  else {
    puts("No");
  }

  return 0;
}
Problem p04019 (C++)

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

int mygcd(int x, int y) {
  if (x <= y) {
    if (x == 0)
      return y;
    else
      return mygcd(y % x, x);
  } else {
    if (y == 0)
      return x;
    else {
      return mygcd(y, x % y);
    }
  }
}

int main() {
  string S;
  cin >> S;
  int n = 0, s = 0, w = 0, e = 0, ns = 0, we = 0;

  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) == 'N')
      n++;
    else if (S.at(i) == 'S')
      s++;
    else if (S.at(i) == 'W')
      w++;
    else
      e++;
  }

  ns = n * s / mygcd(n, s);
  we = w * e / mygcd(w, e);

  if (ns > 0 && we > 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

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

int mygcd(int x, int y) {
  if (x <= y) {
    if (x == 0)
      return y;
    else
      return mygcd(y % x, x);
  } else {
    if (y == 0)
      return x;
    else {
      return mygcd(y, x % y);
    }
  }
}

int main() {
  string S;
  cin >> S;
  int n = 0, s = 0, w = 0, e = 0, ns = 0, we = 0;

  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) == 'N')
      n++;
    else if (S.at(i) == 'S')
      s++;
    else if (S.at(i) == 'W')
      w++;
    else
      e++;
  }
  if (mygcd(n, s) == 0)
    ns = 1;
  else
    ns = n * s / mygcd(n, s);
  if (mygcd(w, e) == 0)
    we = 1;
  else
    we = w * e / mygcd(w, e);

  if (ns > 0 && we > 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p04019 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
signed main() {
  char c;
  int n = 0, w = 0, s = 0, e = 0;
  while (cin >> c, c != '\n' && c != EOF) {
    if (c == 'N')
      n = 1;
    if (c == 'W')
      w = 1;
    if (c == 'S')
      s = 1;
    if (c == 'E')
      e = 1;
  }
  if ((n == 1 && s == 0) || (s == 1 && n == 0) || (w == 1 && e == 0) ||
      (e == 1 && w == 0)) {
    cout << "No";
  } else
    cout << "Yes";
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
signed main() {
  char c;
  int n = 0, w = 0, s = 0, e = 0;
  while (scanf("%c", &c), c != '\n' && c != EOF) {
    if (c == 'N')
      n = 1;
    if (c == 'W')
      w = 1;
    if (c == 'S')
      s = 1;
    if (c == 'E')
      e = 1;
  }
  if ((n == 1 && s == 0) || (s == 1 && n == 0) || (w == 1 && e == 0) ||
      (e == 1 && w == 0)) {
    cout << "No";
  } else
    cout << "Yes";
}
Problem p04020 (C++)

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

using namespace std;
typedef long long ll;
const double PI = 3.14159265;

void fastInOut();

const int N = 100005;
int a[N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
#endif

  fastInOut();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      continue;
    if (i + 1 < n) {
      int sum = a[i] + a[i + 1];
      res += sum / 2;
      if (sum % 2 && a[i + 1])
        a[i + 1] = 1;
      else
        a[i + 1] = 0;

    } else
      res += a[i] / 2;
  }
  cout << res;
  return 0;
}

void fastInOut() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
}

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

using namespace std;
typedef long long ll;
const double PI = 3.14159265;

void fastInOut();

const int N = 100005;
int a[N];

int main() {

  fastInOut();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      continue;
    if (i + 1 < n) {
      int sum = a[i] + a[i + 1];
      res += sum / 2;
      if (sum % 2 && a[i + 1])
        a[i + 1] = 1;
      else
        a[i + 1] = 0;

    } else
      res += a[i] / 2;
  }
  cout << res;
  return 0;
}

void fastInOut() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
}
Problem p04020 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("trapv")
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
const int maxn = 505, mod = 924844033;
int n, a[maxn];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += a[i] / 2;
    a[i] &= 1;
    if (a[i] && a[i + 1])
      ans++, a[i]--, a[i + 1]--;
  }
  cout << ans;
}


Fixed submission:
#pragma GCC optimize("trapv")
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
const int maxn = 1 << 20, mod = 924844033;
int n, a[maxn];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += a[i] / 2;
    a[i] &= 1;
    if (a[i] && a[i + 1])
      ans++, a[i]--, a[i + 1]--;
  }
  cout << ans;
}
Problem p04020 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int a[10005], n;
int main() {
  long s = 0, c = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    c = a[i];
    while (i < n && a[i + 1] != 0) {
      c += a[i + 1];
      i++;
    }
    s += c / 2;
  }
  cout << s << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int a[100005], n;
int main() {
  long s = 0, c = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    c = a[i];
    while (i < n && a[i + 1] != 0) {
      c += a[i + 1];
      i++;
    }
    s += c / 2;
  }
  cout << s << endl;
  return 0;
}
Problem p04020 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

signed main() {

  int N;
  cin >> N;
  vec A(N);
  readv(A);

  int ans = 0;
  vec p(0);
  REP(i, N) {
    int tmp = max(0LL, (A[i] - 2) / 2);
    ans += tmp;
    A[i] -= 2 * tmp;
    REP(k, A[i]) p.push_back(i);
  }

  int i = 0;
  while (i < p.size() - 1) {
    if (p[i + 1] - p[i] <= 1) {
      ans++;
      i += 2;
    } else
      i++;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

signed main() {

  int N;
  cin >> N;
  vec A(N);
  readv(A);

  int ans = 0;
  vec p(0);
  REP(i, N) {
    int tmp = max(0LL, (A[i] - 2) / 2);
    ans += tmp;
    A[i] -= 2 * tmp;
    REP(k, A[i]) p.push_back(i);
  }

  int i = 0;
  while (i < (int)p.size() - 1) {
    if (p[i + 1] - p[i] <= 1) {
      ans++;
      i += 2;
    } else
      i++;
  }
  cout << ans;

  return 0;
}