Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
typedef long long ll;
typedef long double ld;
const ll MAXN = 2 * 1e5 + 10;
ll tAdd[4 * MAXN];
ll a[4 * MAXN];
ll t[4 * MAXN];

void push(ll v, ll tl, ll tr) {
  if (tAdd[v] != 0) {
    t[v] += (tr - tl + 1) * tAdd[v];
    tAdd[2 * v + 1] += tAdd[v];
    tAdd[2 * v + 2] += tAdd[v];
  }
  tAdd[v] = 0;
}

void build(ll v, ll tl, ll tr, ll a[]) {
  if (tl > tr) {
    return;
  }
  if (tl == tr) {
    t[v] = a[tl];
    return;
  }
  ll tm = (tl + tr) / 2;
  build(2 * v + 1, tl, tm, a);
  build(2 * v + 2, tm + 1, tr, a);
  t[v] = t[2 * v + 1] + t[2 * v + 2];
  return;
}

ll get(ll v, ll tl, ll tr, ll l, ll r) {
  push(v, tl, tr);
  if (l > tr || r < tl) {
    return 0;
  }
  if (l <= tl && r >= tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  return get(2 * v + 1, tl, tm, l, r) + get(2 * v + 2, tm + 1, tr, l, r);
}

void update(ll v, ll tl, ll tr, ll l, ll r, ll add) {
  push(v, tl, tr);
  if (l > tr || r < tl) {
    return;
  }
  if (l <= tl && r >= tr) {
    tAdd[v] += add;
    push(v, tl, tr);
    return;
  }
  ll tm = (tl + tr) / 2;
  update(2 * v + 1, tl, tm, l, r, add);
  update(2 * v + 2, tm + 1, tr, l, r, add);
}

int main() {
#ifdef _DEBUG
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  ll n, d, at;
  cin >> n >> d >> at;
  vector<ll> coord(n);
  vector<pair<ll, ll>> kek(n);
  for (int i = 0; i < n; i++) {
    cin >> kek[i].first >> kek[i].second;
  }
  sort(kek.begin(), kek.end());
  for (ll i = 0; i < n; i++) {
    coord[i] = kek[i].first;
    a[i] = kek[i].second;
  }
  build(0, 0, n - 1, a);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll cur = get(0, 0, n - 1, i, i);
    // cout << cur << "\n";
    if (cur <= 0)
      continue;
    ll cor = coord[i] + 2 * d;
    auto it = upper_bound(coord.begin(), coord.end(), cor);
    it--;
    ll r_bord = (it - coord.begin());
    ll now_at = (cur - 1) / at + 1;
    ans += now_at;
    update(0, 0, n - 1, i, r_bord, -now_at * at);
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
typedef long long ll;
typedef long double ld;
const ll MAXN = 1e6;
ll tAdd[4 * MAXN];
ll a[4 * MAXN];
ll t[4 * MAXN];

void push(ll v, ll tl, ll tr) {
  if (tAdd[v] != 0) {
    t[v] += (tr - tl + 1) * tAdd[v];
    tAdd[2 * v + 1] += tAdd[v];
    tAdd[2 * v + 2] += tAdd[v];
  }
  tAdd[v] = 0;
}

void build(ll v, ll tl, ll tr, ll a[]) {
  if (tl > tr) {
    return;
  }
  if (tl == tr) {
    t[v] = a[tl];
    return;
  }
  ll tm = (tl + tr) / 2;
  build(2 * v + 1, tl, tm, a);
  build(2 * v + 2, tm + 1, tr, a);
  t[v] = t[2 * v + 1] + t[2 * v + 2];
  return;
}

ll get(ll v, ll tl, ll tr, ll l, ll r) {
  push(v, tl, tr);
  if (l > tr || r < tl) {
    return 0;
  }
  if (l <= tl && r >= tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  return get(2 * v + 1, tl, tm, l, r) + get(2 * v + 2, tm + 1, tr, l, r);
}

void update(ll v, ll tl, ll tr, ll l, ll r, ll add) {
  push(v, tl, tr);
  if (l > tr || r < tl) {
    return;
  }
  if (l <= tl && r >= tr) {
    tAdd[v] += add;
    push(v, tl, tr);
    return;
  }
  ll tm = (tl + tr) / 2;
  update(2 * v + 1, tl, tm, l, r, add);
  update(2 * v + 2, tm + 1, tr, l, r, add);
}

int main() {
#ifdef _DEBUG
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  ll n, d, at;
  cin >> n >> d >> at;
  vector<ll> coord(n);
  vector<pair<ll, ll>> kek(n);
  for (int i = 0; i < n; i++) {
    cin >> kek[i].first >> kek[i].second;
  }
  sort(kek.begin(), kek.end());
  for (ll i = 0; i < n; i++) {
    coord[i] = kek[i].first;
    a[i] = kek[i].second;
  }
  build(0, 0, n - 1, a);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll cur = get(0, 0, n - 1, i, i);
    // cout << cur << "\n";
    if (cur <= 0)
      continue;
    ll cor = coord[i] + 2 * d;
    auto it = upper_bound(coord.begin(), coord.end(), cor);
    it--;
    ll r_bord = (it - coord.begin());
    ll now_at = (cur - 1) / at + 1;
    ans += now_at;
    update(0, 0, n - 1, i, r_bord, -now_at * at);
  }
  cout << ans;
}