Problem:
Score : 600 points

Problem Statement

Silver Fox is fighting with N monsters.

The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.

Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.

Silver Fox wins when all the monsters' healths become 0 or below.

Find the minimum number of bombs needed to win.

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq D \leq 10^9

1 \leq A \leq 10^9

0 \leq X_i \leq 10^9

1 \leq H_i \leq 10^9

X_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D A
X_1 H_1
:
X_N H_N

Output

Print the minimum number of bombs needed to win.

Sample Input 1

3 3 2
1 2
5 4
9 2

Sample Output 1

2

First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.

Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.

Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.

Sample Input 2

9 4 1
1 5
2 4
3 3
4 2
5 1
6 2
7 3
8 4
9 5

Sample Output 2

5

We should use five bombs at the coordinate 5.

Sample Input 3

3 0 1
300000000 1000000000
100000000 1000000000
200000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;

template<typename T, typename E>
struct SegTree {
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  typedef function<E(E, E)> H;
  typedef function<E(E, int)> P;
  int n;
  T td;
  E ed;
  F f;
  G g;
  H h;
  P p;
  vector<T> dat;
  vector<E> laz;
  SegTree(T td, E ed, F f, G g, H h, P p=[](E a, int b){return a*b;})
      : td(td), ed(ed), f(f), g(g), h(h), p(p) {}
  void init(int _n) {
    n = 1;
    while (n < _n) n *= 2;
    dat = vector<T>(2*n-1, td);
    laz = vector<E>(2*n-1, ed);
  }
  void build(vector<T> v) {
    int _n = v.size();
    init(_n);
    for (int i=0; i<_n; i++) dat[i+n-1] = v[i];
    for (int i=n-2; i>=0; i--)
      dat[i] = f(dat[2*i+1], dat[2*i+2]);
  }
  void eval(int len, int k) {
    if (laz[k] == ed) return;
    if (k*2+1 < n*2-1) {
      laz[2*k+1] = h(laz[2*k+1], laz[k]);
      laz[2*k+2] = h(laz[2*k+2], laz[k]);
    }
    dat[k] = g(dat[k], p(laz[k], len));
    laz[k] = ed;
  }
  T _update(int a, int b, E x, int k, int l, int r) {
    eval(r-l, k);
    if (b <= l || r <= a) return dat[k];
    if (a <= l && r <= b) {
      laz[k] = h(laz[k], x);
      return g(dat[k], p(laz[k], r-l));
    }
    T vl = _update(a, b, x, 2*k+1, l, (l+r)/2);
    T vr = _update(a, b, x, 2*k+2, (l+r)/2, r);
    return dat[k] = f(vl, vr);
  }
  T update(int a, int b, E x) { return _update(a, b, x, 0, 0, n); }
  T _query(int a, int b, int k, int l, int r) {
    eval(r-l, k);
    if (r <= a || b <= l) return td;
    if (a <= l && r <= b) return dat[k];
    T vl = _query(a, b, 2*k+1, l, (l+r)/2);
    T vr = _query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
};

int main()
{
  ll N; cin >> N;
  ll D; cin >> D;
  ll A; cin >> A;
  vector<ii> f;
  for (int i=0; i<N; i++){
    ll x, h; cin >> x >> h;
    f.push_back(make_pair(x, h));
  }
  sort(f.begin(),f.end());

  ll d = 2*D;
  vector<ll> X(N, 0);
  vector<ll> H(N, 0);
  for (int i=0; i<N; i++) {
    X[i] = f[i].first;
    H[i] = f[i].second;
  }
  SegTree<ll, ll> st(0, 0,
        [](ll a, ll b){return a+b;},
        [](ll a, ll b){return a+b;},
        [](ll a, ll b){return a+b;});
  st.build(H);

  int l = 0;
  ll ans = 0;
  while (l < N) {
    ll h = st.query(l, l+1);
    if (h < 1) {l++; continue;}
    int r = upper_bound(X.begin(), X.end(), X[l]+d) - X.begin();
    ll c = (h+A-1)/A;
    ans += c;
    st.update(l, r, -A*c);
  }
  cout << ans << endl;

  return 0;
}