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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct segtree {
  vector<long long> dat;
  int n;

  segtree(int n_) {
    n = 1;

    while (n < n_)
      n *= 2;

    dat.resize(2 * n, 0);
  }

  void update(int i, int x) {
    i += n - 1;
    int diff = x - dat[i];
    dat[i] = x;

    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] += diff;
    }
  }

  long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = n;

    if (r <= a || l >= b)
      return 0;

    if (a <= l && r <= b) {
      return dat[k];
    }

    long long lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long rv = query(a, b, k * 2 + 2, (l + r) / 2, r);

    return lv + rv;
  }
};

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

  int N;
  cin >> N;
  vector<int> a(N), b(N);

  segtree P(N), Q(N);
  vector<pair<int, int>> A, B;

  for (int i = 0; i < N; ++i) {
    cin >> a[i] >> b[i];
    A.push_back(make_pair(a[i], b[i]));
  }

  sort(begin(A), end(A));

  for (int i = 0; i < N; ++i) {
    B.push_back(make_pair(A[i].second, i));
  }

  sort(begin(B), end(B));

  for (int i = 0; i < N; ++i) {
    P.update(i, A[i].first);
    Q.update(i, 1);
  }

  long long ma = 0;

  for (pair<int, int> &d : B) {
    int l = 0, r = N;

    long long PP, QQ;

    for (int j = 0; j < 20; ++j) {
      int mid = (l + r) / 2;

      PP = P.query(0, mid + 1);
      QQ = Q.query(0, mid + 1);

      if (PP <= d.first * QQ) {
        l = mid;
      } else {
        r = mid;
      }
    }

    if (PP <= d.first * QQ) {
      ma = max(ma, QQ);
    }

    P.update(d.second, 0);
    Q.update(d.second, 0);
  }

  cout << ma << endl;
}

Error: 1

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct segtree {
  vector<long long> dat;
  int n;

  segtree(int n_) {
    n = 1;

    while (n < n_)
      n *= 2;

    dat.resize(2 * n, 0);
  }

  void update(int i, int x) {
    i += n - 1;
    int diff = x - dat[i];
    dat[i] = x;

    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] += diff;
    }
  }

  long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = n;

    if (r <= a || l >= b)
      return 0;

    if (a <= l && r <= b) {
      return dat[k];
    }

    long long lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long rv = query(a, b, k * 2 + 2, (l + r) / 2, r);

    return lv + rv;
  }
};

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

  int N;
  cin >> N;
  vector<int> a(N), b(N);

  segtree P(N), Q(N);
  vector<pair<int, int>> A, B;

  for (int i = 0; i < N; ++i) {
    cin >> a[i] >> b[i];
    A.push_back(make_pair(a[i], b[i]));
  }

  sort(begin(A), end(A));

  for (int i = 0; i < N; ++i) {
    B.push_back(make_pair(A[i].second, i));
  }

  sort(begin(B), end(B));

  for (int i = 0; i < N; ++i) {
    P.update(i, A[i].first);
    Q.update(i, 1);
  }

  long long ma = 0;

  for (pair<int, int> &d : B) {
    int l = 0, r = N;

    long long PP, QQ;

    for (int j = 0; j < 20; ++j) {
      int mid = (l + r) / 2;

      PP = P.query(0, mid + 1);
      QQ = Q.query(0, mid + 1);

      if (PP <= d.first * QQ) {
        l = mid;
      } else {
        r = mid;
      }
    }

    if (QQ == 0) {
      return 1;
    }

    if (PP <= d.first * QQ) {
      ma = max(ma, QQ);
    }

    P.update(d.second, 0);
    Q.update(d.second, 0);
  }

  cout << ma << endl;
}