Problem p03851 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
#define db double
#define ll long long
#define int long long
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0')
    f = c == '-' ? -1 : 1, c = getchar();
  while (c <= '9' && c >= '0')
    x = 10 * x + c - '0', c = getchar();
  return x * f;
}
int a[N], n, psum[N];
int f1[N], f2[N], tf[N];
int A[N], S[N], frm[N];
db getfl(int i, int j) { return 1.0 * (A[i] - A[j]) / (1.0 * i - j); }
void getit(int f[]) {
  int top = 0;
  S[++top] = 0;
  for (int i = 1; i <= n; i++) {
    while (top > 1 && getfl(S[top - 1], S[top]) <= i)
      top--;
    f[i] = A[S[top]] - i * S[top] + (i * i + i) / 2 - psum[i];
    frm[i] = S[top];
    f[i] = max(f[i], f[i - 1]);
    A[i] = f[i] + (i * i - i) / 2 + psum[i];
    while (top > 1 && getfl(i, S[top]) > getfl(S[top], S[top - 1]))
      top--;
    S[++top] = i;
  }
}
void readit() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), psum[i] = a[i] + psum[i - 1];
  getit(f1);
  reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    psum[i] = a[i] + psum[i - 1];
  getit(f2);
  reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    psum[i] = a[i] + psum[i - 1];
  reverse(f2 + 1, f2 + n + 1);
}
int tmx[N];
void find(int *tf1, int *tf2, int l, int r) {
  if (l == r) {
    tf[l] = max(tf[l], tf1[l - 1] + tf2[l + 1] - a[l] + 1);
    return;
  }
  int mid = (l + r) >> 1, top = 0;
  for (int i = l - 1; i < mid; i++) { // 鎵斿埌鍗曡皟鏍堥噷闈㈠幓
    A[i] = i * (i - 1) / 2 + tf1[i] + psum[i];
    while (top > 1 && getfl(S[top], S[top - 1]) <= getfl(i, S[top - 1]))
      top--;
    S[++top] = i;
  }
  for (int i = mid; i <= r; i++) { // 鍦ㄥ崟璋冩爤閲岄潰鏌ユ壘
    while (top > 1 && getfl(S[top], S[top - 1]) <= i)
      top--;
    int pos = S[top];
    tmx[i] = tf1[pos] + tf2[i + 1] + (i - pos) * (i - pos + 1) / 2 -
             (psum[i] - psum[pos]);
  }
  for (int i = r - 1; i >= mid; i--)
    tmx[i] = max(tmx[i], tmx[i + 1]);
  for (int i = mid; i <= r; i++)
    tf[i] = max(tf[i], tmx[i]);
  find(tf1, tf2, l, mid);
  find(tf1, tf2, mid + 1, r);
}
void CDQ() {
  find(f1, f2, 1, n);
  // for(int i=1;i<=n;i++) printf("%d ",f1[i]); cout<<endl;
  // for(int i=1;i<=n;i++) printf("%d ",f2[i]); cout<<endl;
  reverse(a + 1, a + n + 1);
  reverse(f1 + 1, f1 + 1 + n), reverse(f2 + 1, f2 + 1 + n),
      reverse(tf + 1, tf + 1 + n);
  for (int i = 1; i <= n; i++)
    psum[i] = psum[i - 1] + a[i];
  find(f2, f1, 1, n);
  reverse(a + 1, a + n + 1);
  reverse(f1 + 1, f1 + 1 + n), reverse(f2 + 1, f2 + 1 + n),
      reverse(tf + 1, tf + 1 + n);
}
signed main() {
  freopen("066F1.in", "r", stdin);
  freopen("066F1.out", "w", stdout);
  readit();
  for (int i = 1; i <= n; i++)
    tf[i] = 1 - a[i];
  CDQ();
  int m = read();
  //	for(int i=1;i<=n;i++) printf("%d ",f1[i]);cout<<endl;
  //	for(int i=1;i<=n;i++) printf("%d ",f2[i]);cout<<endl;
  //	for(int i=1;i<=n;i++) printf("%d ",tf[i]);cout<<endl;
  while (m--) {
    int p = read(), v = read(), t;
    int ans = f1[p - 1] + f2[p + 1];
    ans = max(ans, tf[p] + a[p] - v);
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
#define db double
#define ll long long
#define int long long
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0')
    f = c == '-' ? -1 : 1, c = getchar();
  while (c <= '9' && c >= '0')
    x = 10 * x + c - '0', c = getchar();
  return x * f;
}
int a[N], n, psum[N];
int f1[N], f2[N], tf[N];
int A[N], S[N], frm[N];
db getfl(int i, int j) { return 1.0 * (A[i] - A[j]) / (1.0 * i - j); }
void getit(int f[]) {
  int top = 0;
  S[++top] = 0;
  for (int i = 1; i <= n; i++) {
    while (top > 1 && getfl(S[top - 1], S[top]) <= i)
      top--;
    f[i] = A[S[top]] - i * S[top] + (i * i + i) / 2 - psum[i];
    frm[i] = S[top];
    f[i] = max(f[i], f[i - 1]);
    A[i] = f[i] + (i * i - i) / 2 + psum[i];
    while (top > 1 && getfl(i, S[top]) > getfl(S[top], S[top - 1]))
      top--;
    S[++top] = i;
  }
}
void readit() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), psum[i] = a[i] + psum[i - 1];
  getit(f1);
  reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    psum[i] = a[i] + psum[i - 1];
  getit(f2);
  reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    psum[i] = a[i] + psum[i - 1];
  reverse(f2 + 1, f2 + n + 1);
}
int tmx[N];
void find(int *tf1, int *tf2, int l, int r) {
  if (l == r) {
    tf[l] = max(tf[l], tf1[l - 1] + tf2[l + 1] - a[l] + 1);
    return;
  }
  int mid = (l + r) >> 1, top = 0;
  for (int i = l - 1; i < mid; i++) { // 鎵斿埌鍗曡皟鏍堥噷闈㈠幓
    A[i] = i * (i - 1) / 2 + tf1[i] + psum[i];
    while (top > 1 && getfl(S[top], S[top - 1]) <= getfl(i, S[top - 1]))
      top--;
    S[++top] = i;
  }
  for (int i = mid; i <= r; i++) { // 鍦ㄥ崟璋冩爤閲岄潰鏌ユ壘
    while (top > 1 && getfl(S[top], S[top - 1]) <= i)
      top--;
    int pos = S[top];
    tmx[i] = tf1[pos] + tf2[i + 1] + (i - pos) * (i - pos + 1) / 2 -
             (psum[i] - psum[pos]);
  }
  for (int i = r - 1; i >= mid; i--)
    tmx[i] = max(tmx[i], tmx[i + 1]);
  for (int i = mid; i <= r; i++)
    tf[i] = max(tf[i], tmx[i]);
  find(tf1, tf2, l, mid);
  find(tf1, tf2, mid + 1, r);
}
void CDQ() {
  find(f1, f2, 1, n);
  // for(int i=1;i<=n;i++) printf("%d ",f1[i]); cout<<endl;
  // for(int i=1;i<=n;i++) printf("%d ",f2[i]); cout<<endl;
  reverse(a + 1, a + n + 1);
  reverse(f1 + 1, f1 + 1 + n), reverse(f2 + 1, f2 + 1 + n),
      reverse(tf + 1, tf + 1 + n);
  for (int i = 1; i <= n; i++)
    psum[i] = psum[i - 1] + a[i];
  find(f2, f1, 1, n);
  reverse(a + 1, a + n + 1);
  reverse(f1 + 1, f1 + 1 + n), reverse(f2 + 1, f2 + 1 + n),
      reverse(tf + 1, tf + 1 + n);
}
signed main() {

  readit();
  for (int i = 1; i <= n; i++)
    tf[i] = 1 - a[i];
  CDQ();
  int m = read();
  //	for(int i=1;i<=n;i++) printf("%d ",f1[i]);cout<<endl;
  //	for(int i=1;i<=n;i++) printf("%d ",f2[i]);cout<<endl;
  //	for(int i=1;i<=n;i++) printf("%d ",tf[i]);cout<<endl;
  while (m--) {
    int p = read(), v = read(), t;
    int ans = f1[p - 1] + f2[p + 1];
    ans = max(ans, tf[p] + a[p] - v);
    printf("%lld\n", ans);
  }
  return 0;
}