Problem p00476 (C++) — buggy submission, verdict: Time Limit Exceeded

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#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 rep(i, x) for (int i = 0; i < x; i++)
int main() {
  int n;
  ll maxv;
  scanf("%d %lld", &n, &maxv);
  ll totv = maxv;
  ll totd = 0LL;
  deque<pair<ll, ll>> deq;
  ll ret = 0LL;
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    scanf("%lld %lld", &a, &b);
    while (!deq.empty() && deq.back().first < b)
      deq.pop_back();
    deq.pb(mp(b, totd));
    // printf("%d %d %lld %lld %lld\\n",i,deq.size(),totv,totd,ret);
    totd += a;
    while (totv <= totd) {
      ll bef = totv;
      while (!deq.empty()) {
        if (bef + deq.front().first - deq.front().second > maxv) {
          totv = max(totv, maxv + deq.front().second);
          deq.pop_front();
        } else {
          break;
        }
      }
      ll kai = deq.front().first;
      ll prev = deq.front().second;
      if (!deq.empty()) {
        totv = min(maxv + prev, max(totv, bef + kai));
        ll pv = max(0ll, min((totd - totv) / kai, (prev + maxv - totv) / kai));
        ret += pv;
        totv += pv * kai;
      }
      ret++;
      // cout << ret << " " << totv << endl;
    }
  }
  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 <deque>
#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 rep(i, x) for (int i = 0; i < x; i++)
int main() {
  int n;
  ll maxv;
  scanf("%d %lld", &n, &maxv);
  ll totv = maxv;
  ll totd = 0LL;
  deque<pair<ll, ll>> deq;
  ll ret = 0LL;
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    scanf("%lld %lld", &a, &b);
    while (!deq.empty() && deq.back().first < b)
      deq.pop_back();
    deq.pb(mp(b, totd));
    // printf("%d %d %lld %lld %lld\\n",i,deq.size(),totv,totd,ret);
    totd += a;
    while (totv <= totd) {
      ll s = 0ll;
      while (!deq.empty()) {
        if (totv + deq.front().first - deq.front().second > maxv) {
          totv = max(totv, maxv + deq.front().second);
          deq.pop_front();
        } else {
          break;
        }
      }
      ll kai = deq.front().first;
      ll prev = deq.front().second;
      if (!deq.empty()) {
        ll pv = max(0ll, min((totd - totv) / kai, (prev + maxv - totv) / kai));
        ret += pv;
        ret--;
        totv += pv * kai;
      }
      ret++;
    }
  }
  printf("%lld\\n", ret);
}