Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

// 入力
ll n, Q;
// BIT
ll bit0[100001], bit1[100001];

ll sum(ll *b, int i) {
  ll s = 0;
  while (i > 0) {
    s += b[i];
    i -= i & -i;
  }
  return s;
}
void add(ll *b, int i, ll v) {
  while (i <= n) {
    b[i] += v;
    i += i & -i;
  }
}

int main() {
  ll d, a;
  cin >> n >> d >> a;
  vector<P> A(n + 1);
  vector<ll> B(n + 1);
  for (int i = 1; i <= n; i++) {
    ll x, y;
    cin >> x >> y;
    A[i] = mp(x, y);
    B[i] = x;
  }
  sort(A.begin() + 1, A.end());
  sort(B.begin() + 1, B.end());

  // for(int i = 1; i <= n; i++) {
  // 	cout << A[i].sc << endl;
  // }

  for (int i = 1; i <= n; i++) {
    add(bit0, i, A[i].sc);
  }

  ll count = 0LL;
  for (int i = 1; i <= n; i++) {
    ll res = 0;
    res += sum(bit0, i) + sum(bit1, i) * i;
    res -= sum(bit0, i - 1) + sum(bit1, i - 1) * (i - 1);
    // cout << res << endl;

    if (res > 0) { // 爆撃(i ~ i + 2D)
      ll bomb = (res + a - 1) / a;
      count += bomb;
      auto index = upper_bound(B.begin() + 1, B.end(), B[i] + 2 * d);
      ll r = index - B.begin() - 1;
      // cout << "r" << r << endl;
      add(bit0, i, bomb * a * (i - 1));
      add(bit1, i, -1 * bomb * a);
      add(bit0, r + 1, -1 * bomb * a * r);
      add(bit1, r + 1, bomb * a);
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

// 入力
ll n, Q;
// BIT
ll bit0[200001], bit1[200001];

ll sum(ll *b, int i) {
  ll s = 0;
  while (i > 0) {
    s += b[i];
    i -= i & -i;
  }
  return s;
}
void add(ll *b, int i, ll v) {
  while (i <= n) {
    b[i] += v;
    i += i & -i;
  }
}

int main() {
  ll d, a;
  cin >> n >> d >> a;
  vector<P> A(n + 1);
  vector<ll> B(n + 1);
  for (int i = 1; i <= n; i++) {
    ll x, y;
    cin >> x >> y;
    A[i] = mp(x, y);
    B[i] = x;
  }
  sort(A.begin() + 1, A.end());
  sort(B.begin() + 1, B.end());

  // for(int i = 1; i <= n; i++) {
  // 	cout << A[i].sc << endl;
  // }

  for (int i = 1; i <= n; i++) {
    add(bit0, i, A[i].sc);
  }

  ll count = 0LL;
  for (int i = 1; i <= n; i++) {
    ll res = 0;
    res += sum(bit0, i) + sum(bit1, i) * i;
    res -= sum(bit0, i - 1) + sum(bit1, i - 1) * (i - 1);
    // cout << res << endl;

    if (res > 0) { // 爆撃(i ~ i + 2D)
      ll bomb = (res + a - 1) / a;
      count += bomb;
      auto index = upper_bound(B.begin() + 1, B.end(), B[i] + 2 * d);
      ll r = index - B.begin() - 1;
      // cout << "r" << r << endl;
      add(bit0, i, bomb * a * (i - 1));
      add(bit1, i, -1 * bomb * a);
      add(bit0, r + 1, -1 * bomb * a * r);
      add(bit1, r + 1, bomb * a);
    }
  }
  cout << count << endl;
  return 0;
}