Problem p03744 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>

using namespace std;

#define For(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define Ford(i, a, b) for (int i = a, _b = b : i >= _b; --i)
#define Rep(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define sz(A) ((int)A.size())
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1)

typedef pair<int, int> pt;

int n;
int L;

void ReadData() { cin >> n >> L; }

deque<pair<long double, long long>> qu;
void Process() {
  cout << fixed << setprecision(6);

  long long sumRem = 0;
  long double sumTV = 0;
  For(i, 1, n) {
    int t, v;
    cin >> t >> v;
    if (sz(qu) == 0) {
      qu.push_back(make_pair(t, v));
      sumRem += v;
      sumTV += 1LL * v * t;
      cout << t << "\n";
      continue;
    }
    while (sumRem + v > L) {
      auto &cur = qu.front();
      if (cur.second <= (sumRem + v) - L) {
        sumRem -= cur.second;
        sumTV -= 1LL * cur.first * cur.second;
        qu.pop_back();
      } else {
        cur.second -= ((sumRem + v) - L);
        sumTV -= cur.first * ((sumRem + v) - L);
        sumRem -= ((sumRem + v) - L);
      }
    }
    long double res = (sumTV + 1LL * v * t) / (sumRem + v);
    cout << (double)res << "\n";

    pair<long double, long long> add = make_pair(t, v);
    while (sz(qu) && qu.back().first >= add.first) {
      add = make_pair(
          (qu.back().first * qu.back().second + add.first * add.second) /
              (qu.back().second + add.second),
          qu.back().second + add.second);
      sumRem -= qu.back().second;
      sumTV -= qu.back().first * qu.back().second;
      qu.pop_back();
    }
    qu.push_back(add);
    sumRem += add.second;
    sumTV += add.first * add.second;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.inp" , "r" , stdin);
  ReadData();
  Process();

  return 0;
}

Fixed submission:
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>

using namespace std;

#define For(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define Ford(i, a, b) for (int i = a, _b = b : i >= _b; --i)
#define Rep(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define sz(A) ((int)A.size())
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1)

typedef pair<int, int> pt;

int n;
int L;

void ReadData() { cin >> n >> L; }

deque<pair<long double, long long>> qu;
void Process() {
  cout << fixed << setprecision(6);

  long long sumRem = 0;
  long double sumTV = 0;
  For(i, 1, n) {
    int t, v;
    cin >> t >> v;
    if (sz(qu) == 0) {
      qu.push_back(make_pair(t, v));
      sumRem += v;
      sumTV += 1LL * v * t;
      cout << t << "\n";
      continue;
    }
    while (sumRem + v > L) {
      auto &cur = qu.front();
      if (cur.second <= (sumRem + v) - L) {
        sumRem -= cur.second;
        sumTV -= 1LL * cur.first * cur.second;
        qu.pop_front();
      } else {
        cur.second -= ((sumRem + v) - L);
        sumTV -= cur.first * ((sumRem + v) - L);
        sumRem -= ((sumRem + v) - L);
      }
    }
    long double res = (sumTV + 1LL * v * t) / (sumRem + v);
    cout << (double)res << "\n";

    pair<long double, long long> add = make_pair(t, v);
    while (sz(qu) && qu.back().first >= add.first) {
      add = make_pair(
          (qu.back().first * qu.back().second + add.first * add.second) /
              (qu.back().second + add.second),
          qu.back().second + add.second);
      sumRem -= qu.back().second;
      sumTV -= qu.back().first * qu.back().second;
      qu.pop_back();
    }
    qu.push_back(add);
    sumRem += add.second;
    sumTV += add.first * add.second;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.inp" , "r" , stdin);
  ReadData();
  Process();

  return 0;
}