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

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

using ll = long long;

template <typename T, unsigned int B = 32> class trie {
public:
  struct node {
    std::unique_ptr<node> l, r;
    int cnt;
    node(std::unique_ptr<node> l_ = nullptr, std::unique_ptr<node> r_ = nullptr)
        : l(std::move(l_)), r(std::move(r_)), cnt(0) {}
  };

public:
  trie() : root(nullptr) {}

  void insert(T val) {
    // when you do not want multiset but set, insert below:
    // if(find(val)) return;
    insert(root, val, B - 1);
  }

  void erase(T val) { erase(root, val, B - 1); }

  bool find(T val) const {
    node *t = root.get();
    for (int i = B - 1; i >= 0; --i) {
      if (val & ((T)1 << i)) {
        if (!t->r) {
          return false;
        }
        t = t->r.get();
      } else {
        if (!t->l) {
          return false;
        }
        t = t->l.get();
      }
    }
    return true;
  }

  int size() const { return count(root); }

  T get(int k) const {
    assert(0 <= k && k < count(root));
    return get(root, k, 0, B - 1);
  }

private:
  int count(std::unique_ptr<node> const &t) const { return t ? t->cnt : 0; }
  T get(std::unique_ptr<node> const &t, int k, T tmp, int b) const {
    if (b < 0) {
      return tmp;
    }
    assert(t);
    int c = count(t->l);
    if (k < c) {
      return get(t->l, k, tmp, b - 1);
    } else {
      return get(t->r, k - c, tmp | ((T)1 << b), b - 1);
    }
  }
  void insert(std::unique_ptr<node> &t, T val, int b) {
    if (!t) {
      t = std::make_unique<node>();
    }
    t->cnt++;
    if (b >= 0) {
      if (val & ((T)1 << b)) {
        insert(t->r, val, b - 1);
      } else {
        insert(t->l, val, b - 1);
      }
    }
  }
  void erase(std::unique_ptr<node> &t, T val, int b) {
    assert(t);
    t->cnt--;
    if (b >= 0) {
      if (val & ((T)1 << b)) {
        erase(t->r, val, b - 1);
      } else {
        erase(t->l, val, b - 1);
      }
    }
    if (t->cnt == 0) {
      t.release();
    }
  }

private:
  std::unique_ptr<node> root;
};

// 0-indexed
template <typename T> class fenwick_tree {
public:
  fenwick_tree(int n) : n(n), dat(n, 0) {}

  void add(int i, T value) {
    for (; i < n; i |= i + 1) {
      dat[i] += value;
    }
  }

  T sum(int i) const {
    T res = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1) {
      res += dat[i];
    }
    return res;
  }
  // [l, r)
  T sum(int l, int r) const { return sum(r - 1) - sum(l - 1); }

private:
  const int n;
  std::vector<T> dat;
};

