Problem p00476 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
#define s 131072
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
class segtree {
public:
  ll seg[s * 2];
  ll lazy[s * 2];
  void lazy_evaluate(int k) {
    if (k * 2 + 2 >= s * 2)
      return;
    lazy[k * 2 + 2] += lazy[k];
    lazy[k * 2 + 1] += lazy[k];
    seg[k * 2 + 2] += lazy[k];
    seg[k * 2 + 1] += lazy[k];
    lazy[k] = 0;
  }
  ll update(int beg, int end, int idx, int lb, int ub, ll num) {
    if (ub < beg || end < lb) {
      return seg[idx];
    }
    if (beg <= lb && ub <= end) {
      lazy[idx] += num;
      seg[idx] += num;
      return seg[idx];
    }
    if (lazy[idx]) {
      lazy_evaluate(idx);
    }
    return seg[idx] =
               max(update(beg, end, idx * 2 + 1, lb, (lb + ub) / 2, num),
                   update(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub, num));
  }
  ll query(int beg, int end, int idx, int lb, int ub) {
    if (ub < beg || end < lb) {
      return -1000000000000000000LL;
    }
    if (beg <= lb && ub <= end) {
      return seg[idx];
    }
    if (lazy[idx]) {
      lazy_evaluate(idx);
    }
    return max(query(beg, end, idx * 2 + 1, lb, (lb + ub) / 2),
               query(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub));
  }
};
segtree seg_tree;
int main() {
  ll n, x;
  scanf("%lld %lld", &n, &x);
  seg_tree.update(0, s - 1, 0, 0, s - 1, x);
  ll ret = 0LL;
  priority_queue<pii> que;
  for (int i = 0; i < n - 1; i++) {
    ll za, zb;
    scanf("%lld %lld", &za, &zb);
    que.push(make_pair(zb, i));
    for (;;) {
      if (que.empty())
        break;
      if (seg_tree.query(i + 1, i + 1, 0, 0, s - 1) > za)
        break;
      pii zan = que.top();
      ll ka = (x - seg_tree.query(zan.second, i + 1, 0, 0, s - 1)) / zan.first,
         kb = (1 - seg_tree.query(i + 1, i + 1, 0, 0, s - 1) + za + zan.first -
               1) /
              zan.first;
      if (ka >= kb) {
        ret += kb;
        seg_tree.update(zan.second, s - 1, 0, 0, s - 1, kb * zan.first);
      } else {
        ret += ka;
        que.pop();
        que.push(make_pair(
            (x - seg_tree.query(zan.second, i + 1, 0, 0, s - 1)) % zan.first,
            zan.second));
        seg_tree.update(zan.second, s - 1, 0, 0, s - 1, ka * zan.first);
      }
    }
    seg_tree.update(i + 1, s - 1, 0, 0, s - 1, -za);
  }
  printf("%lld\\n", ret);
}

Error: TLE

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define maxs 1 << 18
class segtree {
public:
  ll seg[maxs];
  ll lazy[maxs];

  void init() {
    memset(seg, 0, sizeof(seg));
    memset(lazy, 0, sizeof(lazy));
  }
  void lazy_evaluate(int x) {
    if (x * 2 + 2 >= maxs)
      return;
    lazy[x * 2 + 1] += lazy[x];
    lazy[x * 2 + 2] += lazy[x];
    seg[x * 2 + 1] += lazy[x];
    seg[x * 2 + 2] += lazy[x];
    lazy[x] = 0;
  }
  ll update(int beg, int en, int idx, int lb, int ub, int val) {
    if (ub < beg || en < lb) {
      return seg[idx];
    }
    if (beg <= lb && ub <= en) {
      lazy[idx] += val;
      seg[idx] += val;
      lazy_evaluate(idx);
      return seg[idx];
    }
    if (lazy[idx]) {
      lazy_evaluate(idx);
    }
    return seg[idx] =
               max(update(beg, en, idx * 2 + 1, lb, (lb + ub) / 2, val),
                   update(beg, en, idx * 2 + 2, (lb + ub) / 2 + 1, ub, val));
  }
  ll query(int beg, int en, int idx, int lb, int ub) {
    if (ub < beg || en < lb) {
      return -100000000000LL;
    }
    if (beg <= lb && ub <= en) {
      return seg[idx];
    }
    if (lazy[idx]) {
      lazy_evaluate(idx);
    }
    return max(query(beg, en, idx * 2 + 1, lb, (lb + ub) / 2),
               query(beg, en, idx * 2 + 2, (lb + ub) / 2 + 1, ub));
  }
} seg_tree;
int main() {
  int n;
  ll x;
  scanf("%d %lld", &n, &x);
  seg_tree.init();
  seg_tree.update(0, maxs - 1, 0, 0, maxs - 1, x);
  priority_queue<pair<ll, ll>> que;
  int ret = 0;
  for (int i = 0; i < n - 1; i++) {
    ll x, y;
    scanf("%lld %lld", &x, &y);
    que.push(mp(y, i));

    for (;;) {
      if (que.empty())
        break;
      if (seg_tree.query(i + 1, i + 1, 0, 0, maxs - 1) > x)
        break;
      pair<ll, ll> p = que.top();
      que.pop();
      int v1 = (x - seg_tree.query(p.second, i, 0, 0, maxs - 1)) / p.first;
      int v2 =
          (1 - seg_tree.query(i + 1, i + 1, 0, 0, maxs - 1) + x + p.first - 1) /
          p.first;
      if (v1 >= v2) {
        ret += v1;
        seg_tree.update(p.second, maxs - 1, 0, 0, maxs - 1, v1 * p.first);
      } else {
        ret += v2;
        que.pop();
        seg_tree.update(p.second, maxs - 1, 0, 0, maxs - 1, v2 * p.first);
      }
    }
    seg_tree.update(i + 1, maxs - 1, 0, 0, maxs - 1, -x);
  }
  printf("%d\\n", ret);
}