Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int M = 1 << 20, N = 2 * M, INF = 1e18;

int n, val[N], h[N], a[N], b[N], d[N], f[N];

void insert(int i, int A, int B) {
  int deb[] = {A * d[i] + B, a[i] * d[i] + b[i]},
      fin[] = {A * f[i] + B, a[i] * f[i] + b[i]};
  if (deb[0] <= deb[1] && fin[0] <= fin[1])
    return;
  if (deb[1] <= deb[0] && fin[1] <= fin[0]) {
    a[i] = A, b[i] = B;
    return;
  }
  int m = (d[i] + f[i]) / 2, med[] = {A * m + B, a[i] * m + b[i]};
  if (med[0] > med[1]) {
    swap(a[i], A);
    swap(b[i], B);
    swap(deb[0], deb[1]);
  }
  if (deb[0] > deb[1])
    insert(i * 2, A, B);
  else
    insert(i * 2 + 1, A, B);
}

int getMax(int x) {
  int i = x + M, maxi = -INF;
  while (i > 0) {
    maxi = max(maxi, a[i] * x + b[i]);
    i /= 2;
  }
  return maxi;
}

signed main() {
  /*
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  */
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  for (int i = M; i < N; i++)
    f[i] = 1 + (d[i] = i - M);
  for (int i = M - 1; i > -1; i--) {
    d[i] = d[i * 2];
    f[i] = f[i * 2 + 1];
  }
  for (int i = 0; i < N; i++)
    b[i] = -INF;

  int A, B, C;
  /*cin >> n >> A >> B >> C;

  n++;
  for(int i = 1; i < n; i++)
      cin >> h[i];
  for(int i = 1; i < n; i++)
      h[i] += h[i-1];*/
  A = -1, B = 0;
  cin >> n >> C;
  C = -C;
  for (int i = 0; i < n; i++)
    cin >> h[i];

  for (int i = 0, j = 1; j < n; i++, j++) {
    insert(1, -2 * h[i] * A, A * h[i] * h[i] - B * h[i] + C + val[i]);
    val[j] = getMax(h[j]) + A * h[j] * h[j] + B * h[j];
  }
  cout << -val[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int M = 1 << 20, N = 2 * M, INF = 1e18;

int n, val[N], h[N], a[N], b[N], d[N], f[N];

void insert(int i, int A, int B) {
  if (i >= N)
    return;
  int deb[] = {A * d[i] + B, a[i] * d[i] + b[i]},
      fin[] = {A * f[i] + B, a[i] * f[i] + b[i]};
  if (deb[0] <= deb[1] && fin[0] <= fin[1])
    return;
  if (deb[1] <= deb[0] && fin[1] <= fin[0]) {
    a[i] = A, b[i] = B;
    return;
  }
  int m = (d[i] + f[i]) / 2, med[] = {A * m + B, a[i] * m + b[i]};
  if (med[0] > med[1]) {
    swap(a[i], A);
    swap(b[i], B);
    swap(deb[0], deb[1]);
  }
  if (deb[0] > deb[1])
    insert(i * 2, A, B);
  else
    insert(i * 2 + 1, A, B);
}

int getMax(int x) {
  int i = x + M, maxi = -INF;
  while (i > 0) {
    maxi = max(maxi, a[i] * x + b[i]);
    i /= 2;
  }
  return maxi;
}

signed main() {
  /*
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  */
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  for (int i = M; i < N; i++)
    f[i] = 1 + (d[i] = i - M);
  for (int i = M - 1; i > -1; i--) {
    d[i] = d[i * 2];
    f[i] = f[i * 2 + 1];
  }
  for (int i = 0; i < N; i++)
    b[i] = -INF;

  int A, B, C;
  /*cin >> n >> A >> B >> C;

  n++;
  for(int i = 1; i < n; i++)
      cin >> h[i];
  for(int i = 1; i < n; i++)
      h[i] += h[i-1];*/
  A = -1, B = 0;
  cin >> n >> C;
  C = -C;
  for (int i = 0; i < n; i++)
    cin >> h[i];

  for (int i = 0, j = 1; j < n; i++, j++) {
    insert(1, -2 * h[i] * A, A * h[i] * h[i] - B * h[i] + C + val[i]);
    val[j] = getMax(h[j]) + A * h[j] * h[j] + B * h[j];
  }
  cout << -val[n - 1];
}