int main() {
  int n, c;
  cin >> n >> c;
  vector<int> t(c, -1), p(c, -1), m(c, -1);
  vector<ll> xs = {0};
  vector<ll> score(n);
  for (int i = 0; i < c; ++i) {
    int type;
    cin >> type;
    if (type == 0) {
      cin >> t[i] >> p[i];
      t[i]--;
      score[t[i]] += p[i];
      xs.push_back(score[t[i]]);
    } else {
      cin >> m[i];
    }
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  auto idx = [&](ll v) {
    return lower_bound(begin(xs), end(xs), v) - begin(xs);
  };
  score.assign(n, 0);

  fenwick_tree<int> bit(xs.size());
  bit.add(0, n);
  vector<trie<int>> s(xs.size());
  for (int i = 0; i < n; ++i) {
    s[0].insert(i);
  }
  for (int i = 0; i < c; ++i) {
    if (t[i] != -1) {
      int id = idx(score[t[i]]);
      int nid = idx(score[t[i]] += p[i]);
      bit.add(id, -1);
      bit.add(nid, 1);
      s[id].erase(t[i]);
      s[nid].insert(t[i]);
    } else {
      int lb = 0, ub = xs.size();
      while (ub - lb > 1) {
        const int mid = (ub + lb) / 2;
        if (bit.sum(mid, xs.size()) < m[i]) {
          ub = mid;
        } else {
          lb = mid;
        }
      }
      int k = m[i] - bit.sum(ub, xs.size());
      cout << s[lb].get(k - 1) + 1 << ' ' << xs[lb] << endl;
    }
  }
}


Error: 0

Fixed submission:

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

template <typename T, unsigned int B = 32> class trie {
public:
  struct node {
    std::unique_ptr<node> l, r;
    int cnt;
    node(std::unique_ptr<node> l_ = nullptr, std::unique_ptr<node> r_ = nullptr)
        : l(std::move(l_)), r(std::move(r_)), cnt(0) {}
  };

public:
  trie() : root(nullptr) {}

  void insert(T val) {
    // when you do not want multiset but set, insert below:
    // if(find(val)) return;
    insert(root, val, B - 1);
  }

  void erase(T val) { erase(root, val, B - 1); }

  bool find(T val) const {
    node *t = root.get();
    for (int i = B - 1; i >= 0; --i) {
      if (val & ((T)1 << i)) {
        if (!t->r) {
          return false;
        }
        t = t->r.get();
      } else {
        if (!t->l) {
          return false;
        }
        t = t->l.get();
      }
    }
    return true;
  }

  int size() const { return count(root); }

  T get(int k) const {
    assert(0 <= k && k < count(root));
    return get(root, k, 0, B - 1);
  }

private:
  int count(std::unique_ptr<node> const &t) const { return t ? t->cnt : 0; }
  T get(std::unique_ptr<node> const &t, int k, T tmp, int b) const {
    if (b < 0) {
      return tmp;
    }
    assert(t);
    int c = count(t->l);
    if (k < c) {
      return get(t->l, k, tmp, b - 1);
    } else {
      return get(t->r, k - c, tmp | ((T)1 << b), b - 1);
    }
  }
  void insert(std::unique_ptr<node> &t, T val, int b) {
    if (!t) {
      t = std::make_unique<node>();
    }
    t->cnt++;
    if (b >= 0) {
      if (val & ((T)1 << b)) {
        insert(t->r, val, b - 1);
      } else {
        insert(t->l, val, b - 1);
      }
    }
  }
  void erase(std::unique_ptr<node> &t, T val, int b) {
    assert(t);
    t->cnt--;
    if (b >= 0) {
      if (val & ((T)1 << b)) {
        erase(t->r, val, b - 1);
      } else {
        erase(t->l, val, b - 1);
      }
    }
    if (t->cnt == 0) {
      t.release();
    }
  }

private:
  std::unique_ptr<node> root;
};

// 0-indexed
template <typename T> class fenwick_tree {
public:
  fenwick_tree(int n) : n(n), dat(n, 0) {}

  void add(int i, T value) {
    for (; i < n; i |= i + 1) {
      dat[i] += value;
    }
  }

  T sum(int i) const {
    T res = 0;
    for (; i >= 0; i = (i & (i + 1)) - 1) {
      res += dat[i];
    }
    return res;
  }
  // [l, r)
  T sum(int l, int r) const { return sum(r - 1) - sum(l - 1); }

private:
  const int n;
  std::vector<T> dat;
};

int main() {
  int n, c;
  cin >> n >> c;
  vector<int> t(c, -1), p(c, -1), m(c, -1);
  vector<int> xs = {0};
  vector<int> score(n);
  for (int i = 0; i < c; ++i) {
    int type;
    cin >> type;
    if (type == 0) {
      cin >> t[i] >> p[i];
      t[i]--;
      score[t[i]] += p[i];
      xs.push_back(score[t[i]]);
    } else {
      cin >> m[i];
    }
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  auto idx = [&](int v) {
    return lower_bound(begin(xs), end(xs), v) - begin(xs);
  };
  score.assign(n, 0);

  fenwick_tree<int> bit(xs.size());
  bit.add(0, n);
  vector<trie<int>> s(xs.size());
  for (int i = 0; i < n; ++i) {
    s[0].insert(i);
  }
  for (int i = 0; i < c; ++i) {
    if (t[i] != -1) {
      int id = idx(score[t[i]]);
      int nid = idx(score[t[i]] += p[i]);
      bit.add(id, -1);
      bit.add(nid, 1);
      s[id].erase(t[i]);
      s[nid].insert(t[i]);
    } else {
      int lb = 0, ub = xs.size();
      while (ub - lb > 1) {
        const int mid = (ub + lb) / 2;
        if (bit.sum(mid, xs.size()) < m[i]) {
          ub = mid;
        } else {
          lb = mid;
        }
      }
      int k = m[i] - bit.sum(ub, xs.size());
      cout << s[lb].get(k - 1) + 1 << ' ' << xs[lb] << endl;
    }
  }
}