Problem p01343 (C++) — buggy submission, verdict: Runtime Error

#ifndef VS
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#endif
using namespace std;
typedef long long LL;

#ifdef BTK
#define DEBUG if (1)
#else
#define DEBUG if (0)
#endif

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a)
    l = r;
  return a;
}
template <typename T> inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a)
    l = r;
  return a;
}

struct input {
  bool success;
  input() { success = true; }
#ifdef BTK
  template <typename T> inline input &operator>>(T &x) {
    success = !!(cin >> x);
    return *this;
  }
#else
  inline input &operator>>(LL &x) {
    success = (scanf("%lld", &x) != EOF);
    return *this;
  }
  inline input &operator>>(int &x) {
    success = (scanf("%d", &x) != EOF);
    return *this;
  }
  inline input &operator>>(char *x) {
    success = (scanf("%s", x) != EOF);
    return *this;
  }
  inline input &operator>>(double &x) {
    success = (scanf("%lf", &x) != EOF);
    return *this;
  }
#endif
} in;

const char space = ' ';
const char ln = '\\n';
struct output {
#ifdef BTK
  template <typename T> inline output &operator<<(T x) {
    cout << x;
    return (*this);
  }
  inline void fp(const double x, const char *s) {
    char buf[20];
    sprintf(buf, s, x);
    cout << buf;
  }
#else
  inline output &operator<<(const string &x) {
    printf("%s", x.c_str());
    return *this;
  }
  inline output &operator<<(const int x) {
    printf("%d", x);
    return *this;
  }
  inline output &operator<<(const char x) {
    putchar(x);
    return *this;
  }
  inline output &operator<<(const LL x) {
    printf("%lld", x);
    return *this;
  }
  inline output &operator<<(const double x) {
    printf("%lf", x);
    return *this;
  }
  inline void fp(const double x, const char *s) { printf(s, x); }
#endif
} out;

#define fi first
#define se second
#define pb push_back

#define IL inline
namespace geo {
typedef double D;
typedef bool B;
typedef complex<D> P;
#define X real()
#define Y imag()
B comp(const P &l, const P &r) { return (l.X == r.X) ? l.Y < r.Y : l.X < r.X; }
typedef pair<P, P> L;  // line
typedef pair<P, P> LS; // line segment
typedef pair<P, D> C;  // circle
typedef vector<P> Poly;
const D EPS = 1e-8;
// Decompotision Macro
#define DCl(a, b, l) P(a) = l.fi, (b) = l.se;
#define DCc(a, b, c)                                                           \\
  P(a) = c.fi;                                                                 \\
  D(b) = c.se;

// A dot B
IL D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
// A cross B
IL D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }
IL D ccw(P a, P b, P c) { return cross(b - a, c - a); }

IL int sgn(P a, P b, P c) {
  if (cross(b - a, c - a) > EPS)
    return 1;
  if (cross(b - a, c - a) < -EPS)
    return -1;
  if (dot(b - a, c - a) < -EPS)
    return 2; // b-a-c
  if (dot(a - b, c - b) < -EPS)
    return -2; // a-b-c
  return 0;
}
IL B is_L_L(L p, L q) {
  DCl(a, b, p);
  DCl(c, d, q);
  return abs(cross(a - b, c - d)) > EPS
      //|| abs(cross(a - b, d - b)) < EPS
      ;
}
IL D signed_distance_P_L(P p, L l) {
  DCl(a, b, l);
  return ccw(a, b, p) / abs(b - a);
}

