Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define REP(i, x, y) for (ll i = x; i <= y; ++i)
vector<string> vec_splitter(string s) {
  for (char &c : s)
    c = c == ',' ? ' ' : c;
  stringstream ss;
  ss << s;
  vector<string> res;
  for (string z; ss >> z; res.push_back(z))
    ;
  return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
void localTest() {
#ifndef ONLINE_JUDGE
  freopen("inp", "r", stdin);
  freopen("out", "w", stdout);
#endif
}
const ll N = 2e5 + 5, inf = 1e15, M = 1e6 + 5, MX = 1e6;
ll dp[N], n, b, A[N];
struct line {
  ll m, c;
};
vector<line> T(4 * M, (line){0LL, inf});
ll getY(line l, ll x) { return l.m * x + l.c; }
void addLine(ll node, ll lo, ll hi, line l) {
  if (lo > hi)
    return;
  ll mid = (lo + hi) >> 1LL;
  bool leftVal = getY(l, lo) < getY(T[node], lo);
  bool rightVal = getY(l, mid + 1) < getY(T[node], mid + 1);
  if (rightVal)
    swap(T[node], l);
  if (lo == hi)
    return;
  if (leftVal != rightVal)
    addLine(node << 1, lo, mid, l);
  else
    addLine(node << 1 | 1, mid + 1, hi, l);
  return;
}
ll query(ll node, ll lo, ll hi, ll x) {
  ll ans = getY(T[node], x);
  if (lo >= hi)
    return ans;
  ll mid = (lo + hi) >> 1LL;
  if (x <= mid)
    return min(ans, query(node << 1, lo, mid, x));
  else
    return min(ans, query(node << 1 | 1, mid + 1, hi, x));
}
int main() {

  localTest();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> b;
  for (ll i = 1; i <= n; ++i) {
    cin >> A[i];
    if (i > 1)
      dp[i] = b + A[i] * A[i] + query(1, 1, MX, A[i]);
    addLine(1, 1, MX, (line){-2 * A[i], dp[i] + A[i] * A[i]});
  }
  cout << dp[n] << "\n";

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define REP(i, x, y) for (ll i = x; i <= y; ++i)
vector<string> vec_splitter(string s) {
  for (char &c : s)
    c = c == ',' ? ' ' : c;
  stringstream ss;
  ss << s;
  vector<string> res;
  for (string z; ss >> z; res.push_back(z))
    ;
  return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
void localTest() {
#ifndef ONLINE_JUDGE
  freopen("inp", "r", stdin);
  freopen("out", "w", stdout);
#endif
}
const ll N = 2e5 + 5, inf = 1e15, M = 1e6 + 5, MX = 1e6;
ll dp[N], n, b, A[N];
struct line {
  ll m, c;
};
vector<line> T(4 * M, (line){0LL, inf});
ll getY(line l, ll x) { return l.m * x + l.c; }
void addLine(ll node, ll lo, ll hi, line l) {
  if (lo > hi)
    return;
  ll mid = (lo + hi) >> 1LL;
  bool leftVal = getY(l, lo) < getY(T[node], lo);
  bool rightVal = getY(l, mid + 1) < getY(T[node], mid + 1);
  if (rightVal)
    swap(T[node], l);
  if (lo == hi)
    return;
  if (leftVal != rightVal)
    addLine(node << 1, lo, mid, l);
  else
    addLine(node << 1 | 1, mid + 1, hi, l);
  return;
}
ll query(ll node, ll lo, ll hi, ll x) {
  ll ans = getY(T[node], x);
  if (lo >= hi)
    return ans;
  ll mid = (lo + hi) >> 1LL;
  if (x <= mid)
    return min(ans, query(node << 1, lo, mid, x));
  else
    return min(ans, query(node << 1 | 1, mid + 1, hi, x));
}
int main() {

  // localTest();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> b;
  for (ll i = 1; i <= n; ++i) {
    cin >> A[i];
    if (i > 1)
      dp[i] = b + A[i] * A[i] + query(1, 1, MX, A[i]);
    addLine(1, 1, MX, (line){-2 * A[i], dp[i] + A[i] * A[i]});
  }
  cout << dp[n] << "\n";

  return 0;
}