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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs;
  ll val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 20];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}
int update(int pre, int l, int r, int pos, ll v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}
ll query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}
int a[maxn], vis[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  root[0] = build(1, n); // 根节点
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    ll tmp = query(root[r], 1, n, l);
    printf("%lld\n", tmp);
  }
}
int main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs;
  ll val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 100];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}
int update(int pre, int l, int r, int pos, ll v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}
ll query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}
int a[maxn], vis[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  root[0] = build(1, n); // 根节点
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    ll tmp = query(root[r], 1, n, l);
    printf("%lld\n", tmp);
  }
}
int main() {
  solve();
  return 0;
}
Problem p02599 (C++)

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

const int N = 500000;

int pre[N + 5];

template <int M, int SZ> struct Persistable_Segment_Tree {
  int tot, p, k, L, R;
  int rt[M + 5], lc[SZ + 5], rc[SZ + 5], cntv[SZ + 5];
  void init() { tot = rt[0] = lc[0] = rc[0] = cntv[0] = 0; }
  int update(int pre, int l, int r, int k) {
    int now = ++tot;
    if (l == r) {
      cntv[now] = cntv[pre] + k;
    } else {
      int m = (l + r) >> 1;
      if (p <= m) {
        lc[now] = update(lc[pre], l, m, k);
        rc[now] = rc[pre];
      } else {
        rc[now] = update(rc[pre], m + 1, r, k);
        lc[now] = lc[pre];
      }
      cntv[now] = cntv[lc[now]] + cntv[rc[now]];
    }
    return now;
  }
  void query(int o, int l, int r) {
    if (L <= l && r <= R) {
      k += cntv[o];
    } else {
      int m = (l + r) >> 1;
      if (L <= m)
        query(lc[o], l, m);
      if (R > m)
        query(rc[o], m + 1, r);
    }
  }
};

Persistable_Segment_Tree<N, N * 20> psegt;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    int rt = psegt.rt[i - 1];
    if (pre[x]) {
      psegt.p = pre[x];
      rt = psegt.update(rt, 1, n, -1);
    }
    pre[x] = i;
    psegt.p = i;
    rt = psegt.update(rt, 1, n, 1);
    psegt.rt[i] = rt;
  }
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    psegt.L = l;
    psegt.R = r;
    psegt.k = 0;
    psegt.query(psegt.rt[r], 1, n);
    printf("%d\n", psegt.k);
  }
  return 0;
}

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

const int N = 500000;

int pre[N + 5];

template <int M, int SZ> struct Persistable_Segment_Tree {
  int tot, p, k, L, R;
  int rt[M + 5], lc[SZ + 5], rc[SZ + 5], cntv[SZ + 5];
  void init() { tot = rt[0] = lc[0] = rc[0] = cntv[0] = 0; }
  int update(int pre, int l, int r, int k) {
    int now = ++tot;
    if (l == r) {
      cntv[now] = cntv[pre] + k;
    } else {
      int m = (l + r) >> 1;
      if (p <= m) {
        lc[now] = update(lc[pre], l, m, k);
        rc[now] = rc[pre];
      } else {
        rc[now] = update(rc[pre], m + 1, r, k);
        lc[now] = lc[pre];
      }
      cntv[now] = cntv[lc[now]] + cntv[rc[now]];
    }
    return now;
  }
  void query(int o, int l, int r) {
    if (L <= l && r <= R) {
      k += cntv[o];
    } else {
      int m = (l + r) >> 1;
      if (L <= m)
        query(lc[o], l, m);
      if (R > m)
        query(rc[o], m + 1, r);
    }
  }
};

Persistable_Segment_Tree<N, N * 40> psegt;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    int rt = psegt.rt[i - 1];
    if (pre[x]) {
      psegt.p = pre[x];
      rt = psegt.update(rt, 1, n, -1);
    }
    pre[x] = i;
    psegt.p = i;
    rt = psegt.update(rt, 1, n, 1);
    psegt.rt[i] = rt;
  }
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    psegt.L = l;
    psegt.R = r;
    psegt.k = 0;
    psegt.query(psegt.rt[r], 1, n);
    printf("%d\n", psegt.k);
  }
  return 0;
}
Problem p02599 (C++)

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

using namespace std;
using ll = long long;

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

template <typename T> struct fenwick_tree {
  int n;
  vector<T> d;
  fenwick_tree(int n = 0) : n(n), d(n + 1) {}

  void add(int i, T x) {
    i++;
    while (i <= n) {
      d[i] += x;
      i += i & -i;
    }
  }
  T sum(int i) {
    i++;
    T x = 0;
    while (i > 0) {
      x += d[i];
      i -= i & -i;
    }
    return x;
  }
};