IL P cross_L_L(L p, L q) {
  DCl(a, b, p);
  D d1 = signed_distance_P_L(a, q);
  D d2 = signed_distance_P_L(b, q);
  return (a * d2 - b * d1) / (d2 - d1);
}

} // namespace geo
using namespace geo;
const D pi = acos(-1);
int main() {
  int N;
  in >> N;
  D amax;
  in >> amax;
  vector<LS> ls(N);
  vector<P> center(N - 1);
  vector<D> vmax(N + 1);
  REP(i, N) {
    D a, b, c, d;
    in >> a >> b >> c >> d;
    ls[i] = LS(P(a, b), P(c, d));
  }
  vmax[0] = 0;
  REP(i, N - 1) {
    LS prv = ls[i];
    LS nxt = ls[i + 1];
    P v = prv.second - prv.first;
    P u = nxt.second - nxt.first;
    v = P(-v.Y, v.X);
    u = P(-u.Y, u.X);
    prv = LS(prv.second, prv.second + v);
    nxt = LS(nxt.first, nxt.first + u);
    if (is_L_L(prv, nxt)) {
      center[i] = cross_L_L(prv, nxt);
    } else {
      center[i] = (prv.first + nxt.first) / 2.0;
    }
    D r = abs(prv.first - center[i]);
    vmax[i + 1] = amax * r;
  }
  vmax[N] = 0;
  REP(i, N) {
    D s = abs(ls[i].second - ls[i].first);
    chmin(vmax[i + 1], vmax[i] + 2 * amax * s);
  }
  D ret = 0;
  REP(_, N) {
    int i = N - _ - 1;
    D s = abs(ls[i].second - ls[i].first);
    chmin(vmax[i], vmax[i + 1] + 2 * amax * s);
  }
  REP(i, N - 1) {
    P v = ls[i].second - center[i];
    P u = ls[i + 1].first - center[i];
    D r = abs(v);
    D si = cross(v, u);
    D co = dot(v, u);
    D theta = atan2(si, co);
    if (theta < 0)
      theta = -theta;
    if (abs(theta - pi) > EPS) {
      P cp = cross_L_L(ls[i], ls[i + 1]);
      if (dot(cp - ls[i].second, ls[i].second - ls[i].first) < 0) {
        theta = 2 * pi - theta;
      }
    }
    D s = r * theta;
    ret += s / sqrt(vmax[i + 1]);
  }
  REP(i, N) {
    /*
    v1-v0 = 2*a*s
    w = v0+2*a*x;
    w = v1+2*a*y;
    v1-v0 = 2*a*(x-y)

    */
    D s = abs(ls[i].second - ls[i].first);
    D x = (s + (vmax[i + 1] - vmax[i]) / (2.0 * amax)) / 2.0;
    D y = s - x;
    D w = sqrt(vmax[i] + 2 * amax * x);
    D ww = sqrt(vmax[i + 1] + 2 * amax * y);
    // t*(v+u)/2 = x
    ret += 2.0 * x / (sqrt(vmax[i]) + w);
    ret += 2.0 * y / (sqrt(vmax[i + 1]) + w);
  }
  out.fp(ret, "%.10f\\n");
  return 0;
}


