Problem p02788 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdalign>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
// #include <conio.h>
// #include <windows.h>
using namespace std;

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) ||    \
    (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)

const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repa(i, a, n) for (int i = ((int)(a)); i < ((int)(n)); i++)
#define repa1(i, a, n) for (int i = ((int)(a)); i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repda(i, n, a) for (int i = ((int)(n)); i > ((int)(a)); i--)
#define repda1(i, n, a) for (int i = ((int)(n)); i >= ((int)(a)); i--)
#define FOR(i, a, n, step)                                                     \
  for (int i = ((int)(a)); i < ((int)(n)); i += ((int)(step)))
#define repv(itr, v)                                                           \
  for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define sqr(x) ((x) * (x))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x)                                                              \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define file(filename, name) fstream name(filename, ios::in | ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x = getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf

#define j0 J0
#define j1 J1
#define jn Jn
#define y0 Y0
#define y1 Y1
#define yn Yn

inline int Read() {
  int x = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return w ? -x : x;
}
inline void Write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

inline LL powmod(LL a, LL b) {
  LL res = 1;
  a %= MOD;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res % MOD;
}
inline LL gcdll(LL a, LL b) { return b ? gcdll(b, a % b) : a; }
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};
/************************************************************BEGIN************************************************************/
#define int LL

const int maxn = 200010;

int n, d, a, x[maxn], h[maxn], mon[maxn], bit[maxn], ans;

inline void add(int u, int v) {
  while (u <= n) {
    bit[u] += v;
    u += u & -u;
  }
}

inline int sum(int u) {
  int res = 0;
  while (u) {
    res += bit[u];
    u -= u & -u;
  }

  return res;
}

inline bool cmp(int u, int v) { return x[u] < x[v]; }

signed main() {
  SF("%lld%lld%lld", &n, &d, &a);
  rep1(i, n) {
    SF("%lld%lld", &x[i], &h[i]);
    mon[i] = i;
  }
  mon[n + 1] = n + 1;
  x[n + 1] = INF;

  sort(mon + 1, mon + n + 1, cmp);

  rep1(i, n) {
    rep1(j, n) cerr << sum(j) - sum(j - 1) << ' ';
    cerr << endl;

    int hp = h[mon[i]] + sum(i);

    if (hp > 0) {
      int att = (hp + a - 1) / a;
      ans += att;
      att *= a;

      int l = i + 1, r = n + 1; // [l,r)
      while (l < r) {
        int m = (l + r) / 2;
        if (x[mon[m]] > x[mon[i]] + 2 * d)
          r = m;
        else
          l = m + 1;
      }

      add(i, -att);
      add(r, att);
    }
  }

  PF("%lld", ans);

  return 0;
}
/*************************************************************END**************************************************************/

Fixed submission:
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdalign>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
// #include <conio.h>
// #include <windows.h>
using namespace std;

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) ||    \
    (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)

const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repa(i, a, n) for (int i = ((int)(a)); i < ((int)(n)); i++)
#define repa1(i, a, n) for (int i = ((int)(a)); i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repda(i, n, a) for (int i = ((int)(n)); i > ((int)(a)); i--)
#define repda1(i, n, a) for (int i = ((int)(n)); i >= ((int)(a)); i--)
#define FOR(i, a, n, step)                                                     \
  for (int i = ((int)(a)); i < ((int)(n)); i += ((int)(step)))
#define repv(itr, v)                                                           \
  for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define sqr(x) ((x) * (x))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x)                                                              \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define file(filename, name) fstream name(filename, ios::in | ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x = getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf

#define j0 J0
#define j1 J1
#define jn Jn
#define y0 Y0
#define y1 Y1
#define yn Yn

inline int Read() {
  int x = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return w ? -x : x;
}
inline void Write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

inline LL powmod(LL a, LL b) {
  LL res = 1;
  a %= MOD;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res % MOD;
}
inline LL gcdll(LL a, LL b) { return b ? gcdll(b, a % b) : a; }
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};
/************************************************************BEGIN************************************************************/
#define int LL

const int maxn = 200010;

int n, d, a, x[maxn], h[maxn], mon[maxn], bit[maxn], ans;

inline void add(int u, int v) {
  while (u <= n) {
    bit[u] += v;
    u += u & -u;
  }
}

inline int sum(int u) {
  int res = 0;
  while (u) {
    res += bit[u];
    u -= u & -u;
  }

  return res;
}

inline bool cmp(int u, int v) { return x[u] < x[v]; }

signed main() {
  SF("%lld%lld%lld", &n, &d, &a);
  rep1(i, n) {
    SF("%lld%lld", &x[i], &h[i]);
    mon[i] = i;
  }
  mon[n + 1] = n + 1;
  x[n + 1] = INF;

  sort(mon + 1, mon + n + 1, cmp);

  rep1(i, n) {
    int hp = h[mon[i]] + sum(i);

    if (hp > 0) {
      int att = (hp + a - 1) / a;
      ans += att;
      att *= a;

      int l = i + 1, r = n + 1; // [l,r)
      while (l < r) {
        int m = (l + r) / 2;
        if (x[mon[m]] > x[mon[i]] + 2 * d)
          r = m;
        else
          l = m + 1;
      }

      add(i, -att);
      add(r, att);
    }
  }

  PF("%lld", ans);

  return 0;
}
/*************************************************************END**************************************************************/