int main() {
  ll N, Q, l, r, idx = 0;
  cin >> N >> Q;
  vector<ll> c(N), good(N, -1), ans(N);
  REP(i, N) {
    cin >> c[i];
    c[i]--;
  }
  vector<tuple<ll, ll, ll>> query(Q);
  REP(i, Q) {
    cin >> l >> r;
    query[i] = {r - 1, l - 1, i};
  }

  sort(query.begin(), query.end());
  fenwick_tree<ll> fw(N);

  REP(i, N) {
    if (good[c[i]] != -1)
      fw.add(good[c[i]], -1);
    good[c[i]] = i;
    fw.add(i, 1);

    while (idx != Q && get<0>(query[idx]) == i) {
      ans[get<2>(query[idx])] =
          fw.sum(get<0>(query[idx])) - fw.sum(get<1>(query[idx]) - 1);
      idx++;
    }
  }

  REP(i, Q) cout << ans[i] << endl;
}

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

using namespace std;
using ll = long long;

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

template <typename T> struct fenwick_tree {
  int n;
  vector<T> d;
  fenwick_tree(int n = 0) : n(n), d(n + 1) {}

  void add(int i, T x) {
    i++;
    while (i <= n) {
      d[i] += x;
      i += i & -i;
    }
  }
  T sum(int i) {
    i++;
    T x = 0;
    while (i > 0) {
      x += d[i];
      i -= i & -i;
    }
    return x;
  }
};

int main() {
  ll N, Q, l, r, idx = 0;
  cin >> N >> Q;
  vector<ll> c(N), good(N, -1), ans(Q);
  REP(i, N) {
    cin >> c[i];
    c[i]--;
  }
  vector<tuple<ll, ll, ll>> query(Q);
  REP(i, Q) {
    cin >> l >> r;
    query[i] = {r - 1, l - 1, i};
  }

  sort(query.begin(), query.end());
  fenwick_tree<ll> fw(N);

  REP(i, N) {
    if (good[c[i]] != -1)
      fw.add(good[c[i]], -1);
    good[c[i]] = i;
    fw.add(i, 1);

    while (idx != Q && get<0>(query[idx]) == i) {
      ans[get<2>(query[idx])] =
          fw.sum(get<0>(query[idx])) - fw.sum(get<1>(query[idx]) - 1);
      idx++;
    }
  }

  REP(i, Q) cout << ans[i] << endl;
}
Problem p02599 (C++)

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

using namespace std;

const int BLOCK = 707;

int c[500005], ans[500005], res, cnt[500005];

struct dat {
  int l, r, id;

  bool operator<(const dat &T) const {
    if (l / BLOCK != T.l / BLOCK)
      return l / BLOCK < T.l / BLOCK;
    return r / BLOCK < T.r / BLOCK;
  }
} a[500005];

void remove(int id) {
  cnt[c[id]]--;
  if (cnt[c[id]] == 0)
    res--;
}

void add(int id) {
  cnt[c[id]]++;
  if (cnt[c[id]] == 1)
    res++;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);

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

  for (int i = 0; i < q; i++) {
    scanf("%d%d", &a[i].l, &a[i].r);
    a[i].l--;
    a[i].r--;
    a[i].id = i;
  }

  sort(a, a + q);

  int l = 0;
  int r = -1;
  res = 0;

  for (int i = 0; i < q; i++) {
    while (l < a[i].l)
      remove(l++);
    while (l > a[i].l)
      add(--l);
    while (r < a[i].r)
      add(++r);
    while (r > a[i].r)
      remove(r--);

    ans[a[i].id] = res;
  }

  for (int i = 0; i < q; i++)
    printf("%d\n", ans[i]);
}


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

using namespace std;

const int BLOCK = 707;

int c[500005], ans[500005], res, cnt[500005];

struct dat {
  int l, r, id;

  bool operator<(const dat &T) const {
    if (l / BLOCK != T.l / BLOCK)
      return l / BLOCK < T.l / BLOCK;
    return r < T.r;
  }
} a[500005];

void remove(int id) {
  cnt[c[id]]--;
  if (cnt[c[id]] == 0)
    res--;
}

void add(int id) {
  cnt[c[id]]++;
  if (cnt[c[id]] == 1)
    res++;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);

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

  for (int i = 0; i < q; i++) {
    scanf("%d%d", &a[i].l, &a[i].r);
    a[i].l--;
    a[i].r--;
    a[i].id = i;
  }

  sort(a, a + q);

  int l = 0;
  int r = -1;
  res = 0;

  for (int i = 0; i < q; i++) {
    while (l < a[i].l)
      remove(l++);
    while (l > a[i].l)
      add(--l);
    while (r < a[i].r)
      add(++r);
    while (r > a[i].r)
      remove(r--);

    ans[a[i].id] = res;
  }

  for (int i = 0; i < q; i++)
    printf("%d\n", ans[i]);
}
Problem p02599 (C++)

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

using namespace std;

template <typename T> class BIT {
  vector<T> tree_;

public:
  BIT(int n) : tree_(n, 0) {}
  void Add(int i, T x) {
    for (i++; i <= tree_.size(); i += i & -i) {
      tree_[i - 1] += x;
    }
  }

  T Sum(int s) {
    T ans = 0;
    for (s++; s > 0; s -= s & -s) {
      ans += tree_[s - 1];
    }

    return ans;
  }

  T Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
};

