Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, d, a;
  cin >> n >> d >> a;
  vector<pair<int, int>> monsters;
  for (int i = 0; i < n; ++i) {
    int x, h;
    cin >> x >> h;
    h = (h + a - 1) / a; // number of bombs to drop on monster i to kill it
    monsters.emplace_back(x, h);
  }
  sort(monsters.begin(), monsters.end());

  int base = 1;    // n = 5
  while (base < n) // base = 8
    base *= 2;
  vector<int> tree(2 * base, 0);

  for (int i = 0; i < n; ++i)
    tree[base + i] = monsters[i].second;

  auto getval = [&](int i) {
    int sum = 0;
    for (int j = base + i; j >= 1; j /= 2) {
      sum += tree[j];
    }
    return sum;
  };

  auto addInterval = [&](int l, int r, int val) {
    l += base;
    r += base;
    tree[l] += val;
    if (l != r)
      tree[r] += val;

    while (l + 1 < r) {
      if (l % 2 == 0)
        tree[l + 1] += val;
      if (r % 2)
        tree[r - 1] += val;
      l /= 2;
      r /= 2;
    }
  };

  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int health = getval(i);
    if (health <= 0)
      continue;
    // binary search  for the monsters that will be affected by the diameter of
    // detonation of bomb
    int l = i;
    int r = n - 1;
    int ok = -1;
    while (l <= r) {
      int mid = l + ((r - l) >> 1);
      if (monsters[mid].first <= monsters[i].first + 2 * d) {
        ok = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    assert(ok != -1);
    addInterval(i, ok, -health);
    ans += health;
  }
  cout << ans << "\n";
  return 0;
}

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

int main() {
  int n, d, a;
  cin >> n >> d >> a;
  vector<pair<int, int>> monsters;
  for (int i = 0; i < n; ++i) {
    int x, h;
    cin >> x >> h;
    h = (h + a - 1) / a; // number of bombs to drop on monster i to kill it
    monsters.emplace_back(x, h);
  }
  sort(monsters.begin(), monsters.end());

  int base = 1;    // n = 5
  while (base < n) // base = 8
    base *= 2;
  vector<int> tree(2 * base, 0);

  for (int i = 0; i < n; ++i)
    tree[base + i] = monsters[i].second;

  auto getval = [&](int i) {
    int sum = 0;
    for (int j = base + i; j >= 1; j /= 2) {
      sum += tree[j];
    }
    return sum;
  };

  auto addInterval = [&](int l, int r, int val) {
    l += base;
    r += base;
    tree[l] += val;
    if (l != r)
      tree[r] += val;

    while (l + 1 < r) {
      if (l % 2 == 0)
        tree[l + 1] += val;
      if (r % 2)
        tree[r - 1] += val;
      l /= 2;
      r /= 2;
    }
  };

  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int health = getval(i);
    if (health <= 0)
      continue;
    // binary search  for the monsters that will be affected by the diameter of
    // detonation of bomb
    int l = i;
    int r = n - 1;
    int ok = -1;
    while (l <= r) {
      int mid = l + ((r - l) >> 1);
      if (monsters[mid].first <= (long long)monsters[i].first + 2 * d) {
        ok = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    assert(ok != -1);
    addInterval(i, ok, -health);
    ans += health;
  }
  cout << ans << "\n";
  return 0;
}