Problem p02788 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e9
#define mod (ll)(1e9 + 7)
#define d(x) cerr << #x << "=" << x << endl;
#define p(x) cout << (x) << endl
#define ps(x) cout << (x);
#define pk(x) cout << (x) << " ";
#define pfix(d, x) cout << fixed << setprecision(d) << x << endl;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define minel(v) *min_element(all(v))
#define minind(v) distance((v).begin(), min_element(all(v)))
#define maxel(v) *max_element(all(v))
#define maxind(v) distance((v).begin(), max_element(all(v)))
#define p2d(v, n, m)                                                           \
  rep(i, n) {                                                                  \
    rep(j, m) { pk(v[i][j]); }                                                 \
    p("");                                                                     \
  }
#define fi first
#define se second
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
ll a[100001], b[100001], c[100001], d, n, m, x, y, z, h, w, k, sum,
    mi = inf, ma = -inf, ans, s, t, l, r, res, tmp, itr, p, q;
bool flag;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
char cha;
string st;
typedef map<ll, ll> imap;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pair<ll, ll>> VP;
P pa;

ll damages[200002];

int main() {
  cin >> n >> d >> m;
  VP vp;
  rep(i, n) {
    cin >> x >> h;
    vp.pb({x, h});
  }
  sort(all(vp));
  // 尺取り法でx_iが左端の場合の攻撃範囲を求める
  ll bombnum, nowhp, damage;
  rep(i, n) {
    rep(j, r + 1, n) {
      if (vp[j].fi - vp[i].fi <= 2 * d)
        r++;
    }
    // iからはrまで届く。
    nowhp = vp[i].se - damages[i];
    if (nowhp > 0) {
      bombnum = ((nowhp % m) ? nowhp / m + 1 : nowhp / m);
      sum += bombnum;
      damage = bombnum * m;
      damages[i] += damage;
      damages[r + 1] -= damage;
    }
    damages[i + 1] += damages[i];
    // rep(i,n+1)d(damages[i]);
  }
  p(sum);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e9
#define mod (ll)(1e9 + 7)
#define d(x) cerr << #x << "=" << x << endl;
#define p(x) cout << (x) << endl
#define ps(x) cout << (x);
#define pk(x) cout << (x) << " ";
#define pfix(d, x) cout << fixed << setprecision(d) << x << endl;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define minel(v) *min_element(all(v))
#define minind(v) distance((v).begin(), min_element(all(v)))
#define maxel(v) *max_element(all(v))
#define maxind(v) distance((v).begin(), max_element(all(v)))
#define p2d(v, n, m)                                                           \
  rep(i, n) {                                                                  \
    rep(j, m) { pk(v[i][j]); }                                                 \
    p("");                                                                     \
  }
#define fi first
#define se second
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
ll a[100001], b[100001], c[100001], d, n, m, x, y, z, h, w, k, sum,
    mi = inf, ma = -inf, ans, s, t, l, r, res, tmp, itr, p, q;
bool flag;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
char cha;
string st;
typedef map<ll, ll> imap;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pair<ll, ll>> VP;
P pa;

ll damages[200002];

int main() {
  cin >> n >> d >> m;
  VP vp;
  rep(i, n) {
    cin >> x >> h;
    vp.pb({x, h});
  }
  sort(all(vp));
  // 尺取り法でx_iが左端の場合の攻撃範囲を求める
  ll bombnum, nowhp, damage;
  rep(i, n) {
    rep(j, r + 1, n) {
      if (vp[j].fi - vp[i].fi <= 2 * d)
        r++;
      else
        break;
    }
    // iからはrまで届く。
    nowhp = vp[i].se - damages[i];
    if (nowhp > 0) {
      bombnum = ((nowhp % m) ? nowhp / m + 1 : nowhp / m);
      sum += bombnum;
      damage = bombnum * m;
      damages[i] += damage;
      damages[r + 1] -= damage;
    }
    damages[i + 1] += damages[i];
    // rep(i,n+1)d(damages[i]);
  }
  p(sum);
  return 0;
}