Error: -6
758a6c21-4647-4326-b61b-71567a8a63f3.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01343/C++/s219223259.cpp:195: int main(): Assertion `abs(w - ww) < EPS' failed.


Fixed submission:

#ifndef VS
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#endif
using namespace std;
typedef long long LL;

#ifdef BTK
#define DEBUG if (1)
#else
#define DEBUG if (0)
#endif

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a)
    l = r;
  return a;
}
template <typename T> inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a)
    l = r;
  return a;
}

struct input {
  bool success;
  input() { success = true; }
#ifdef BTK
  template <typename T> inline input &operator>>(T &x) {
    success = !!(cin >> x);
    return *this;
  }
#else
  inline input &operator>>(LL &x) {
    success = (scanf("%lld", &x) != EOF);
    return *this;
  }
  inline input &operator>>(int &x) {
    success = (scanf("%d", &x) != EOF);
    return *this;
  }
  inline input &operator>>(char *x) {
    success = (scanf("%s", x) != EOF);
    return *this;
  }
  inline input &operator>>(double &x) {
    success = (scanf("%llf", &x) != EOF);
    return *this;
  }
#endif
} in;

const char space = ' ';
const char ln = '\\n';
struct output {
#ifdef BTK
  template <typename T> inline output &operator<<(T x) {
    cout << x;
    return (*this);
  }
  inline void fp(const double x, const char *s) {
    char buf[20];
    sprintf(buf, s, x);
    cout << buf;
  }
#else
  inline output &operator<<(const string &x) {
    printf("%s", x.c_str());
    return *this;
  }
  inline output &operator<<(const int x) {
    printf("%d", x);
    return *this;
  }
  inline output &operator<<(const char x) {
    putchar(x);
    return *this;
  }
  inline output &operator<<(const LL x) {
    printf("%lld", x);
    return *this;
  }
  inline output &operator<<(const double x) {
    printf("%lf", x);
    return *this;
  }
  inline void fp(const double x, const char *s) { printf(s, x); }
#endif
} out;

#define fi first
#define se second
#define pb push_back

#define IL inline
namespace geo {
typedef double D;
typedef bool B;
typedef complex<D> P;
#define X real()
#define Y imag()
B comp(const P &l, const P &r) { return (l.X == r.X) ? l.Y < r.Y : l.X < r.X; }
typedef pair<P, P> L;  // line
typedef pair<P, P> LS; // line segment
typedef pair<P, D> C;  // circle
typedef vector<P> Poly;
const D EPS = 1e-8;
// Decompotision Macro
#define DCl(a, b, l) P(a) = l.fi, (b) = l.se;
#define DCc(a, b, c)                                                           \\
  P(a) = c.fi;                                                                 \\
  D(b) = c.se;

// A dot B
IL D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
// A cross B
IL D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }
IL D ccw(P a, P b, P c) { return cross(b - a, c - a); }

IL int sgn(P a, P b, P c) {
  if (cross(b - a, c - a) > EPS)
    return 1;
  if (cross(b - a, c - a) < -EPS)
    return -1;
  if (dot(b - a, c - a) < -EPS)
    return 2; // b-a-c
  if (dot(a - b, c - b) < -EPS)
    return -2; // a-b-c
  return 0;
}
IL B is_L_L(L p, L q) {
  DCl(a, b, p);
  DCl(c, d, q);
  return abs(cross(a - b, c - d)) > EPS
      //|| abs(cross(a - b, d - b)) < EPS
      ;
}
IL D signed_distance_P_L(P p, L l) {
  DCl(a, b, l);
  return ccw(a, b, p) / abs(b - a);
}

IL P cross_L_L(L p, L q) {
  DCl(a, b, p);
  D d1 = signed_distance_P_L(a, q);
  D d2 = signed_distance_P_L(b, q);
  return (a * d2 - b * d1) / (d2 - d1);
}

} // namespace geo
using namespace geo;
const D pi = acos(-1);
int main() {
  int N;
  in >> N;
  D amax;
  in >> amax;
  vector<LS> ls;
  vector<P> center(N - 1);
  vector<D> vmax(N + 1);
  REP(i, N) {
    D a, b, c, d;
    in >> a >> b >> c >> d;
    ls.push_back(LS(P(a, b), P(c, d)));
  }
  vmax[0] = 0;
  REP(i, N - 1) {
    LS prv = ls[i];
    LS nxt = ls[i + 1];
    P v = prv.second - prv.first;
    P u = nxt.second - nxt.first;
    v = P(-v.Y, v.X);
    u = P(-u.Y, u.X);
    prv = LS(prv.second, prv.second + v);
    nxt = LS(nxt.first, nxt.first + u);
    if (is_L_L(prv, nxt)) {
      center[i] = cross_L_L(prv, nxt);
    } else {
      center[i] = (prv.first + nxt.first) / 2.0;
    }
    D r = abs(prv.first - center[i]);
    vmax[i + 1] = amax * r;
  }
  vmax[N] = 0;
  REP(i, N) {
    D s = abs(ls[i].second - ls[i].first);
    chmin(vmax[i + 1], vmax[i] + 2 * amax * s);
  }
  D ret = 0;
  REP(_, N) {
    int i = N - _ - 1;
    D s = abs(ls[i].second - ls[i].first);
    chmin(vmax[i], vmax[i + 1] + 2 * amax * s);
  }
  REP(i, N - 1) {
    P v = ls[i].second - center[i];
    P u = ls[i + 1].first - center[i];
    D r = abs(v);
    D si = cross(v, u);
    D co = dot(v, u);
    D theta = atan2(si, co);
    if (theta < 0)
      theta = -theta;
    if (abs(theta - pi) > EPS) {
      P cp = cross_L_L(ls[i], ls[i + 1]);
      if (dot(cp - ls[i].second, ls[i].second - ls[i].first) < 0) {
        theta = 2 * pi - theta;
      }
    }
    D s = r * theta;
    ret += s / sqrt(vmax[i + 1]);
  }
  REP(i, N) {
    /*
    v1-v0 = 2*a*s
    w = v0+2*a*x;
    w = v1+2*a*y;
    v1-v0 = 2*a*(x-y)

    */
    D s = abs(ls[i].second - ls[i].first);
    D x = (s + (vmax[i + 1] - vmax[i]) / (2.0 * amax)) / 2.0;
    D y = s - x;
    D w = sqrt(vmax[i] + 2 * amax * x);
    D ww = sqrt(vmax[i + 1] + 2 * amax * y);
    assert(abs(w - ww) < EPS);
    // t*(v+u)/2 = x
    ret += 2.0 * x / (sqrt(vmax[i]) + w);
    ret += 2.0 * y / (sqrt(vmax[i + 1]) + w);
  }
  out.fp(ret, "%.10f\\n");
  return 0;
}