Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n, d, a;
P p[200000];
int ans;
int cnt;
queue<P> que;

signed main() {
  cin >> n >> d >> a;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    p[i].second = (p[i].second - 1) / a + 1;
  }
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    if (que.size()) {
      while (1) {
        P x = que.front();
        if (x.first < p[i].first) {
          cnt -= x.second;
          que.pop();
        } else
          break;
      }
    }
    if (cnt < p[i].second) {
      ans += p[i].second - cnt;
      que.push({p[i].first + d * 2, p[i].second - cnt});
      cnt += p[i].second - cnt;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n, d, a;
P p[200000];
int ans;
int cnt;
queue<P> que;

signed main() {
  cin >> n >> d >> a;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    p[i].second = (p[i].second - 1) / a + 1;
  }
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    while (que.size()) {
      P x = que.front();
      if (x.first < p[i].first) {
        cnt -= x.second;
        que.pop();
      } else
        break;
    }
    if (cnt < p[i].second) {
      ans += p[i].second - cnt;
      que.push({p[i].first + d * 2, p[i].second - cnt});
      cnt += p[i].second - cnt;
    }
  }
  cout << ans << endl;
}