Problem p03744 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!(ch >= '0' && ch <= '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

const int N = 500005;
const double eps = 1e-8;

int n, head = 0, tail = 0;
double L, q[N][2];

int main() {

#ifndef ONLINE_JUDGE
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
#endif

  n = read();
  L = read();
  double S = 0, V = 0;
  rep(i, 1, n) {
    int t = read(), v = read();
    V += v;
    S += (double)t * v;
    while (V > L && head < tail) {
      if (V - L >= q[head][0]) {
        V -= q[head][0];
        S -= q[head][0] * q[head][1];
        head++;
      } else {
        q[head][0] -= V - L;
        S -= (V - L) * q[head][1];
        V = L;
        break;
      }
    }
    q[tail][0] = v;
    q[tail++][1] = t;
    printf("%lf\n", S / L);
    while (tail - head >= 2) {
      if (q[tail - 1][1] > q[tail - 2][1])
        break;
      q[tail - 2][1] =
          (q[tail - 2][0] * q[tail - 2][1] + q[tail - 1][0] * q[tail - 1][1]) /
          (q[tail - 2][0] + q[tail - 1][0]);
      q[tail - 2][0] += q[tail - 1][0];
      tail--;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!(ch >= '0' && ch <= '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

const int N = 500005;
const double eps = 1e-8;

int n, head = 0, tail = 0;
double L, q[N][2];

int main() {

#ifndef ONLINE_JUDGE
//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);
#endif

  n = read();
  L = read();
  double S = 0, V = 0;
  rep(i, 1, n) {
    int t = read(), v = read();
    V += v;
    S += (double)t * v;
    while (V > L && head < tail) {
      if (V - L >= q[head][0]) {
        V -= q[head][0];
        S -= q[head][0] * q[head][1];
        head++;
      } else {
        q[head][0] -= V - L;
        S -= (V - L) * q[head][1];
        V = L;
        break;
      }
    }
    q[tail][0] = v;
    q[tail++][1] = t;
    printf("%lf\n", S / L);
    while (tail - head >= 2) {
      if (q[tail - 1][1] > q[tail - 2][1])
        break;
      q[tail - 2][1] =
          (q[tail - 2][0] * q[tail - 2][1] + q[tail - 1][0] * q[tail - 1][1]) /
          (q[tail - 2][0] + q[tail - 1][0]);
      q[tail - 2][0] += q[tail - 1][0];
      tail--;
    }
  }

  return 0;
}