Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node *, Node *>
#define uid uniform_int_distribution
#define FILE                                                                   \
  ifstream in("input.txt");                                                    \
  ofstream out("output.txt");
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cin.sync_with_stdio(0);                                                      \
  cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

ld inf = 1e10;

struct CHT {
  vec<pdd> line;
  vec<pdd> gr;

  CHT() {
    line.pb({0, 0});
    gr.pb({-inf, inf});
  }

  ld gval(ld X) {
    int l = -1, r = (int)(gr.size()) - 1;
    for (; l + 1 < r;) {
      int md = (l + r) / 2;
      if (X > gr[md].S)
        l = md;
      else
        r = md;
    }
    /*for(int q=0; q<gr.size(); q++){
        if(gr[q].F<=X && X<=gr[q].S){
            l = q;
        }
    }*/
    assert(gr[r].F <= X && X <= gr[r].S);
    return line[r].F * X + line[r].S;
  }

  void add_line(ld K, ld B) {
    int l = -1, r = (int)(gr.size()) - 1;
    for (; l + 1 < r;) {
      int md = (l + r) / 2;
      ld px = (B - line[md].S) / (line[md].F - K);
      if (px > gr[md].S)
        l = md;
      else
        r = md;
    }
    ld px = (B - line[r].S) / (line[r].F - K);
    /*for(int q=0; q<line.size(); q++){
        ld X = (B-line[l].S) / (line[l].F-K);
        if(gr[q].F<=X && X<=gr[q].S) l = q;
    }
    cout<<"NEED: "<<px<<endl;
    for(pdd p : line) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl;
    for(int q=0; q<line.size(); q++) cout<<(B-line[q].S)/(line[q].F-K)<<"  ";
    cout<<endl; for(pdd p : gr) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl;
    cout<<"RES: "<<r+1<<endl<<endl;*/
    assert(gr[r].F <= px && px <= gr[r].S);
    for (; line.size() > r + 1;) {
      line.pop_back();
      gr.pop_back();
    }
    gr[r].S = px;
    line.pb({K, B});
    gr.pb({px, inf});
  }
};

ll a, c;
vec<ll> m, dp;

int main() {
  fast;
  cout.precision(2);
  cout << fixed;
  cin >> a >> c;
  m = vec<ll>(a);
  for (int q = 0; q < a; q++)
    cin >> m[q];
  dp = vec<ll>(a);
  CHT cht;
  cht.add_line(-2 * m[0], m[0] * m[0] + dp[0]);
  for (int q = 1; q < a; q++) {
    ll mn = 1e18;
    ld res = cht.gval(m[q]);
    // cout<<"VAL: "<<res<<endl;
    mn = res;
    /*for(int w=0; w<q; w++){
        ll tyt = -2*m[w]*m[q] + (dp[w]+m[w]*m[w]);
        mn = min(mn, tyt);
    }*/
    dp[q] = mn + m[q] * m[q] + c;
    cht.add_line(-2 * m[q], m[q] * m[q] + dp[q]);
  }
  // for(ll i : dp) cout<<i<<" "; cout<<endl;
  cout << dp[a - 1];
}

Fixed submission:
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node *, Node *>
#define uid uniform_int_distribution
#define FILE                                                                   \
  ifstream in("input.txt");                                                    \
  ofstream out("output.txt");
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cin.sync_with_stdio(0);                                                      \
  cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

ld inf = 1e15;

struct CHT {
  vec<pdd> line;
  vec<pdd> gr;

  CHT() {
    line.pb({0, 0});
    gr.pb({-inf, inf});
  }

  ld gval(ld X) {
    int l = -1, r = (int)(gr.size()) - 1;
    for (; l + 1 < r;) {
      int md = (l + r) / 2;
      if (X > gr[md].S)
        l = md;
      else
        r = md;
    }
    /*for(int q=0; q<gr.size(); q++){
        if(gr[q].F<=X && X<=gr[q].S){
            l = q;
        }
    }*/
    assert(gr[r].F <= X && X <= gr[r].S);
    return line[r].F * X + line[r].S;
  }

  void add_line(ld K, ld B) {
    int l = -1, r = (int)(gr.size()) - 1;
    for (; l + 1 < r;) {
      int md = (l + r) / 2;
      ld px = (B - line[md].S) / (line[md].F - K);
      if (px > gr[md].S)
        l = md;
      else
        r = md;
    }
    ld px = (B - line[r].S) / (line[r].F - K);
    /*for(int q=0; q<line.size(); q++){
        ld X = (B-line[l].S) / (line[l].F-K);
        if(gr[q].F<=X && X<=gr[q].S) l = q;
    }
    cout<<"NEED: "<<px<<endl;
    for(pdd p : line) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl;
    for(int q=0; q<line.size(); q++) cout<<(B-line[q].S)/(line[q].F-K)<<"  ";
    cout<<endl; for(pdd p : gr) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl;
    cout<<"RES: "<<r+1<<endl<<endl;*/
    assert(gr[r].F <= px && px <= gr[r].S);
    for (; line.size() > r + 1;) {
      line.pop_back();
      gr.pop_back();
    }
    gr[r].S = px;
    line.pb({K, B});
    gr.pb({px, inf});
  }
};

ll a, c;
vec<ll> m, dp;

int main() {
  fast;
  cout.precision(2);
  cout << fixed;
  cin >> a >> c;
  m = vec<ll>(a);
  for (int q = 0; q < a; q++)
    cin >> m[q];
  dp = vec<ll>(a);
  CHT cht;
  cht.add_line(-2 * m[0], m[0] * m[0] + dp[0]);
  for (int q = 1; q < a; q++) {
    ll mn = 1e18;
    ld res = cht.gval(m[q]);
    // cout<<"VAL: "<<res<<endl;
    mn = res;
    /*for(int w=0; w<q; w++){
        ll tyt = -2*m[w]*m[q] + (dp[w]+m[w]*m[w]);
        mn = min(mn, tyt);
    }*/
    dp[q] = mn + m[q] * m[q] + c;
    cht.add_line(-2 * m[q], m[q] * m[q] + dp[q]);
  }
  // for(ll i : dp) cout<<i<<" "; cout<<endl;
  cout << dp[a - 1];
}