int main(int argc, const char *argv[]) {
  int n, q;
  cin >> n >> q;

  vector<int> vc(n);
  for (int i = 0; i < n; ++i) {
    cin >> vc[i];
    vc[i]--;
  }

  vector<array<int, 3>> vq(n);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    vq[i][0] = r - 1;
    vq[i][1] = l - 1;
    vq[i][2] = i;
  }

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

  BIT<int> bit(n);
  vector<int> vp(n, -1), ans(q);
  int prev = 0;
  for (auto &q : vq) {
    int l, r, idx;
    r = q[0];
    l = q[1];
    idx = q[2];
    for (int i = prev; i <= r; ++i) {
      int c = vc[i];
      if (vp[c] < i) {
        if (vp[c] != -1) {
          bit.Add(vp[c], -1);
        }

        bit.Add(i, 1);
        vp[c] = i;
      }

      ans[idx] = bit.Sum(l, r);
    }

    prev = r;
  }

  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << '\n';
  }
  return 0;
}

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

using namespace std;

template <typename T> class BIT {
  vector<T> tree_;

public:
  BIT(int n) : tree_(n, 0) {}
  void Add(int i, T x) {
    for (i++; i <= tree_.size(); i += i & -i) {
      tree_[i - 1] += x;
    }
  }

  T Sum(int s) {
    T ans = 0;
    for (s++; s > 0; s -= s & -s) {
      ans += tree_[s - 1];
    }

    return ans;
  }

  T Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
};

int main(int argc, const char *argv[]) {
  int n, q;
  cin >> n >> q;

  vector<int> vc(n);
  for (int i = 0; i < n; ++i) {
    cin >> vc[i];
    vc[i]--;
  }

  vector<array<int, 3>> vq(q);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    vq[i][0] = r - 1;
    vq[i][1] = l - 1;
    vq[i][2] = i;
  }

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

  BIT<int> bit(n);
  vector<int> vp(n, -1), ans(q);
  int prev = 0;
  for (auto &q : vq) {
    int l, r, idx;
    r = q[0];
    l = q[1];
    idx = q[2];
    for (int i = prev; i <= r; ++i) {
      int c = vc[i];
      if (vp[c] < i) {
        if (vp[c] != -1) {
          bit.Add(vp[c], -1);
        }

        bit.Add(i, 1);
        vp[c] = i;
      }

      ans[idx] = bit.Sum(l, r);
    }

    prev = r;
  }

  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << '\n';
  }
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (a); i > (n); i--)
typedef long long ll;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
using namespace std;

int block, curl, curr, res;
struct qry {
  int id, l, r;
} q[maxn];
int ans[maxn], a[maxn];

unordered_map<int, int> mp;

void add(int pos) {
  if (mp[a[pos]] == 0) {
    res++;
    mp[a[pos]] = 1;
  } else {
    mp[a[pos]]++;
  }
}

void del(int pos) {
  if (mp[a[pos]] - 1 == 0) {
    mp[a[pos]] = 0;
    res--;
  } else {
    mp[a[pos]]--;
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  block = sqrt(n);
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, m + 1) cin >> q[i].l >> q[i].r, q[i].id = i;
  sort(q + 1, q + m + 1, [](qry x, qry y) {
    if (x.l / block == y.l / block)
      return x.r < y.r;
    return x.l / block < y.l / block;
  });
  curl = curr = res = 0;
  for (int i = 1; i <= m; i++) {
    while (curr > q[i].r)
      del(curr--);
    while (curr < q[i].r)
      add(++curr);
    while (curl > q[i].l)
      add(--curl);
    while (curl < q[i].l)
      del(curl++);
    ans[q[i].id] = res;
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << '\n';
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin);
  // freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout);
#endif

  // int t; cin >> t; while(t--)
  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (a); i > (n); i--)
typedef long long ll;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
using namespace std;

int block, curl, curr, res;
struct qry {
  int id, l, r;
} q[maxn];
int ans[maxn], a[maxn], mp[maxn];

void add(int pos) {
  if (mp[a[pos]] == 0) {
    res++;
    mp[a[pos]] = 1;
  } else {
    mp[a[pos]]++;
  }
}

void del(int pos) {
  if (mp[a[pos]] - 1 == 0) {
    mp[a[pos]] = 0;
    res--;
  } else {
    mp[a[pos]]--;
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  block = sqrt(n);
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, m + 1) cin >> q[i].l >> q[i].r, q[i].id = i;
  sort(q + 1, q + m + 1, [](qry x, qry y) {
    if (x.l / block == y.l / block)
      return x.r < y.r;
    return x.l / block < y.l / block;
  });
  curl = curr = res = 0;
  for (int i = 1; i <= m; i++) {
    while (curr > q[i].r)
      del(curr--);
    while (curr < q[i].r)
      add(++curr);
    while (curl > q[i].l)
      add(--curl);
    while (curl < q[i].l)
      del(curl++);
    ans[q[i].id] = res;
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << '\n';
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin);
  // freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout);
#endif

  // int t; cin >> t; while(t--)
  solve();

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs, val;
} tre[maxn * 40];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}

