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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

class P {
public:
  ll point;
  int id;

  P() {}
  P(ll point, int id) : point(point), id(id) {}

  bool operator<(const P &p) const {
    if (point != p.point) {
      return point < p.point;
    } else {
      return id > p.id;
    }
  }
};

class BIT {
public:
  int n;
  vector<int> b;

  BIT(int n) : n(n), b(n, 0) {}

  int sum(int i) {
    int s = 0;
    while (i > 0) {
      s += b[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, int x) {
    while (i <= n) {
      b[i] += x;
      i += i & -i;
    }
  }

  int lower_bound(int rnk) {
    int lb = 1, ub = n;
    while (ub - lb > 0) {
      int mid = (lb + ub) / 2;
      if (sum(mid) <= rnk) {
        lb = mid + 1;
      } else {
        ub = mid;
      }
    }
    return lb;
  }
};

int main() {
  int N, C;

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

  vector<ll> point(N, 1);
  vector<int> com(C), a(C), b(C);
  map<P, int> p2r;
  map<int, P> r2p;

  set<P> st;
  for (int i = 0; i < N; i++) {
    st.insert(P(1LL, i));
  }

  for (int i = 0; i < C; i++) {
    scanf("%d", &com[i]);
    if (com[i] == 0) {
      scanf("%d %d", &a[i], &b[i]);
      a[i]--;
      point[a[i]] += b[i];
      st.insert(P(point[a[i]], a[i]));
    } else {
      scanf("%d", &a[i]);
    }
  }

  int n = st.size(), idx = 1;
  for (auto it = st.begin(); it != st.end(); ++it) {
    p2r[*it] = idx;
    r2p[idx++] = *it;
  }

  BIT bit(n + 10);
  for (int i = 0; i < N; i++) {
    bit.add(i + 1, +1);
  }

  fill(point.begin(), point.end(), 1);
  for (int i = 0; i < C; i++) {
    switch (com[i]) {
    case 0: {
      int t = a[i], p = b[i];
      bit.add(p2r[P(point[t], t)], -1);
      point[t] += p;
      bit.add(p2r[P(point[t], t)], +1);
      break;
    }
    case 1: {
      int m = N - a[i];
      P res = r2p[bit.lower_bound(m)];
      printf("%d %lld\\n", res.id + 1, res.point - 1);
      break;
    }
    default: {
      assert(0);
      break;
    }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

class P {
public:
  ll point;
  int id;

  P() {}
  P(ll point, int id) : point(point), id(id) {}

  bool operator<(const P &p) const {
    if (point != p.point) {
      return point < p.point;
    } else {
      return id > p.id;
    }
  }
};

class BIT {
public:
  int n;
  vector<int> b;

  BIT(int n) : n(n + 1), b(n + 1, 0) {}

  int sum(int i) {
    int s = 0;
    while (i > 0) {
      s += b[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, int x) {
    while (i <= n) {
      b[i] += x;
      i += i & -i;
    }
  }

  int lower_bound(int rnk) {
    int lb = 1, ub = n;
    while (ub - lb > 0) {
      int mid = (lb + ub) / 2;
      if (sum(mid) <= rnk) {
        lb = mid + 1;
      } else {
        ub = mid;
      }
    }
    return lb;
  }
};

int main() {
  int N, C;

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

  vector<ll> point(N, 1);
  vector<int> com(C), a(C), b(C);
  map<P, int> p2r;
  map<int, P> r2p;

  set<P> st;
  for (int i = 0; i < N; i++) {
    st.insert(P(1LL, i));
  }

  for (int i = 0; i < C; i++) {
    scanf("%d", &com[i]);
    if (com[i] == 0) {
      scanf("%d %d", &a[i], &b[i]);
      a[i]--;
      point[a[i]] += b[i];
      st.insert(P(point[a[i]], a[i]));
    } else {
      scanf("%d", &a[i]);
    }
  }

  int n = st.size(), idx = 1;
  set<P>::iterator it;
  for (it = st.begin(); it != st.end(); ++it) {
    p2r[*it] = idx;
    r2p[idx++] = *it;
  }

  BIT bit(n);
  for (int i = 0; i < N; i++) {
    bit.add(i + 1, +1);
  }

  fill(point.begin(), point.end(), 1);
  for (int i = 0; i < C; i++) {
    switch (com[i]) {
    case 0: {
      int t = a[i], p = b[i];
      bit.add(p2r[P(point[t], t)], -1);
      point[t] += p;
      bit.add(p2r[P(point[t], t)], +1);
      break;
    }
    case 1: {
      int m = N - a[i];
      P res = r2p[bit.lower_bound(m)];
      printf("%d %lld\\n", res.id + 1, res.point - 1);
      break;
    }
    default: {
      assert(0);
      break;
    }
    }
  }
  return 0;
}