Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
long long int n, m, j, k, l, t, arr[100009], brr[100009], tree[600009], i, ans,
    d;
vector<pair<ll, ll>> v;
vector<ll> v1, v2;
void update(ll node, ll start, ll end, ll idx, ll val) {
  if (start == end) {
    // Leaf node
    tree[node] += val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx and idx <= mid) {
      // If idx is in the left child, recurse on the left child
      update(2 * node + 1, start, mid, idx, val);
    } else {
      // if idx is in the right child, recurse on the right child
      update(2 * node + 2, mid + 1, end, idx, val);
    }
    // Internal node will have the sum of both of its children
    tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
  }
}
ll query(ll node, ll start, ll end, ll l, ll r) {
  if (r < start or end < l) {
    // range represented by a node is completely outside the given range
    return 0;
  }
  if (l <= start and end <= r) {
    // range represented by a node is completely inside the given range
    return tree[node];
  }
  // range represented by a node is partially inside and partially outside the
  // given range
  ll mid = (start + end) / 2;
  ll p1 = query(2 * node + 1, start, mid, l, r);
  ll p2 = query(2 * node + 2, mid + 1, end, l, r);
  return (p1 + p2);
}
int main() {
  scanf("%lld %lld %lld", &n, &m, &d);
  for (i = 0; i < n; i++) {
    scanf("%lld %lld", &arr[i], &brr[i]);
    v.push_back({arr[i], brr[i]});
  }
  sort(v.begin(), v.end());
  for (i = 0; i < n; i++) {
    v1.push_back(v[i].first);
    v2.push_back(v[i].second);
  }
  for (i = 0; i < n; i++) {
    j = v1[i] - (2 * m);
    t = lower_bound(v1.begin(), v1.end(), j) - v1.begin();
    while (t < 0)
      t = 0;
    while (v1[t] < j)
      t++;
    l = query(0, 0, n - 1, t, i);
    v2[i] -= l;
    if (v2[i] < 0)
      v2[i] = 0;
    j = v2[i] / d;
    if (v2[i] % d)
      j++;
    j *= d;
    update(0, 0, n - 1, i, j);
    j /= d;
    ans += j;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
long long int n, m, j, k, l, t, arr[200009], brr[200009], tree[600009], i, ans,
    d;
vector<pair<ll, ll>> v;
vector<ll> v1, v2;
void update(ll node, ll start, ll end, ll idx, ll val) {
  if (start == end) {
    // Leaf node
    tree[node] += val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx and idx <= mid) {
      // If idx is in the left child, recurse on the left child
      update(2 * node + 1, start, mid, idx, val);
    } else {
      // if idx is in the right child, recurse on the right child
      update(2 * node + 2, mid + 1, end, idx, val);
    }
    // Internal node will have the sum of both of its children
    tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
  }
}
ll query(ll node, ll start, ll end, ll l, ll r) {
  if (r < start or end < l) {
    // range represented by a node is completely outside the given range
    return 0;
  }
  if (l <= start and end <= r) {
    // range represented by a node is completely inside the given range
    return tree[node];
  }
  // range represented by a node is partially inside and partially outside the
  // given range
  ll mid = (start + end) / 2;
  ll p1 = query(2 * node + 1, start, mid, l, r);
  ll p2 = query(2 * node + 2, mid + 1, end, l, r);
  return (p1 + p2);
}
int main() {
  scanf("%lld %lld %lld", &n, &m, &d);
  for (i = 0; i < n; i++) {
    scanf("%lld %lld", &arr[i], &brr[i]);
    v.push_back({arr[i], brr[i]});
  }
  sort(v.begin(), v.end());
  for (i = 0; i < n; i++) {
    v1.push_back(v[i].first);
    v2.push_back(v[i].second);
  }
  for (i = 0; i < n; i++) {
    j = v1[i] - (2 * m);
    t = lower_bound(v1.begin(), v1.end(), j) - v1.begin();
    while (t < 0)
      t = 0;
    while (v1[t] < j)
      t++;
    l = query(0, 0, n - 1, t, i);
    v2[i] -= l;
    if (v2[i] < 0)
      v2[i] = 0;
    j = v2[i] / d;
    if (v2[i] % d)
      j++;
    j *= d;
    update(0, 0, n - 1, i, j);
    j /= d;
    ans += j;
  }
  printf("%lld\n", ans);
}