int update(int pre, int l, int r, int pos, int v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}

int query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}

int a[maxn], vis[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  root[0] = build(1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int tmp = query(root[r], 1, n, l);
    printf("%d\n", tmp);
  }
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs, val;
} tre[maxn * 50];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}

int update(int pre, int l, int r, int pos, int v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}

int query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}

int a[maxn], vis[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  root[0] = build(1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int tmp = query(root[r], 1, n, l);
    printf("%d\n", tmp);
  }
}

int main() {
  solve();
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 30005;
const int maxq = 200005;
const int maxm = 1000005;
int n, sqr, Q;
int a[maxn];
struct query {
  int l, r, lb, pos;
} q[maxq];
bool cmp(query a, query b) {
  if (a.lb == b.lb)
    return a.lb & 1 ? a.r > b.r : a.r < b.r;
  return a.lb < b.lb;
}
int ans[maxq], num[maxm], cnt;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
inline void write(int x) {
  if (x / 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void solve() {
  int l = 1, r = 0;
  for (int i = 1; i <= Q; ++i) {
    while (r < q[i].r)
      cnt += !num[a[++r]]++;
    while (r > q[i].r)
      cnt -= !--num[a[r--]];
    while (l > q[i].l)
      cnt += !num[a[--l]]++;
    while (l < q[i].l)
      cnt -= !--num[a[l++]];
    ans[q[i].pos] = cnt;
  }
  for (int i = 1; i <= Q; ++i)
    write(ans[i]), puts("");
}
int main() {
  n = read();
  Q = read();
  for (int i = 0; i < n; ++i)
    a[i] = read();
  sqr = sqrt(n) + 1;
  for (int i = 1; i <= Q; ++i) {
    q[i].l = read();
    q[i].r = read();
    --q[i].l;
    --q[i].r;
    q[i].pos = i;
    q[i].lb = q[i].l / sqr;
  }
  sort(q + 1, q + Q + 1, cmp);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 500005;
const int maxq = 500005;
const int maxm = 500005;
int n, sqr, Q;
int a[maxn];
struct query {
  int l, r, lb, pos;
} q[maxq];
bool cmp(query a, query b) {
  if (a.lb == b.lb)
    return a.lb & 1 ? a.r > b.r : a.r < b.r;
  return a.lb < b.lb;
}
int ans[maxq], num[maxm], cnt;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
inline void write(int x) {
  if (x / 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void solve() {
  int l = 1, r = 0;
  for (int i = 1; i <= Q; ++i) {
    while (r < q[i].r)
      cnt += !num[a[++r]]++;
    while (r > q[i].r)
      cnt -= !--num[a[r--]];
    while (l > q[i].l)
      cnt += !num[a[--l]]++;
    while (l < q[i].l)
      cnt -= !--num[a[l++]];
    ans[q[i].pos] = cnt;
  }
  for (int i = 1; i <= Q; ++i)
    write(ans[i]), puts("");
}
int main() {
  n = read();
  Q = read();
  for (int i = 0; i < n; ++i)
    a[i] = read();
  sqr = sqrt(n) + 1;
  for (int i = 1; i <= Q; ++i) {
    q[i].l = read();
    q[i].r = read();
    --q[i].l;
    --q[i].r;
    q[i].pos = i;
    q[i].lb = q[i].l / sqr;
  }
  sort(q + 1, q + Q + 1, cmp);
  solve();
  return 0;
}
Problem p02599 (C++)

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

template <typename T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, int>> vp(q), in(n), pnt;
  for (int i = 0; i < n; i++) {
    cin >> in[i].first;
    in[i].second = i;
  }
  sort(in.begin(), in.end());
  for (int i = 0; i < n - 1; i++) {
    if (in[i].first == in[i + 1].first) {
      pnt.emplace_back(in[i].second, in[i + 1].second);
    }
  }
  sort(pnt.begin(), pnt.end(),
       [&](auto a, auto b) { return a.first < b.first; });
  // for(auto i:pnt)cout<<i.first<<' '<<i.second<<endl;
  for (int i = 0; i < q; i++)
    cin >> vp[i].first >> vp[i].second, vp[i].first--, vp[i].second--;
  auto oder = vp;
  map<pair<int, int>, int> ans;
  sort(vp.begin(), vp.end(), [&](auto a, auto b) { return a.first < b.first; });
  BinaryIndexedTree<int> BIT(133333);
  while (vp.size()) {

    if ((!pnt.empty()) && vp.back().first <= pnt.back().first) {
      BIT.add(pnt.back().second, 1);
      pnt.pop_back();
    } else {
      auto now = vp.back();
      ans[now] = 1 + now.second - now.first - BIT.sum(now.second);
      vp.pop_back();
    }
  }
  // 出力
  for (int i = 0; i < q; i++)
    cout << ans[oder[i]] << endl;
}


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

template <typename T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, int>> vp(q), in(n), pnt;
  for (int i = 0; i < n; i++) {
    cin >> in[i].first;
    in[i].second = i;
  }
  sort(in.begin(), in.end());
  for (int i = 0; i < n - 1; i++) {
    if (in[i].first == in[i + 1].first) {
      pnt.emplace_back(in[i].second, in[i + 1].second);
    }
  }
  sort(pnt.begin(), pnt.end(),
       [&](auto a, auto b) { return a.first < b.first; });
  // for(auto i:pnt)cout<<i.first<<' '<<i.second<<endl;
  for (int i = 0; i < q; i++)
    cin >> vp[i].first >> vp[i].second, vp[i].first--, vp[i].second--;
  auto oder = vp;
  map<pair<int, int>, int> ans;
  sort(vp.begin(), vp.end(), [&](auto a, auto b) { return a.first < b.first; });
  BinaryIndexedTree<int> BIT(533333);
  while (vp.size()) {

    if ((!pnt.empty()) && vp.back().first <= pnt.back().first) {
      BIT.add(pnt.back().second, 1);
      pnt.pop_back();
    } else {
      auto now = vp.back();
      ans[now] = 1 + now.second - now.first - BIT.sum(now.second);
      vp.pop_back();
    }
  }
  // 出力
  for (int i = 0; i < q; i++)
    cout << ans[oder[i]] << endl;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
typedef long long ll;
typedef long double db;
const int N = 3e5 + 5, M = 1e9 + 7;

typedef pair<int, int> pii;
#define fi first
#define se second

int bit[N];
void add(int p, int v) {
  for (; p < N; p += p & -p)
    bit[p] += v;
}
int get(int p) {
  int res = 0;
  for (; p; p -= p & -p)
    res += bit[p];
  return res;
}
vector<pii> q[N];
int ans[N], lst[N], a[N];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int l, r;
  for (int i = 1; i <= m; i++) {
    cin >> l >> r;
    q[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (lst[a[i]])
      add(lst[a[i]], -1);
    add(i, 1);
    lst[a[i]] = i;
    for (auto &p : q[i]) {
      ans[p.se] = get(i) - get(p.fi - 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
typedef long long ll;
typedef long double db;
const int N = 5e5 + 5, M = 1e9 + 7;

typedef pair<int, int> pii;
#define fi first
#define se second

int bit[N];
void add(int p, int v) {
  for (; p < N; p += p & -p)
    bit[p] += v;
}
int get(int p) {
  int res = 0;
  for (; p; p -= p & -p)
    res += bit[p];
  return res;
}
vector<pii> q[N];
int ans[N], lst[N], a[N];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int l, r;
  for (int i = 1; i <= m; i++) {
    cin >> l >> r;
    q[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (lst[a[i]])
      add(lst[a[i]], -1);
    add(i, 1);
    lst[a[i]] = i;
    for (auto &p : q[i]) {
      ans[p.se] = get(i) - get(p.fi - 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << '\n';
  }
}
Problem p02599 (C++)

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

constexpr int B = 400;
struct Q {
  int L, R, id;
  bool operator<(const Q &o) const { return pii{L / B, R} < pii{o.L / B, o.R}; }
};

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int n, nq;
  cin >> n >> nq;
  map<int, vi> f;
  rep(i, 0, n) {
    int a;
    cin >> a;
    f[a].push_back(i);
  }
  vi x(n);
  int k = 0;
  trav(p, f) {
    trav(i, p.second) x[i] = k;
    k++;
  }
  vi cnt(k);
  vector<Q> q(nq);
  rep(i, 0, nq) {
    cin >> q[i].L >> q[i].R;
    q[i].L--, q[i].R--;
    q[i].id = i;
  }
  sort(all(q));
  int L = 0, R = -1, dc = 0;
  vi qans(nq);
  auto add = [&](int i) {
    if (++cnt[i] == 1)
      dc++;
  };
  auto rem = [&](int i) {
    if (--cnt[i] == 0)
      dc--;
  };
  trav(cq, q) {
    while (L < cq.L)
      rem(x[L++]);
    while (L > cq.L)
      add(x[--L]);
    while (R < cq.R)
      add(x[++R]);
    while (R > cq.R)
      rem(x[R--]);
    qans[cq.id] = dc;
  }
  trav(i, qans) cout << i << '\n';
  return 0;
}

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

constexpr int B = 900;
struct Q {
  int L, R, id;
  bool operator<(const Q &o) const { return pii{L / B, R} < pii{o.L / B, o.R}; }
};

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int n, nq;
  cin >> n >> nq;
  map<int, vi> f;
  rep(i, 0, n) {
    int a;
    cin >> a;
    f[a].push_back(i);
  }
  vi x(n);
  int k = 0;
  trav(p, f) {
    trav(i, p.second) x[i] = k;
    k++;
  }
  vi cnt(k);
  vector<Q> q(nq);
  rep(i, 0, nq) {
    cin >> q[i].L >> q[i].R;
    q[i].L--, q[i].R--;
    q[i].id = i;
  }
  sort(all(q));
  int L = 0, R = -1, dc = 0;
  vi qans(nq);
  auto add = [&](int i) {
    if (++cnt[i] == 1)
      dc++;
  };
  auto rem = [&](int i) {
    if (--cnt[i] == 0)
      dc--;
  };
  trav(cq, q) {
    while (L < cq.L)
      rem(x[L++]);
    while (L > cq.L)
      add(x[--L]);
    while (R < cq.R)
      add(x[++R]);
    while (R > cq.R)
      rem(x[R--]);
    qans[cq.id] = dc;
  }
  trav(i, qans) cout << i << '\n';
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FASTINOUT                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const int N = 5e5 + 9;
int a[N], Block, value, m[N];
bool ok[N];
struct query {
  int l, r, i;
};
query q[N];
bool f(query q1, query q2) {
  if (q1.l / Block != q2.l / Block)
    return q1.l / Block < q2.l / Block;

  return q1.r < q2.r;
}
void add(int pos) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}
int main() {
  FASTINOUT;
  int x, t;
  scanf("%d%d", &x, &t);
  Block = sqrtl(x);
  for (int i = 0; i < x; i++)
    scanf("%d", &a[i]);
  for (int j = 0; j < t; j++) {
    scanf("%d%d", &q[j].l, &q[j].r);
    q[j].l--;
    q[j].r--;
    q[j].i = j;
  }
  sort(q, q + t, f);
  int start = 0, end = -1;
  vector<int> ans(t);
  for (int i = 0; i < t; i++) {
    int L = q[i].l, R = q[i].r;

    while (start > L)
      start--, add(start);

    while (end < R)
      end++, add(end);

    while (start < L)
      remove(start), start++;

    while (end > R)
      remove(end), end--;

    ans[q[i].i] = value;
  }
  for (int j : ans)
    printf("%d\n", j);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FASTINOUT                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const int N = 5e5 + 9;
int a[N], Block, value, m[N];
bool ok[N];
struct query {
  int l, r, i;
};
query q[N];
bool f(query a, query b) {
  if (a.l / Block != b.l / Block)
    return a.l < b.l;
  return a.l / Block % 2 ? a.r < b.r : a.r > b.r;
}
void add(int pos) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}
int main() {
  FASTINOUT;
  int x, t;
  scanf("%d%d", &x, &t);
  Block = sqrtl(x);
  for (int i = 0; i < x; i++)
    scanf("%d", &a[i]);
  for (int j = 0; j < t; j++) {
    scanf("%d%d", &q[j].l, &q[j].r);
    q[j].l--;
    q[j].r--;
    q[j].i = j;
  }
  sort(q, q + t, f);
  int start = 0, end = -1;
  vector<int> ans(t);
  for (int i = 0; i < t; i++) {
    int L = q[i].l, R = q[i].r;

    while (start > L)
      start--, add(start);

    while (end < R)
      end++, add(end);

    while (start < L)
      remove(start), start++;

    while (end > R)
      remove(end), end--;

    ans[q[i].i] = value;
  }
  for (int j : ans)
    printf("%d\n", j);
  return 0;
}
Problem p02599 (C++)

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

#define ll long long
#define pll pair<ll, ll>

#define left 2 * node + 1
#define right left + 1
#define mid (s + e >> 1)

const ll N = 5e5 + 5;
vector<ll> T[4 * N];

void build(ll A[], ll node, ll s, ll e) {
  if (s == e) {
    T[node].push_back(A[s]);
    return;
  }
  build(A, left, s, mid);
  build(A, right, mid + 1, e);
  merge(T[left].begin(), T[left].end(), T[right].begin(), T[right].end(),
        back_inserter(T[node]));
}

ll query(ll node, ll s, ll e, ll x, ll y) {
  if (e < s || s > y || e < x)
    return 0;
  if (x <= s && e <= y) {
    ll a = upper_bound(T[node].begin(), T[node].end(), y) - T[node].begin();
    return T[node].size() - a;
  }
  ll p1 = query(left, s, mid, x, y);
  ll p2 = query(right, 1 + mid, e, x, y);
  return p1 + p2;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, q;
  cin >> n >> q;
  ll A[n];
  ll B[n];
  for (ll i = 0; i < n; i++)
    cin >> A[i];
  map<ll, ll> M;
  for (ll i = n - 1; i >= 0; i--) {
    if (M.count(A[i]))
      B[i] = M[A[i]];
    else
      B[i] = INT_MAX;
    M[A[i]] = i;
  }

  build(B, 0, 0, n - 1);

  while (q--) {
    ll x, y;
    cin >> x >> y;
    ll ans = query(0, 0, n - 1, x - 1, y - 1);
    cout << ans << endl;
  }
}


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

#define ll long long
#define pll pair<ll, ll>

#define left 2 * node + 1
#define right left + 1
#define mid (s + e >> 1)

const ll N = 5e5 + 5;
vector<ll> T[4 * N];

void build(ll A[], ll node, ll s, ll e) {
  if (s == e) {
    T[node].push_back(A[s]);
    return;
  }
  build(A, left, s, mid);
  build(A, right, mid + 1, e);
  merge(T[left].begin(), T[left].end(), T[right].begin(), T[right].end(),
        back_inserter(T[node]));
}

ll query(ll node, ll s, ll e, ll x, ll y) {
  if (e < s || s > y || e < x)
    return 0;
  if (x <= s && e <= y) {
    ll a = upper_bound(T[node].begin(), T[node].end(), y) - T[node].begin();
    return T[node].size() - a;
  }
  ll p1 = query(left, s, mid, x, y);
  ll p2 = query(right, 1 + mid, e, x, y);
  return p1 + p2;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, q;
  cin >> n >> q;
  ll A[n];
  ll B[n];
  for (ll i = 0; i < n; i++)
    cin >> A[i];
  map<ll, ll> M;
  for (ll i = n - 1; i >= 0; i--) {
    if (M.count(A[i]))
      B[i] = M[A[i]];
    else
      B[i] = INT_MAX;
    M[A[i]] = i;
  }

  build(B, 0, 0, n - 1);

  while (q--) {
    ll x, y;
    cin >> x >> y;
    ll ans = query(0, 0, n - 1, x - 1, y - 1);
    cout << ans << "\n";
  }
}
Problem p02600 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int n;
  scanf("%d", n);
  if (n <= 1999 && n >= 1800) {
    printf("1");
  } else if (n <= 1799 && n >= 1600) {
    printf("2");
  } else if (n <= 1599 && n >= 1400) {
    printf("3");
  } else if (n <= 1399 && n >= 1200) {
    printf("4");
  } else if (n <= 1199 && n >= 1000) {
    printf("5");
  } else if (n <= 999 && n >= 800) {
    printf("6");
  } else if (n <= 799 && n >= 600) {
    printf("7");
  } else if (n <= 599 && n >= 400) {
    printf("8");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int n;
  scanf("%d", &n);
  if (n <= 1999 && n >= 1800) {
    printf("1");
  } else if (n <= 1799 && n >= 1600) {
    printf("2");
  } else if (n <= 1599 && n >= 1400) {
    printf("3");
  } else if (n <= 1399 && n >= 1200) {
    printf("4");
  } else if (n <= 1199 && n >= 1000) {
    printf("5");
  } else if (n <= 999 && n >= 800) {
    printf("6");
  } else if (n <= 799 && n >= 600) {
    printf("7");
  } else if (n <= 599 && n >= 400) {
    printf("8");
  }
  return 0;
}
Problem p02600 (C++)

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

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

  assert(400 <= X && X <= 1999);
  int kyu;
  if (X < 600)
    kyu = 8;
  else if (X < 800)
    kyu = 7;
  else if (X < 1000)
    kyu = 6;
  else if (X < 1200)
    kyu = 5;
  else if (X < 1400)
    kyu = 4;
  else if (X < 1600)
    kyu = 3;
  else if (X < 1800)
    kyu = 2;
  else if (X < 200)
    kyu = 1;
  else
    throw runtime_error("what's?");

  cout << kyu << endl;
  return 0;
}


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

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

  assert(400 <= X && X <= 1999);
  int kyu;
  if (X < 600)
    kyu = 8;
  else if (X < 800)
    kyu = 7;
  else if (X < 1000)
    kyu = 6;
  else if (X < 1200)
    kyu = 5;
  else if (X < 1400)
    kyu = 4;
  else if (X < 1600)
    kyu = 3;
  else if (X < 1800)
    kyu = 2;
  else if (X < 2000)
    kyu = 1;
  else
    throw runtime_error("what's?");

  cout << kyu << endl;
  return 0;
}
Problem p02600 (Python)

Buggy submission (status: Runtime Error):
X = int(input())

if 400 <= X < 600:
    result = 8
elif X < 800:
    result = 7
elif X < 1000:
    result = 6
elif X < 1200:
    result = 5
elif X < 1400:
    result = 4
elif X < 1600:
    result = 3
elif X < 1800:
    result = 2
elif X < 200:
    result = 1

print(result)


Fixed submission:
X = int(input())

if 400 <= X < 600:
    result = 8
elif X < 800:
    result = 7
elif X < 1000:
    result = 6
elif X < 1200:
    result = 5
elif X < 1400:
    result = 4
elif X < 1600:
    result = 3
elif X < 1800:
    result = 2
elif X < 2000:
    result = 1

print(result)
Problem p02601 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int A, B, C, K;
  bool flag = false;

  for (int i = 0; i <= K; ++i) {
    for (int j = 0; j <= K; ++j) {
      for (int l = 0; l <= K; ++l) {
        int x = A * (1 << i), y = B * (1 << j), z = C * (1 << l);
        if (x < y && y < z && i + j + l <= K)
          flag = true;
      }
    }
  }

  if (flag == true)
    cout << "Yes";
  else
    cout << "No";
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int A, B, C, K;
  bool flag = false;
  cin >> A >> B >> C >> K;
  for (int i = 0; i <= K; ++i) {
    for (int j = 0; j <= K; ++j) {
      for (int l = 0; l <= K; ++l) {
        int x = A * (1 << i), y = B * (1 << j), z = C * (1 << l);
        if (x < y && y < z && i + j + l <= K)
          flag = true;
      }
    }
  }

  if (flag == true)
    cout << "Yes";
  else
    cout << "No";
}
Problem p02601 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
// const int mod = 1e9 + 7;
// const int mod = 998244353;

inline void quickread() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

inline void work() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  // c > b > a
  while (k) {
    if (b <= a) {
      b *= 2;
      k--;
    } else if (c <= b) {
      c *= 2;
      k--;
    }
  }
  if (c > b && b > a)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  // freopen(".txt", "r", stdin);
  quickread();
  work();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
// const int mod = 1e9 + 7;
// const int mod = 998244353;

inline void quickread() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

inline void work() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  // c > b > a
  while (k) {
    if (b <= a) {
      b *= 2;
      k--;
    } else if (c <= b) {
      c *= 2;
      k--;
    } else
      break;
  }
  if (c > b && b > a)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  // freopen(".txt", "r", stdin);
  quickread();
  work();
  return 0;
}
Problem p02601 (C++)

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

int main() {
  int A, B, C, K;
  cin >> A >> B >> C;
  cin >> K;

  while (K > 0) {
    if (B <= A) {
      B *= 2;
      K--;
    } else if (C <= B) {
      C *= 2;
      K--;
    }
  }

  if (B > A && C > B) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}


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

int main() {
  int A, B, C, K;
  cin >> A >> B >> C;
  cin >> K;

  while (K > 0) {
    if (B > A && C > B) {
      break;
    }

    if (B <= A) {
      B *= 2;
      K--;
    } else if (C <= B) {
      C *= 2;
      K--;
    }
  }

  if (B > A && C > B) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p02601 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C;
  cin >> K;
  int count = 0;
  while (count < K) {
    if (B <= A) {
      B = B * 2;
      count++;
    } else if (count < K && C <= B) {
      C = C * 2;
      count++;
    }
  }
  if (B > A && C > B)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C;
  cin >> K;
  int count = 0;
  while (count < K && !(B > A && C > B)) {
    if (B <= A) {
      B = B * 2;
      count++;
    } else if (count < K && C <= B) {
      C = C * 2;
      count++;
    }
  }
  if (B > A && C > B)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}
Problem p02601 (C++)

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

int main() {
  int A, B, C;
  cin >> A >> B >> C;
  int K;
  cin >> K;
  while ((B <= A) && (K > 0)) {
    B *= 2;
    K--;
  }
  while ((B > A) && (K > 0)) {
    C *= 2;
  }
  if ((C > B) && (B > A)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

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

int main() {
  int A, B, C;
  cin >> A >> B >> C;
  int K;
  cin >> K;
  while ((B <= A) && (K > 0)) {
    B *= 2;
    K--;
  }
  while ((B > A) && (K > 0)) {
    C *= 2;
    K--;
  }
  if ((C > B) && (B > A)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p02601 (C++)

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

using namespace std;

int main() {

  int v[3];

  for (int i = 0; i < 3; ++i)
    cin >> v[i];

  int k;
  cin >> k;

  int p = 1;
  while (k--) {
    if (v[p] <= v[p - 1])
      v[p] *= 2;
    else
      p++, k++;
  }

  if (v[0] < v[1] and v[1] < v[2])
    cout << "Yes\n";
  else
    cout << "No\n";
  return 0;
}


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

using namespace std;

int main() {

  int v[3];

  for (int i = 0; i < 3; ++i)
    cin >> v[i];

  int k;
  cin >> k;

  int p = 1;
  while (k-- and p < 3) {
    if (v[p] <= v[p - 1])
      v[p] *= 2;
    else
      p++, k++;
  }

  if (v[0] < v[1] and v[1] < v[2])
    cout << "Yes\n";
  else
    cout << "No\n";
  return 0;
}
Problem p02601 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

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

  int a, b, c, k;
  cin >> a >> b >> c >> k;
  for (int i = 0; i <= k; ++i) {
    int B = b;
    for (int j = 0; j < i; ++j) {
      B *= 2;
    }
    for (int j = 0; i + j <= k; ++j) {
      int C = c;
      for (int x = 0; x < j; ++x) {
        C *= 2;
      }
      if (a < B && B < C) {
        cout << "Yes\n";
        return 9;
      }
    }
  }
  cout << "No\n";
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

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

  int a, b, c, k;
  cin >> a >> b >> c >> k;
  for (int i = 0; i <= k; ++i) {
    int B = b;
    for (int j = 0; j < i; ++j) {
      B *= 2;
    }
    for (int j = 0; i + j <= k; ++j) {
      int C = c;
      for (int x = 0; x < j; ++x) {
        C *= 2;
      }
      if (a < B && B < C) {
        cout << "Yes\n";
        return 0;
      }
    }
  }
  cout << "No\n";
}