Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)

using ll = long long;
using P = pair<int, int>;

const long long MOD = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct Bit {
  vector<ll> bit[2];
  int N;
  void init(int n) {
    for (int i = 0; i < 2; i++)
      bit[i].assign(n + 1, 0);
    N = n + 1;
  }

  ll sub_sum(int p, int i) {
    ll s = 0;
    while (0 < i) {
      s += bit[p].at(i);
      i -= i & -i;
    }
    return s;
  }
  // 1-indexed
  ll sum(int i) { return sub_sum(0, i) + sub_sum(1, i) * i; }
  // [a, b]
  ll sum(int a, int b) { return sum(b) - sum(a - 1); }

  void sub_add(int p, int i, ll v) {
    while (i <= N) {
      bit[p].at(i) += v;
      i += i & -i;
    }
  }
  //[a, b] 1-indexed
  void add(int a, int b, ll v) {
    sub_add(0, a, -v * (a - 1));
    sub_add(1, a, v);
    sub_add(0, b + 1, v * b);
    sub_add(1, b + 1, -v);
  }
};

int main() {
  int n;
  ll d, a;
  cin >> n >> d >> a;
  vector<pair<ll, ll>> m(n);
  for (int i = 0; i < n; i++) {
    int x, h;
    cin >> x >> h;
    m.at(i) = make_pair(x, h);
  }
  sort(m.begin(), m.end());

  auto binary_search = [&](ll x) {
    int ng = n;
    int ok = -1;

    while (abs(ok - ng) > 1) {
      int mid = (ok + ng) / 2;

      if (m.at(mid).first <= x)
        ok = mid;
      else
        ng = mid;
    }
    return ok;
  };

  Bit bit;
  bit.init(n + 1);
  for (int i = 0; i < n; i++) {
    bit.add(i + 1, i + 1, m.at(i).second);
  }

  ll cnt = 0;
  for (int l = 0; l < n; l++) {
    ll h = bit.sum(l + 1, l + 1);
    if (h <= 0)
      continue;

    int r = binary_search(m.at(l).first + d * 2);
    ll c = (h + a - 1) / a;
    cnt += c;
    bit.add(l + 1, r + 1, c * -a);
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)

using ll = long long;
using P = pair<int, int>;

const long long MOD = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct Bit {
  vector<ll> bit[2];
  int N;
  void init(int n) {
    for (int i = 0; i < 2; i++)
      bit[i].assign(n + 2, 0);
    N = n + 1;
  }

  ll sub_sum(int p, int i) {
    ll s = 0;
    while (0 < i) {
      s += bit[p].at(i);
      i -= i & -i;
    }
    return s;
  }
  // 1-indexed
  ll sum(int i) { return sub_sum(0, i) + sub_sum(1, i) * i; }
  // [a, b]
  ll sum(int a, int b) { return sum(b) - sum(a - 1); }

  void sub_add(int p, int i, ll v) {
    while (i <= N) {
      bit[p].at(i) += v;
      i += i & -i;
    }
  }
  //[a, b] 1-indexed
  void add(int a, int b, ll v) {
    sub_add(0, a, -v * (a - 1));
    sub_add(1, a, v);
    sub_add(0, b + 1, v * b);
    sub_add(1, b + 1, -v);
  }
};

int main() {
  int n;
  ll d, a;
  cin >> n >> d >> a;
  vector<pair<ll, ll>> m(n);
  for (int i = 0; i < n; i++) {
    int x, h;
    cin >> x >> h;
    m.at(i) = make_pair(x, h);
  }
  sort(m.begin(), m.end());

  auto binary_search = [&](ll x) {
    int ng = n;
    int ok = -1;

    while (abs(ok - ng) > 1) {
      int mid = (ok + ng) / 2;

      if (m.at(mid).first <= x)
        ok = mid;
      else
        ng = mid;
    }
    return ok;
  };

  Bit bit;
  bit.init(n + 1);
  for (int i = 0; i < n; i++) {
    bit.add(i + 1, i + 1, m.at(i).second);
  }

  ll cnt = 0;
  for (int l = 0; l < n; l++) {
    ll h = bit.sum(l + 1, l + 1);
    if (h <= 0)
      continue;

    int r = binary_search(m.at(l).first + d * 2);
    ll c = (h + a - 1) / a;
    cnt += c;
    bit.add(l + 1, r + 1, c * -a);
  }
  cout << cnt << endl;
}