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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 466666;
int n;
ll a[N], b[N], c[N], d[N], qz[N];
Pil sta[N];
int top;
void shift(ll *a, int p) {
  static ll b[N];
  rep(i, 1, n) b[i] = a[i];
  int len = 0;
  p = p + 1 <= n ? p + 1 : 1;
  while (len < n) {
    a[++len] = b[p];
    p = p + 1 <= n ? p + 1 : 1;
  }
}
Pil operator-(Pil a, Pil b) { return mp(a.fi - b.fi, a.se - b.se); }
ll Cross(Pil a, Pil b) { return a.fi * b.se - a.se * b.fi; }
int main() {
  read(n);
  rep(i, 1, n) read(a[i]);
  rep(i, 1, n) read(b[i]);
  int p = max_element(a + 1, a + n + 1) - a;
  shift(a, p);
  shift(b, p);
  a[0] = a[n];
  rep(i, 1, n) qz[i] = qz[i - 1] + 2 * b[i];
  rep(i, 1, n) d[i] = d[i - 1] - qz[i - 1];
  rep(i, 0, n) c[i] = d[i] + a[i];
  //	rep(i,0,n)printf("%d:%lld %lld %lld %lld\\n",i,a[i],b[i],c[i],d[i]);
  rep(i, 0, n) {
    auto x = mp(i, c[i]);
    while (top > 1 && Cross(x - sta[top - 1], sta[top] - sta[top - 1]) <= 0)
      top--;
    sta[++top] = x;
  }
  double res = 0;
  p = 1;
  //	rep(i,1,top)printf("%d %lld\\n",sta[i].fi,sta[i].se);
  rep(i, 0, n - 1) {
    while (p + 1 <= top && sta[p + 1].fi < i)
      p++;
    res += sta[p].se +
           1.0 * (sta[p + 1].se - sta[p].se) / (sta[p + 1].fi - sta[p].fi) *
               (i - sta[p].fi) -
           d[i];
    //	printf("%d:%lf\\n",i,sta[p].se+1.0*(sta[p+1].se-sta[p].se)/(sta[p+1].fi-sta[p].fi)*(i-sta[p].fi));
  }
  res /= n;
  printf("%.11lf\\n", res);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 466666;
int n;
ll a[N], b[N], c[N], d[N], qz[N];
Pil sta[N];
int top;
void shift(ll *a) {
  ll x = a[1];
  rep(i, 1, n - 1) a[i] = a[i + 1];
  a[n] = x;
}
Pil operator-(Pil a, Pil b) { return mp(a.fi - b.fi, a.se - b.se); }
ll Cross(Pil a, Pil b) { return a.fi * b.se - a.se * b.fi; }
int main() {
  read(n);
  rep(i, 1, n) read(a[i]);
  rep(i, 1, n) read(b[i]);
  int p = max_element(a + 1, a + n + 1) - a;
  while (p != n) {
    shift(a);
    shift(b);
    p--;
    if (p < 1)
      p = n;
  }
  a[0] = a[n];
  rep(i, 1, n) qz[i] = qz[i - 1] + 2 * b[i];
  rep(i, 1, n) d[i] = d[i - 1] - qz[i - 1];
  rep(i, 0, n) c[i] = d[i] + a[i];
  //	rep(i,0,n)printf("%d:%lld %lld %lld %lld\\n",i,a[i],b[i],c[i],d[i]);
  rep(i, 0, n) {
    auto x = mp(i, c[i]);
    while (top > 1 && Cross(x - sta[top - 1], sta[top] - sta[top - 1]) <= 0)
      top--;
    sta[++top] = x;
  }
  double res = 0;
  p = 1;
  //	rep(i,1,top)printf("%d %lld\\n",sta[i].fi,sta[i].se);
  rep(i, 0, n - 1) {
    while (p + 1 <= top && sta[p + 1].fi < i)
      p++;
    res += sta[p].se +
           1.0 * (sta[p + 1].se - sta[p].se) / (sta[p + 1].fi - sta[p].fi) *
               (i - sta[p].fi) -
           d[i];
    //	printf("%d:%lf\\n",i,sta[p].se+1.0*(sta[p+1].se-sta[p].se)/(sta[p+1].fi-sta[p].fi)*(i-sta[p].fi));
  }
  res /= n;
  printf("%.11lf\\n", res);
  return 0;
}