Problem p00668 (C++) — buggy submission, verdict: Runtime Error

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

template <class T> struct BIT {
  int n;
  vector<T> x;
  BIT(int n_ = 0) : n(n_), x(n + 1, 0) {}
  int size() const { return n; }
  T sum(int r) const {
    T S = 0;
    for (r = r - 1; r >= 0; r = (r & (r + 1)) - 1)
      S += x[r];
    return S;
  }
  T sum(int l, int r) const { return sum(r) - sum(l); }
  void add(int k, const T &a) {
    for (; k < n; k |= k + 1)
      x[k] += a;
  }
  void set(int k, const T &a) { add(k, a - sum(k, k + 1)); }
};

typedef pair<int, int> P;

int x[500000];
int q, lim;

int f(BIT<int> &b, int x) {
  int ng = -1, ok = q - 1;
  while (abs(ng - ok) > 1) {
    int mid = (ng + ok) / 2;
    if (b.sum(0, mid + 1) >= x)
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

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

  while (cin >> q >> lim, q | lim) {
    BIT<int> b(q + 10);
    set<P> s;
    map<int, int> tmap;
    for (int i = 0; i < q; i++) {
      int type;
      cin >> type >> x[i];
      // cerr << "type : " << type << ", x : " << x[i] << endl;
      if (type == 0) {
        if (s.size() == lim) {
          b.add(s.begin()->first, -1);
          tmap.erase(s.begin()->second);
          s.erase(s.begin());
        }
        b.add(i, 1);
        tmap[x[i]] = i;
        s.insert({i, x[i]});
      } else if (type == 1) {
        int t = f(b, x[i]);
        // assert(b.sum(t, t + 1) == 1);
        b.add(t, -1);
        s.erase({t, x[t]});
        tmap.erase(x[t]);
      } else if (type == 2) {
        int t = f(b, x[i]);
        // assert(b.sum(t, t + 1) == 1);
        cout << x[t] << endl;
      } else {
        b.add(tmap[x[i]], -1);
        s.erase({tmap[x[i]], x[i]});
        tmap.erase(x[i]);
      }
    }
    cout << "end" << endl;
  }
}

Error: 0

Fixed submission:

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

template <class T> struct BIT {
  int n;
  vector<T> x;
  BIT(int n_ = 0) : n(n_), x(n + 1, 0) {}
  int size() const { return n; }
  T sum(int r) const {
    T S = 0;
    for (r = r - 1; r >= 0; r = (r & (r + 1)) - 1)
      S += x[r];
    return S;
  }
  T sum(int l, int r) const { return sum(r) - sum(l); }
  void add(int k, const T &a) {
    for (; k < n; k |= k + 1)
      x[k] += a;
  }
  void set(int k, const T &a) { add(k, a - sum(k, k + 1)); }
};

typedef pair<int, int> P;

int x[400000];
int q, lim;

int f(BIT<int> &b, int x) {
  int ng = -1, ok = q - 1;
  while (abs(ng - ok) > 1) {
    int mid = (ng + ok) / 2;
    if (b.sum(0, mid + 1) >= x)
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

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

  while (cin >> q >> lim, q | lim) {
    BIT<int> b(q);
    set<P> s;
    map<int, int> tmap;
    for (int i = 0; i < q; i++) {
      int type;
      cin >> type >> x[i];
      // cerr << "type : " << type << ", x : " << x[i] << endl;
      if (type == 0) {
        if (s.size() == lim) {
          b.add(s.begin()->first, -1);
          tmap.erase(s.begin()->first);
          s.erase(s.begin());
        }
        b.add(i, 1);
        tmap[x[i]] = i;
        s.insert({i, x[i]});
      } else if (type == 1) {
        int t = f(b, x[i]);
        assert(b.sum(t, t + 1) == 1);
        b.add(t, -1);
        s.erase({t, x[t]});
        tmap.erase(x[t]);
      } else if (type == 2) {
        int t = f(b, x[i]);
        assert(b.sum(t, t + 1) == 1);
        cout << x[t] << endl;
      } else {
        b.add(tmap[x[i]], -1);
        s.erase({tmap[x[i]], x[i]});
        tmap.erase(x[i]);
      }
    }
    cout << "end" << endl;
  }
}