Problem:
One
Problem Statement
電車の窓から美しい山並みが見える．
窓は，左下隅の座標が
(0, 0)
，右上隅の座標が
(W, H)
の長方形である．
窓からは
N
つの山が見えており，
i
番目の山は上に凸な放物線
y = a_i (x-p_i)^2 + q_i
の形をしている．
山と空との境界線の長さを求めよ．
次の三つの図は Sample Input に対応している．太線で示されている部分が山と空との境界線である．
Input
入力は以下の形式に従う．与えられる数は全て整数である．
W
H
N
a_1
p_1
q_1
...
a_N
p_N
q_N
Constraints
1 ≦ W, H ≦ 100
1 ≦ N ≦ 50
-100 ≦ a_i ≦ -1
0 ≦ p_i ≦ W
1 ≦ q_i ≦ H
i \neq j
ならば
(a_i, p_i, q_i) \neq (a_j, p_j, q_j)
Output
山と空との境界線の長さを 1 行に出力せよ．
出力する値は，真の値との絶対誤差または相対誤差が
10^{-6}
未満でなければならない．
Sample Input 1
20 20 1
-1 10 10
Output for the Sample Input 1
21.520346288593280
Sample Input 2
20 20 2
-1 10 10
-2 10 5
Output for the Sample Input 2
21.520346288593280
Sample Input 3
15 100 2
-2 5 100
-2 10 100
Output for the Sample Input 3
126.921542730127873

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;

int main() {
    int W, H, N;
    cin >> W >> H >> N;
    vector<double> a(N), p(N), q(N);
    for (int i = 0; i < N; i++)
        cin >> a[i] >> p[i] >> q[i];
    vector<double> kouten = {0.0, (double)W};
    auto addSeppen = [&](int i) {
        double xs1 = (a[i] * p[i] + sqrt(pow(a[i] * p[i], 2) - a[i] * (a[i] * pow(p[i], 2) + q[i]))) / a[i];
        if (xs1 >= 0 && xs1 <= W)
            kouten.insert(upper_bound(kouten.begin(), kouten.end(), xs1), xs1);
        double xs2 = (a[i] * p[i] - sqrt(pow(a[i] * p[i], 2) - a[i] * (a[i] * pow(p[i], 2) + q[i]))) / a[i];
        if (xs2 >= 0 && xs2 <= W)
            kouten.insert(upper_bound(kouten.begin(), kouten.end(), xs2), xs2);
    };
    auto addKouten = [&](int i, int j) {
        double xs1 = (a[i] * p[i] + sqrt(pow(a[i] * p[i], 2) - a[i] * (a[i] * pow(p[i], 2) + q[i]))) / a[i];
        double xs2 = (a[i] * p[i] - sqrt(pow(a[i] * p[i], 2) - a[i] * (a[i] * pow(p[i], 2) + q[i]))) / a[i];
        if (a[i] == a[j]) {
            if (p[i] == p[j]) return;
            double x = (a[i] * (pow(p[i], 2) - pow(p[j], 2)) + q[i] - q[j]) / (2.0 * a[i] * (p[i] - p[j]));
            if (x >= 0 && x <= W && x <= max(xs1, xs2) && x >= min(xs1, xs2))
                kouten.insert(upper_bound(kouten.begin(), kouten.end(), x), x);
        } else {
            double tmp1 = (a[i] * p[i] - a[j] * p[j]) / (a[i] - a[j]);
            double tmp2 = sqrt(pow(a[i] * p[i] - a[j] * p[j], 2) - (a[i] - a[j]) * (a[i] * pow(p[i], 2) + q[i] - a[j] * pow(p[j], 2) - q[j])) / (a[i] - a[j]);
            double x1 = tmp1 + tmp2;
            if (x1 >= 0 && x1 <= W && x1 <= max(xs1, xs2) && x1 >= min(xs1, xs2))
                kouten.insert(upper_bound(kouten.begin(), kouten.end(), x1), x1);
            double x2 = tmp1 - tmp2;
            if (x2 >= 0 && x2 <= W && x2 <= max(xs1, xs2) && x2 >= min(xs1, xs2))
                kouten.insert(upper_bound(kouten.begin(), kouten.end(), x2), x2);
        }
    };
    for (int i = 0; i < N; i++) {
        addSeppen(i);
        for (int j = 0; j < i; j++)
            addKouten(i, j);
    }
    auto findMax = [&](double start, double end) {
        double max = 0;
        int max_id = -1;
        for (int i = 0; i < N; i++) {
            double tmp = a[i] * pow((start + end) / 2.0 - p[i], 2) + q[i];
            if (tmp > max) max_id = i, max = tmp;
        }
        return max_id;
    };
    double ans = 0;
    for (int i = 0; i < kouten.size() - 1; i++) {
        double start = kouten[i], end = kouten[i + 1];
        int f_id = findMax(start, end);
        if (f_id == -1) continue;
        auto f = [&](double x) {
            return sqrt(1 + pow(2.0 * a[f_id] * (x - p[f_id]), 2));
        };
        auto simpson = [&]() {
            const int n = 10000;
            const double delta = (end - start) / n;
            double res = 0.0;
            for (int i = 0; i < n; i++) {
                double x1 = start + delta * i;
                res += delta / 6.0 * (f(x1) + 4.0 * f((x1 + x1 + delta) / 2.0) + f(x1 + delta));
            }
            return res;
        };
        double len = simpson();
        ans += len;
    }
    cout << fixed << setprecision(15);
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3620
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll inf=1e18;
double a[55],p[55],q[55],l[55],r[55];
double val_i(int i,double x){
    return a[i]*(x-p[i])*(x-p[i])+q[i];
}
double dist(double x1,double y1,double x2,double y2){
    double dx=x2-x1,dy=y2-y1;
    return sqrt(dx*dx+dy*dy);
}
double f(double x){
    return (x*sqrt(1+x*x)+log(x+sqrt(1+x*x)))/2;
}
double len_i(int i,double L,double R){
    double L2=2*a[i]*(L-p[i]),R2=2*a[i]*(R-p[i]);
    return (f(R2)-f(L2))/(2*a[i]);
}
int main(){
    int w,h,n;cin>>w>>h>>n;
    rep(i,n)cin>>a[i]>>p[i]>>q[i];
    double ans=0;
    double dx=1e-6;
    for(double now=0;now<w;now+=dx){
        double xi=0,xj=0;
        rep(i,n){
            xi=max(xi,val_i(i,now));
            xj=max(xj,val_i(i,now+dx));
        }
        if(xi>=1e-8&&xj>=1e-8){
            ans+=dist(now,xi,now+dx,xj);
        }
    }
    printf("%.9lf\n",ans);
}
accuracy: 0.017543859649122806
time_ms: 200
memory_kb: 3460
final_rank: 20

code_snippet: #define _USE_MATH_DEFINES
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using H = pair<ll, ll>;
using P = pair<ll, H>;
using vi = vector<int>;
using vl = vector<ll>;
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define Q(i, j, k) mkp((i), mkp((j), (k)))
#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) int((a).size())
#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())
#define ssp(i, n) (i==(ll)(n)-1?"\n":" ")
#define ctoi(c) (int)((c)-'0')
#define itoc(c) (char)((c)+'0')
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)
#define gcj printf("Case #%lld: ", __quetimes+1)
#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()
#define found(a, x) (a.find(x)!=a.end())
constexpr ll mod = (ll)1e9 + 7;
constexpr ll Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = (ll)3 * 1e18;
constexpr int Inf = 1e9;
constexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T = ll>T read() { T u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
template<typename T>
void fin(T x) { cout << x << endl; exit(0); }
//--------------------------------------------------------------
signed main() {
    ld w, h;int n;cin >> w >> h >> n;
    struct st {
        ld a, p, q;
    };
    vec<st>a(n);
    vec<ld>v;
    rep(i, n) {
        cin >> a[i].a >> a[i].p >> a[i].q;
        ld x = sqrt(-a[i].q / a[i].a);
        if (a[i].p + x <= w) {
            v.pb(a[i].p + x);
        }
        if (a[i].p - x >= 0) {
            v.pb(a[i].p - x);
        }
    }
    rep(i, n)rng(j, i + 1, n) {
        //a[i].a*x^2-2*a[i].a*a[i].p*x+a[i].q=a[i].a*x^2-2*a[i].a*a[i].p*x+a[i].q
        //(a[i].a-a[j].a)x^2-2*(a[i].a*a[i].p-a[j].a*a[j].p)x+a[i].q-a[j].q=0
        ld aa = a[i].a - a[j].a;
        ld bb = -2 * (a[i].a * a[i].p - a[j].a * a[j].p);
        ld cc = a[i].a * a[i].p * a[i].p + a[i].q - a[j].a * a[j].p * a[j].p - a[j].q;
        if (abs(aa) < EPS) {
            //bbx+cc=0となる点
            if (abs(bb) > EPS) {
                ld x = -cc / bb;
                if (0 <= x && x <= w) {
                    v.pb(x);
                }
            }
        }
        else {
            if (bb * bb - 4 * aa * cc < 0) continue;
            ld x = (-bb + sqrt(bb * bb - 4 * aa * cc)) / (2 * aa);
            if (0 <= x && x <= w) v.pb(x);
            x = (-bb - sqrt(bb * bb - 4 * aa * cc)) / (2 * aa);
            if (0 <= x && x <= w) v.pb(x);
        }
    }
    v.pb(0);v.pb(w);
    sort(all(v));
    crdcomp(v);

    //rep(i, siz(v)) cout << v[i] << " ";
    //cout << endl;

    ld ans = 0;
    rep(i, siz(v) - 1) {
        ld mid = (v[i] + v[i + 1]) / 2;
        int top = -1;ld hgt = 0;
        rep(j, n) {
            ld t = a[j].a * (mid - a[j].p) * (mid - a[j].p) + a[j].q;
            if (hgt < t) {
                top = j;hgt = t;
            }
        }
        if (top < 0) continue;
        //integral(v[i],v[i+1]) sqrt(1+4*a[i].a^2*(x-a[i].a)^2) dx
        //f(x)=sqrt(1+4*a[i].a^2*(x-a[i].p)^2)
        //=(v[i+1]-v[i])/6 * (f(v[i])+4*f(v[i]+v[i+1]/2)+f(v[i+1]))
        auto f = [&](ld x, int i)->ld {
            return sqrt(1 + 4 * a[i].a * a[i].a * (x - a[i].p) * (x - a[i].p));
            };
        const int div = 10000;
        rep(j, div) {
            ld lft = v[i] + ld(j) / div * (v[i + 1] - v[i]);
            ld rgt = v[i] + ld(j + 1) / div * (v[i + 1] - v[i]);
            ld itg = (rgt - lft) / 6 * (f(lft, top) + 4 * f((lft + rgt) / 2, top) + f(rgt, top));
            //ld itg = (rgt - lft) * f((lft + rgt) / 2, top);
            ans += itg;
        }
    }
    printf("%.20LF\n", ans);
}
accuracy: 1
time_ms: 150
memory_kb: 3624
final_rank: 8

code_snippet: #define _USE_MATH_DEFINES
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using H = pair<ll, ll>;
using P = pair<ll, H>;
using vi = vector<int>;
using vl = vector<ll>;
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define Q(i, j, k) mkp((i), mkp((j), (k)))
#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) int((a).size())
#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())
#define ssp(i, n) (i==(ll)(n)-1?"\n":" ")
#define ctoi(c) (int)((c)-'0')
#define itoc(c) (char)((c)+'0')
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)
#define gcj printf("Case #%lld: ", __quetimes+1)
#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()
#define found(a, x) (a.find(x)!=a.end())
constexpr ll mod = (ll)1e9 + 7;
constexpr ll Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = (ll)3 * 1e18;
constexpr int Inf = 1e9;
constexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T = ll>T read() { T u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
template<typename T>
void fin(T x) { cout << x << endl; exit(0); }
//--------------------------------------------------------------
signed main() {
    ld w, h;int n;cin >> w >> h >> n;
    struct st {
        ld a, p, q;
    };
    vec<st>a(n);
    vec<ld>v;
    rep(i, n) {
        cin >> a[i].a >> a[i].p >> a[i].q;
        ld x = sqrt(-a[i].q / a[i].a);
        if (a[i].p + x <= w) {
            v.pb(a[i].p + x);
        }
        if (a[i].p - x >= 0) {
            v.pb(a[i].p - x);
        }
    }
    rep(i, n)rng(j, i + 1, n) {
        //a[i].a*x^2-2*a[i].a*a[i].p*x+a[i].q=a[i].a*x^2-2*a[i].a*a[i].p*x+a[i].q
        //(a[i].a-a[j].a)x^2-2*(a[i].a*a[i].p-a[j].a*a[j].p)x+a[i].q-a[j].q=0
        ld aa = a[i].a - a[j].a;
        ld bb = -2 * (a[i].a * a[i].p - a[j].a * a[j].p);
        ld cc = a[i].a * a[i].p * a[i].p + a[i].q - a[j].a * a[j].p * a[j].p - a[j].q;
        if (abs(aa) < EPS) {
            //bbx+cc=0となる点
            if (abs(bb) > EPS) {
                ld x = -cc / bb;
                if (0 <= x && x <= w) {
                    v.pb(x);
                }
            }
        }
        else {
            if (bb * bb - 4 * aa * cc < 0) continue;
            ld x = (-bb + sqrt(bb * bb - 4 * aa * cc)) / (2 * aa);
            if (0 <= x && x <= w) v.pb(x);
            x = (-bb - sqrt(bb * bb - 4 * aa * cc)) / (2 * aa);
            if (0 <= x && x <= w) v.pb(x);
        }
    }
    v.pb(0);v.pb(w);
    sort(all(v));
    crdcomp(v);

    //rep(i, siz(v)) cout << v[i] << " ";
    //cout << endl;

    ld ans = 0;
    rep(i, siz(v) - 1) {
        ld mid = (v[i] + v[i + 1]) / 2;
        int top = -1;ld hgt = 0;
        rep(j, n) {
            ld t = a[j].a * (mid - a[j].p) * (mid - a[j].p) + a[j].q;
            if (hgt < t) {
                top = j;hgt = t;
            }
        }
        if (top < 0) continue;
        //integral(v[i],v[i+1]) sqrt(1+4*a[i].a^2*(x-a[i].a)^2) dx
        //f(x)=sqrt(1+4*a[i].a^2*(x-a[i].p)^2)
        //=(v[i+1]-v[i])/6 * (f(v[i])+4*f(v[i]+v[i+1]/2)+f(v[i+1]))
        auto f = [&](ld x, int i)->ld {
            return sqrt(1 + 4 * a[i].a * a[i].a * (x - a[i].p) * (x - a[i].p));
            };
        const int div = 10000;
        rep(j, div) {
            ld lft = v[i] + ld(j) / div * (v[i + 1] - v[i]);
            ld rgt = v[i] + ld(j + 1) / div * (v[i + 1] - v[i]);
            //ld itg = (rgt - lft) / 6 * (f(lft, top) + 4 * f((lft + rgt) / 2, top) + f(rgt, top));
            ld itg = (rgt - lft) * f((lft + rgt) / 2, top);
            ans += itg;
        }
    }
    printf("%.20LF\n", ans);
}
accuracy: 1
time_ms: 80
memory_kb: 3588
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e18;

bool epseq(const double &a, const double &b){
    return abs(a-b) < EPS;
}

vector<double> solve_quadeq(double a, double b, double c){
    vector<double> ret;
    if(abs(a) < EPS){
        if(abs(b) > EPS) ret.push_back(-c/b);
        return ret;
    }
    double d = b*b -4*a*c;
    if(abs(d) < EPS) d = 0;
    if(d < 0) return ret;
    if(d < 0) d = 0;
    ret.push_back((-b +sqrt(d))/(2*a));
    ret.push_back((-b -sqrt(d))/(2*a));
    return ret;
}

int main(){
    int w,h,n;
    cin >> w >> h >> n;
    vector<double> a(n), b(n), c(n);
    for(int i=0; i<n; i++){
        double aa,p,q;
        cin >> aa >> p >> q;
        a[i] = aa;
        b[i] = -2*aa*p;
        c[i] = aa*p*p +q;
    }

    vector<double> cpx;
    cpx.push_back(0);
    cpx.push_back(w);
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            vector<double> ret = solve_quadeq(a[i]-a[j], b[i]-b[j], c[i]-c[j]);
            cpx.insert(cpx.end(), ret.begin(), ret.end());
        }
        vector<double> ret = solve_quadeq(a[i], b[i], c[i]);
        cpx.insert(cpx.end(), ret.begin(), ret.end());
        ret = solve_quadeq(a[i], b[i], c[i] -h);
        cpx.insert(cpx.end(), ret.begin(), ret.end());
    }
    for(int i=cpx.size()-1; i>=0; i--){
        if(cpx[i] < 0 || w < cpx[i]){
            cpx.erase(cpx.begin() +i);
        }
    }
    sort(cpx.begin(), cpx.end());
    cpx.erase(unique(cpx.begin(), cpx.end(), epseq), cpx.end());

    double ans = 0;
    for(int i=0; i<(int)cpx.size()-1; i++){
        int hidx = -1;
        double hval = -INF;
        double x = (cpx[i] +cpx[i+1])/2.0;
        for(int j=0; j<n; j++){
            double val = a[j]*x*x +b[j]*x +c[j];
            if(val > hval){
                hval = val;
                hidx = j;
            }
        }
        if(hval < 0 || h < hval) continue;

        double interval = cpx[i+1] -cpx[i];
        int sepnum = 1;
        while(interval > sepnum*1e-6){
            sepnum *= 2;
        }
        double h = interval /sepnum;
        vector<double> f(sepnum+1);
        for(int j=0; j<=sepnum; j++){
            double dydx = 2*a[hidx]*(cpx[i] +j*h) +b[hidx];
            f[j] = sqrt(1 +dydx*dydx);
        }
        double sum = f[0] +f[sepnum];
        for(int j=1; j<sepnum; j+=2){
            sum += 4*f[j];
        }
        for(int j=2; j<sepnum; j+=2){
            sum += 2*f[j];
        }
        sum *= h/3;
        ans += sum;
    }
    cout << fixed << setprecision(10);
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 68224
final_rank: 9

code_snippet: #include <iostream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

 
double f(double a,double p,double q,double x){
    return a*(x-p)*(x-p)+q;
} 

double f(double a, double p, double x){
  return sqrt(1.0 + 4.0*a*a*x*x - 8.0*a*a*p*x + 4.0*a*a*p*p);
}
double s(double a, double p, double x1, double x2){
  double N = 10000000.0;
  double h, p1=0.0, p2=0.0;
  h = (x2 - x1)/ N;
  for(int n = 1; n <= N/2-1; n++){
      p1 += f(a, p, x1+2.0*n*h);
  }
  for(int n = 1; n <= N/2; n++){
      p2 += f(a, p, x1+2.0*n*h-h);
  }
  p = f(a, p, x1) + 2.0*p1 + 4.0*p2 + f(a, p, x2);
  return p*h/3.0;
}
 
int main(){
    int W,H,n; cin >> W >> H >> n;
    int a[50],p[50],q[50];
    for(int i=0; i<n; i++) cin >> a[i] >> p[i] >> q[i];
 
    vector<double> X; //動的配列クラス
    X.push_back(0);//末尾に0を挿入
    X.push_back(W);//末尾にWを挿入
    for(int i=0; i<n; i++){
        // 山 i と窓枠の下辺との交点
        if(0<=p[i]-sqrt(-(q[i]/a[i])) && p[i]-sqrt(-(q[i]/a[i]))<=W) X.push_back(p[i]-sqrt(-(q[i]/a[i]))); //枠内ならx1を末尾に挿入
        if(0<=p[i]+sqrt(-(q[i]/a[i])) && p[i]+sqrt(-(q[i]/a[i]))<=W) X.push_back(p[i]+sqrt(-(q[i]/a[i]))); //枠内ならx2を末尾に挿入
 
        for(int j=0; j<i; j++){ // 山 i と山 j の交点
            double A=a[i]-a[j];
            double B=-2*a[i]*p[i]+2*a[j]*p[j];
            double C=a[i]*p[i]*p[i]+q[i]-a[j]*p[j]*p[j]-q[j];
 
            if(A!=0){ // Ax^2+Bx+C=0
                if(B*B-4*A*C>=0){
                    double x;
                    x=(-B-sqrt(B*B-4*A*C))/(2*A);
                    if(0<=x && x<=W) X.push_back(x);//枠内なら交点を末尾に挿入
                    x=(-B+sqrt(B*B-4*A*C))/(2*A);
                    if(0<=x && x<=W) X.push_back(x);//枠内なら交点を末尾に挿入
                }
            }
            else if(B!=0){ // Bx+C=0
                double x=-1.0*C/B;
                if(0<=x && x<=W) X.push_back(x);//枠内なら交点を末尾に挿入
            }
        }
    }
 
    sort(X.begin(),X.end()); //昇順でソートする
 
    double ans=0;
    for(int i=0; i<(int)X.size()-1; i++){
        double x1=X[i],x2=X[i+1];
 
        int jmax=-1;
        for(int j=0; j<n; j++){
            if(f(a[j],p[j],q[j],(x1+x2)/2)>0//ｙ＝０より上
            && (jmax==-1 || f(a[jmax],p[jmax],q[jmax],(x1+x2)/2)<f(a[j],p[j],q[j],(x1+x2)/2))){//それまでで一番高い位置にある関数＜ｊ個目の関数のとき交換
                jmax=j;
            }
        }
 
        if(jmax != -1)ans+=s(a[jmax], p[jmax], x1, x2);
        
    }
 
    cout << fixed;
    cout << setprecision(9) << ans << endl;
 
    return 0;
}
accuracy: 0.017543859649122806
time_ms: 30
memory_kb: 3244
final_rank: 18

code_snippet: #include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INTERVAL 2097152
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double q_int(int nyama1, int nyama2, int left)
{
  int a1=yama[nyama1][0];
  int p1=yama[nyama1][1];
  int q1=yama[nyama1][2];
  int a2=yama[nyama2][0];
  int p2=yama[nyama2][1];
  int q2=yama[nyama2][2];
  if (a1==a2)
  {
    if(p1==p2)
      return -1.0;
    else
    {
        double ans=0.5*(p1+p2+(q2-q1)*1.0/(a1*(p2-p1)));
        if (quad(ans,nyama1)>0-EPS)
          return ans;
        else
          return -1.0;
    }

  }
  else
  {
    double A=a1-a2;
    double B=2*(p2*a2-p1*a1);
    double C=a1*p1*p1-a2*p2*p2+q1-q2;
    double delta=B*B-4*A*C;
    if (delta>0-EPS)
    {
      double ans=(-B+1.0*left*sqrt(delta))/(2.0*A);
      if (quad(ans,nyama1)>0-EPS)
        return ans;
      else
        return -1.0;
    }
    else
      return -1.0;
  }
}

double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrates(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  //return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
  return 1.0/2.0/INTERVAL*(cir(a,nyama)+cir(b,nyama));
}
double integrate(double b,double e,int nyama)
{
  int begin=b*INTERVAL;
  int end=e*INTERVAL;
  double ans=0.0;
  for(int i=begin;i<end;i++)
  {
    ans+=integrates(i,nyama);
  }
  return ans;
}
vector<double> sec;
int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
    double tmp1=x_int(i,-1);
    double tmp2=x_int(i,1);
    if (-EPS< tmp1 && tmp1<W+EPS)
      sec.push_back(tmp1);
    if (-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
      sec.push_back(tmp2);
  }
  for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++)
    {
      double tmp1=q_int(i,j,-1);
      double tmp2=q_int(i,j,1);
      if (-EPS< tmp1 && tmp1<W+EPS)
        sec.push_back(tmp1);
      if (-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
        sec.push_back(tmp2);
    }
  sort(sec.begin(),sec.end());
  sec.erase(unique(sec.begin(),sec.end()), sec.end() );

  double ans=0;
  double mae=0.0;
  //for(vector<double>::iterator it=sec.begin();it!=sec.end();++it)
  vector<double>::iterator it=sec.begin();
  while(1)
  {
    int nyama=-1;
    double max=0.0;
    double now_p;
    if (it==sec.end())
      now_p=1.0*W;
    else
      now_p=(*it);
    //printf("%lf\n",now_p);
    for(int j=0;j<N;j++)
    {
      double x=(mae+now_p)/2.0;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(mae,now_p,nyama);
    if (it==sec.end()) break;
    mae=now_p;
    ++it;
  }

  printf("%lf\n",ans);
}
accuracy: 0.7368421052631579
time_ms: 750
memory_kb: 2812
final_rank: 10

code_snippet: #include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INTERVAL 1048576
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double q_int(int nyama1, int nyama2, int left)
{
  int a1=yama[nyama1][0];
  int p1=yama[nyama1][1];
  int q1=yama[nyama1][2];
  int a2=yama[nyama2][0];
  int p2=yama[nyama2][1];
  int q2=yama[nyama2][2];
  if (a1==a2)
  {
    if(p1==p2)
      return -1.0;
    else
    {
        double ans=0.5*(p1+p2+(q2-q1)*1.0/(a1*(p2-p1)));
        if (quad(ans,nyama1)>0-EPS)
          return ans;
        else
          return -1.0;
    }

  }
  else
  {
    double A=a1-a2;
    double B=2*(p2*a2-p1*a1);
    double C=a1*p1*p1-a2*p2*p2+q1-q2;
    double delta=B*B-4*A*C;
    if (delta>0-EPS)
    {
      double ans=(-B+1.0*left*sqrt(delta))/(2.0*A);
      if (quad(ans,nyama1)>0-EPS)
        return ans;
      else
        return -1.0;
    }
    else
      return -1.0;
  }
}

double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrates(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  //return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
  return 1.0/2.0/INTERVAL*(cir(a,nyama)+cir(b,nyama));
}
double integrate(double b,double e,int nyama)
{
  int begin=b*INTERVAL;
  int end=e*INTERVAL;
  double ans=0.0;
  for(int i=begin;i<end;i++)
  {
    ans+=integrates(i,nyama);
  }
  return ans;
}
vector<double> sec;
int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
    double tmp1=x_int(i,-1);
    double tmp2=x_int(i,1);
    if (-EPS< tmp1 && tmp1<W+EPS)
      sec.push_back(tmp1);
    if (-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
      sec.push_back(tmp2);
  }
  for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++)
    {
      double tmp1=q_int(i,j,-1);
      double tmp2=q_int(i,j,1);
      if (-EPS< tmp1 && tmp1<W+EPS)
        sec.push_back(tmp1);
      if (-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
        sec.push_back(tmp2);
    }
  sort(sec.begin(),sec.end());
  sec.erase(unique(sec.begin(),sec.end()), sec.end() );

  double ans=0;
  double mae=0.0;
  //for(vector<double>::iterator it=sec.begin();it!=sec.end();++it)
  vector<double>::iterator it=sec.begin();
  while(1)
  {
    int nyama=-1;
    double max=0.0;
    double now_p;
    if (it==sec.end())
      now_p=1.0*W;
    else
      now_p=(*it);
    //printf("%lf\n",now_p);
    for(int j=0;j<N;j++)
    {
      double x=(mae+now_p)/2.0;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(mae,now_p,nyama);
    if (it==sec.end()) break;
    mae=now_p;
    ++it;
  }

  printf("%lf\n",ans);
}
accuracy: 0.7192982456140351
time_ms: 380
memory_kb: 2792
final_rank: 12

code_snippet: #include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INTERVAL 1048576
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double q_int(int nyama1, int nyama2, int left)
{
  int a1=yama[nyama1][0];
  int p1=yama[nyama1][1];
  int q1=yama[nyama1][2];
  int a2=yama[nyama2][0];
  int p2=yama[nyama2][1];
  int q2=yama[nyama2][2];
  if (a1==a2)
  {
    if(p1==p2)
      return -1.0;
    else
    {
        double ans=0.5*(p1+p2+(q2-q1)*1.0/(a1*(p2-p1)));
        if (quad(ans,nyama1)>0-EPS)
          return ans;
        else
          return -1.0;
    }

  }
  else
  {
    double A=a1-a2;
    double B=2*(p2*a2-p1*a1);
    double C=a1*p1*p1-a2*p2*p2+q1-q2;
    double delta=B*B-4*A*C;
    if (delta>0-EPS)
    {
      double ans=(-B+1.0*left*sqrt(delta))/(2.0*A);
      if (quad(ans,nyama1)>0-EPS)
        return ans;
      else
        return -1.0;
    }
    else
      return -1.0;
  }
}

double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrates(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
}
double integrate(double b,double e,int nyama)
{
  int begin=b*INTERVAL;
  int end=e*INTERVAL;
  double ans=0.0;
  for(int i=begin;i<end;i++)
  {
    ans+=integrates(i,nyama);
  }
  return ans;
}
vector<double> sec;
int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
    double tmp1=x_int(i,-1);
    double tmp2=x_int(i,1);
    if (-EPS< tmp1 && tmp1<W+EPS)
      sec.push_back(tmp1);
    if (-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
      sec.push_back(tmp2);
  }
  for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++)
    {
      double tmp1=q_int(i,j,-1);
      double tmp2=q_int(i,j,1);
      if (-EPS< tmp1 && tmp1<W+EPS)
        sec.push_back(tmp1);
      if (-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
        sec.push_back(tmp2);
    }
  sort(sec.begin(),sec.end());
  sec.erase(unique(sec.begin(),sec.end()), sec.end() );

  double ans=0;
  double mae=0.0;
  //for(vector<double>::iterator it=sec.begin();it!=sec.end();++it)
  vector<double>::iterator it=sec.begin();
  while(1)
  {
    int nyama=-1;
    double max=0.0;
    double now_p;
    if (it==sec.end())
      now_p=1.0*W;
    else
      now_p=(*it);
    //printf("%lf\n",now_p);
    for(int j=0;j<N;j++)
    {
      double x=(mae+now_p)/2.0;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(mae,now_p,nyama);
    if (it==sec.end()) break;
    mae=now_p;
    ++it;
  }

  printf("%lf\n",ans);
}
accuracy: 0.7192982456140351
time_ms: 560
memory_kb: 2792
final_rank: 13

code_snippet: #include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INTERVAL 1048576
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double q_int(int nyama1, int nyama2, int left)
{
  int a1=yama[nyama1][0];
  int p1=yama[nyama1][1];
  int q1=yama[nyama1][2];
  int a2=yama[nyama2][0];
  int p2=yama[nyama2][1];
  int q2=yama[nyama2][2];
  if (a1==a2)
  {
    if(p1==p2)
      return -1.0;
    else
    {
        double ans=0.5*(p1+p2+(q2-q1)*1.0/(a1*(p2-p1)));
        if (quad(ans,nyama1)>0-EPS)
          return ans;
        else
          return -1.0;
    }

  }
  else
  {
    double A=a1-a2;
    double B=2*(p2*a2-p1*a1);
    double C=a1*p1*p1-a2*p2*p2+q1-q2;
    double delta=B*B-4*A*C;
    if (delta>0-EPS)
    {
      double ans=(-B+1.0*left*sqrt(delta))/(2.0*A);
      if (quad(ans,nyama1)>0-EPS)
        return ans;
      else
        return -1.0;
    }
    else
      return -1.0;
  }
}

double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrates(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
}
double integrate(double b,double e,int nyama)
{
  int begin=b*INTERVAL;
  int end=e*INTERVAL;
  double ans=0.0;
  for(int i=begin;i<end;i++)
  {
    ans+=integrates(i,nyama);
  }
  return ans;
}
vector<double> sec;
int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
    double tmp1=x_int(i,-1);
    double tmp2=x_int(i,1);
    if (0-EPS< tmp1 && tmp1<W+EPS)
      sec.push_back(tmp1);
    if (0-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
      sec.push_back(tmp2);
  }
  for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++)
    {
      double tmp1=q_int(i,j,-1);
      double tmp2=q_int(i,j,1);
      if (0-EPS< tmp1 && tmp1<W+EPS)
        sec.push_back(tmp1);
      if (0-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
        sec.push_back(tmp2);
    }
  sort(sec.begin(),sec.end());
  sec.erase(unique(sec.begin(),sec.end()), sec.end() );

  double ans=0;
  double mae=0.0;
  //for(vector<double>::iterator it=sec.begin();it!=sec.end();++it)
  vector<double>::iterator it=sec.begin();
  while(1)
  {
    int nyama=-1;
    double max=0.0;
    double now_p;
    if (it==sec.end())
      now_p=1.0*W;
    else
      now_p=(*it);
    //printf("%lf\n",now_p);
    for(int j=0;j<N;j++)
    {
      double x=(mae+now_p)/2.0;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(mae,now_p,nyama);
    if (it==sec.end()) break;
    mae=now_p;
    ++it;
  }

  printf("%lf\n",ans);
}
accuracy: 0.7192982456140351
time_ms: 560
memory_kb: 2808
final_rank: 14

code_snippet: #include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INTERVAL 1100000
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double q_int(int nyama1, int nyama2, int left)
{
  int a1=yama[nyama1][0];
  int p1=yama[nyama1][1];
  int q1=yama[nyama1][2];
  int a2=yama[nyama2][0];
  int p2=yama[nyama2][1];
  int q2=yama[nyama2][2];
  if (a1==a2)
  {
    if(p1==p2)
      return -1.0;
    else
    {
        double ans=0.5*(p1+p2+(q2-q1)*1.0/(a1*(p2-p1)));
        if (quad(ans,nyama1)>0-EPS)
          return ans;
        else
          return -1.0;
    }

  }
  else
  {
    double A=a1-a2;
    double B=2*(p2*a2-p1*a1);
    double C=a1*p1*p1-a2*p2*p2+q1-q2;
    double delta=B*B-4*A*C;
    if (delta>0-EPS)
    {
      double ans=(-B+1.0*left*sqrt(delta))/(2.0*A);
      if (quad(ans,nyama1)>0-EPS)
        return ans;
      else
        return -1.0;
    }
    else
      return -1.0;
  }
}

double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrates(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
}
double integrate(double b,double e,int nyama)
{
  int begin=b*INTERVAL;
  int end=e*INTERVAL;
  double ans=0.0;
  for(int i=begin;i<end;i++)
  {
    ans+=integrates(i,nyama);
  }
  return ans;
}
vector<double> sec;
int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
    double tmp1=x_int(i,-1);
    double tmp2=x_int(i,1);
    if (0-EPS< tmp1 && tmp1<W+EPS)
      sec.push_back(tmp1);
    if (0-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
      sec.push_back(tmp2);
  }
  for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++)
    {
      double tmp1=q_int(i,j,-1);
      double tmp2=q_int(i,j,1);
      if (0-EPS< tmp1 && tmp1<W+EPS)
        sec.push_back(tmp1);
      if (0-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
        sec.push_back(tmp2);
    }
  sort(sec.begin(),sec.end());
  sec.erase(unique(sec.begin(),sec.end()), sec.end() );

  double ans=0;
  double mae=0.0;
  //for(vector<double>::iterator it=sec.begin();it!=sec.end();++it)
  vector<double>::iterator it=sec.begin();
  while(1)
  {
    int nyama=-1;
    double max=0.0;
    double now_p;
    if (it==sec.end())
      now_p=1.0*W;
    else
      now_p=(*it);
    //printf("%lf\n",now_p);
    for(int j=0;j<N;j++)
    {
      double x=(mae+now_p)/2.0;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(mae,now_p,nyama);
    if (it==sec.end()) break;
    mae=now_p;
    ++it;
  }

  printf("%lf\n",ans);
}
accuracy: 0.7368421052631579
time_ms: 980
memory_kb: 2808
final_rank: 11

code_snippet: #include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INTERVAL 1100000
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double q_int(int nyama1, int nyama2, int left)
{
  int a1=yama[nyama1][0];
  int p1=yama[nyama1][1];
  int q1=yama[nyama1][2];
  int a2=yama[nyama2][0];
  int p2=yama[nyama2][1];
  int q2=yama[nyama2][2];
  if (a1==a2)
  {
    if(p1==p2)
      return -1.0;
    else
      return 0.5*(p1+p2+(q2-q1)*1.0/(a1*(p2-p1)));
  }
  else
  {
    double A=a1-a2;
    double B=2*(p2*a2-p1*a1);
    double C=a1*p1*p1-a2*p2*p2+q1-q2;
    double delta=B*B-4*A*C;
    if (delta>0+EPS)
      return (-B+1.0*left*sqrt(delta))/(2.0*A);
    else
      return -1.0;
  }
}
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrates(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
}
double integrate(double b,double e,int nyama)
{
  int begin=b*INTERVAL;
  int end=e*INTERVAL;
  double ans=0.0;
  for(int i=begin;i<end;i++)
  {
    ans+=integrates(i,nyama);
  }
  return ans;
}
vector<double> sec;
int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
    double tmp1=x_int(i,-1);
    double tmp2=x_int(i,1);
    if (0-EPS< tmp1 && tmp1<W+EPS)
      sec.push_back(tmp1);
    if (0-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
      sec.push_back(tmp2);
  }
  for(int i=0;i<N;i++)
    for(int j=i+1;j<N;j++)
    {
      double tmp1=q_int(i,j,-1);
      double tmp2=q_int(i,j,1);
      if (0-EPS< tmp1 && tmp1<W+EPS)
        sec.push_back(tmp1);
      if (0-EPS< tmp2 && tmp2<W+EPS && fabs(tmp1-tmp2)>EPS)
        sec.push_back(tmp2);
    }
  sort(sec.begin(),sec.end());
  sec.erase(unique(sec.begin(),sec.end()), sec.end() );

  double ans=0;
  double mae=0.0;
  //for(vector<double>::iterator it=sec.begin();it!=sec.end();++it)
  vector<double>::iterator it=sec.begin();
  while(1)
  {
    int nyama=-1;
    double max=0.0;
    double now_p;
    if (it==sec.end())
      now_p=1.0*W;
    else
      now_p=(*it);
    //printf("%lf\n",now_p);
    for(int j=0;j<N;j++)
    {
      double x=(mae+now_p)/2.0;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(mae,now_p,nyama);
    if (it==sec.end()) break;
    mae=now_p;
    ++it;
  }

  printf("%lf\n",ans);
}
accuracy: 0.5614035087719298
time_ms: 990
memory_kb: 2840
final_rank: 16

code_snippet: #include <cstdio>
#include <cmath>
#define INTERVAL 200000
#define EPS 1e-9

int W,H,N;
int yama[101][3];
double xint[101][2];
double x_int(int nyama,int left)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return p+1.0*left*sqrt(-q/a);
}
double quad(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  double q=yama[nyama][2];
  return a*(x-p)*(x-p)+q;
}
double dquad_dx(double x,int nyama)
{
  double a=yama[nyama][0];
  double p=yama[nyama][1];
  return 2.0*a*(x-p);
}

double cir(double x,int nyama)
{
  double fpx=dquad_dx(x,nyama);
  return sqrt(1+fpx*fpx);
}

double integrate(int s, int nyama)
{
  double a=1.0*s/INTERVAL;
  double b=1.0*(s+1)/INTERVAL;
  return 1.0/6.0/INTERVAL*(cir(a,nyama)+cir(b,nyama)+4*cir((a+b)/2.0,nyama));
}

int main()
{
  scanf("%d%d%d",&W,&H,&N);
  for(int i=0;i<N;i++)
  {
    scanf("%d%d%d",&yama[i][0],&yama[i][1],&yama[i][2]);
    xint[i][0]=x_int(i,-1);
    xint[i][1]=x_int(i,1);
  }
  double ans=0;
  for(int i=0;i<W*INTERVAL;i++)
  {
    int nyama=-1;
    double max=0;
    for(int j=0;j<N;j++)
    {
      double x=i*1.0/INTERVAL;
      if (x<xint[j][0]-EPS||x>xint[j][1]+EPS) continue;
      double y=quad(x,j);
      if(y>max+EPS)
      {
        max=y;
        nyama=j;
      }
    }
    if (nyama!=-1)
      ans+=integrate(i,nyama);
  }
  printf("%lf\n",ans);
}
accuracy: 0.017543859649122806
time_ms: 70
memory_kb: 2676
final_rank: 19

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// 2次方程式 ax^2 + bx + c = 0の解 (重解を一つにまとめる)
vector<double> quadratic(double a, double b, double c){
  // assert(a != 0);
  if(abs(a) < EPS){
    if(abs(b) < EPS) return vector<double>();
    return vector<double>(1, -c / b);
  }
  double D = b*b - 4*a*c;
  if(D < 0) return vector<double>();
  if(D == 0) return vector<double>(1, -b/(2.0 * a));

  // |b| >> |ac|の時の桁落ちを避けるために
  // x_1 = (-b-sign(b)*sqrt(D))/(2*a), x_2 = c / (a*x_1)を利用する
  vector<double> res;
  int sign = (b >= 0) ? 1 : -1;
  double x1 = (-b - sign * sqrt(D))/(2.0 * a);
  double x2 = c / (a * x1);
  res.push_back(x1);
  res.push_back(x2);
  return res;
}
double f(double a, double b, double c, double x){
  return a * x * x + b * x + c ;
}
double A, B;
double g(double x){
  return sqrt(1 + (A * x + B) * (A * x + B));
}
// 区間[l, r]をN分割し、各区間を2次関数に近似する ->
// 各区間の積分値はh/3 * (f(x) + 4*f(x + h) + f(x + 2 * h)) (h = (r - l) / 2N, x = 0, 2 * h, 4 * h, (N - 2) * h)
double simpson(double l, double r, double f(double), int N){
  double h = (r - l) / (2 * N); 
  double S = f(l) + f(r);
  for(int i = 1; i < 2 * N; i += 2){
    S += 4.0 * f(l + h * i);
  }
  for(int i = 2; i < 2 * N; i += 2){
    S += 2.0 * f(l + h * i);
  }
  return S * h / 3.0;
}

int main(){
  double W, H;
  int N;
  while(cin >> W >> H >> N){
    double a[50], b[50], c[50];
    vector<double> inter_point;
    REP(i, N){
      double p, q;
      cin >> a[i] >> p >> q;
      b[i] = -2 * a[i] * p;
      c[i] = a[i] * p * p + q;
      vector<double> v = quadratic(a[i], b[i], c[i]);
      REP(i, v.size()){
        double x = v[i];
        x = max(0.0, x);
        x = min(W, x);
        bool ok = true;
        REP(i, inter_point.size()) if(abs(x - inter_point[i]) < EPS) ok = false;
        if(ok) inter_point.insert(lower_bound(inter_point.begin(), inter_point.end(), x), x);
      }
    }
    REP(i, N) FOR(j, i + 1, N) {
      vector<double> v = quadratic(a[i] - a[j], b[i] - b[j], c[i] - c[j]);
      REP(k, v.size()){
        double x = v[k];
        x = max(0.0, x);
        x = min(W, x);
        bool ok = true;
        REP(i, inter_point.size()) if(abs(x - inter_point[i]) < EPS) ok = false;
        if(ok) inter_point.insert(lower_bound(inter_point.begin(), inter_point.end(), x), x);
      }
    }
    double ans = 0;
    REP(i, inter_point.size() - 1){
      double lb = inter_point[i], ub = inter_point[i + 1];
      double mid = (lb + ub) / 2;
      int idx = 0;
      REP(j, N) if(f(a[idx], b[idx], c[idx], mid) < f(a[j], b[j], c[j], mid)){
        idx = j;
      }
      if(!(f(a[idx], b[idx], c[idx], lb + EPS) >= - EPS)) continue;
      if(!(f(a[idx], b[idx], c[idx], ub - EPS) >= - EPS)) continue;
      //printf("[%lf, %lf] idx = %d\n", lb, ub, idx);
      A = 2 * a[idx];
      B = b[idx];
      const int N = 1000;
      ans += simpson(lb, ub, g, N);
    }
    assert(!isnan(ans));
    printf("%.16lf\n", ans);
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1304
final_rank: 3

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// 2次方程式 ax^2 + bx + c = 0の解 (重解を一つにまとめる)
vector<double> quadratic(double a, double b, double c){
  // assert(a != 0);
  if(abs(a) < EPS){
    if(abs(b) < EPS) return vector<double>();
    return vector<double>(1, -c / b);
  }
  double D = b*b - 4*a*c;
  if(D < 0) return vector<double>();
  if(D == 0) return vector<double>(1, -b/(2.0 * a));

  // |b| >> |ac|の時の桁落ちを避けるために
  // x_1 = (-b-sign(b)*sqrt(D))/(2*a), x_2 = c / (a*x_1)を利用する
  vector<double> res;
  int sign = (b >= 0) ? 1 : -1;
  double x1 = (-b - sign * sqrt(D))/(2.0 * a);
  double x2 = c / (a * x1);
  res.push_back(x1);
  res.push_back(x2);
  return res;
}
double f(double a, double b, double c, double x){
  return a * x * x + b * x + c ;
}
double g(double A, double B, double x){
  return sqrt(1 + (A * x + B) * (A * x + B));
}

int main(){
  double W, H;
  int N;
  while(cin >> W >> H >> N){
    double a[50], b[50], c[50];
    vector<double> inter_point;
    REP(i, N){
      double p, q;
      cin >> a[i] >> p >> q;
      b[i] = -2 * a[i] * p;
      c[i] = a[i] * p * p + q;
      vector<double> v = quadratic(a[i], b[i], c[i]);
      REP(i, v.size()){
        double x = v[i];
        x = max(0.0, x);
        x = min(W, x);
        bool ok = true;
        REP(i, inter_point.size()) if(abs(x - inter_point[i]) < EPS) ok = false;
        if(ok) inter_point.insert(lower_bound(inter_point.begin(), inter_point.end(), x), x);
      }
    }
    REP(i, N) FOR(j, i + 1, N) {
      vector<double> v = quadratic(a[i] - a[j], b[i] - b[j], c[i] - c[j]);
      REP(k, v.size()){
        double x = v[k];
        x = max(0.0, x);
        x = min(W, x);
        bool ok = true;
        REP(i, inter_point.size()) if(abs(x - inter_point[i]) < EPS) ok = false;
        if(ok) inter_point.insert(lower_bound(inter_point.begin(), inter_point.end(), x), x);
      }
    }
    double ans = 0;
    REP(i, inter_point.size() - 1){
      double lb = inter_point[i], ub = inter_point[i + 1];
      double mid = (lb + ub) / 2;
      int idx = 0;
      REP(j, N) if(f(a[idx], b[idx], c[idx], mid) < f(a[j], b[j], c[j], mid)){
        idx = j;
      }
      if(!(f(a[idx], b[idx], c[idx], lb + EPS) >= - EPS)) continue;
      if(!(f(a[idx], b[idx], c[idx], ub - EPS) >= - EPS)) continue;
      //printf("[%lf, %lf] idx = %d\n", lb, ub, idx);
      double A = 2 * a[idx];
      double B = b[idx];
      const int N = 1000;
      double delta = (ub - lb) / N;
      double S = 0;
      S += g(A, B, lb) + g(A, B, ub);
      for(int i = 1; i < N; i += 2){
        S += 4.0 * g(A, B, lb + delta * i);
      }
      for(int i = 2; i < N; i += 2){
        S += 2.0 * g(A, B, lb + delta * i);
      }
      ans += S * delta / 3.0;
    }
    assert(!isnan(ans));
    printf("%.16lf\n", ans);
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1308
final_rank: 1

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// 2次方程式 ax^2 + bx + c = 0の解 (重解を一つにまとめる)
vector<double> quadratic(double a, double b, double c){
  // assert(a != 0);
  if(abs(a) < EPS){
    if(abs(b) < EPS) return vector<double>();
    return vector<double>(1, -c / b);
  }
  double D = b*b - 4*a*c;
  if(D < 0) return vector<double>();
  if(D == 0) return vector<double>(1, -b/(2.0 * a));

  // |b| >> |ac|の時の桁落ちを避けるために
  // x_1 = (-b-sign(b)*sqrt(D))/(2*a), x_2 = c / (a*x_1)を利用する
  vector<double> res;
  int sign = (b >= 0) ? 1 : -1;
  double x1 = (-b - sign * sqrt(D))/(2.0 * a);
  double x2 = c / (a * x1);
  res.push_back(x1);
  res.push_back(x2);
  return res;
}
double f(double a, double b, double c, double x){
  return a * x * x + b * x + c ;
}
double g(double A, double B, double x){
  return sqrt(1 + (A * x + B) * (A * x + B));
}

int main(){
  double W, H;
  int N;
  while(cin >> W >> H >> N){
    double a[50], b[50], c[50];
    vector<double> inter_point;
    REP(i, N){
      double p, q;
      cin >> a[i] >> p >> q;
      b[i] = -2 * a[i] * p;
      c[i] = a[i] * p * p + q;
      vector<double> v = quadratic(a[i], b[i], c[i]);
      REP(i, v.size()){
        double x = v[i];
        x = max(0.0, x);
        x = min(W, x);
        bool ok = true;
        REP(i, inter_point.size()) if(abs(x - inter_point[i]) < EPS) ok = false;
        if(ok) inter_point.insert(lower_bound(inter_point.begin(), inter_point.end(), x), x);
      }
    }
    REP(i, N) FOR(j, i + 1, N) {
      vector<double> v = quadratic(a[i] - a[j], b[i] - b[j], c[i] - c[j]);
      REP(k, v.size()){
        double x = v[k];
        x = max(0.0, x);
        x = min(W, x);
        bool ok = true;
        REP(i, inter_point.size()) if(abs(x - inter_point[i]) < EPS) ok = false;
        if(ok) inter_point.insert(lower_bound(inter_point.begin(), inter_point.end(), x), x);
      }
    }
    double ans = 0;
    REP(i, inter_point.size() - 1){
      double lb = inter_point[i], ub = inter_point[i + 1];
      double mid = (lb + ub) / 2;
      int idx = 0;
      REP(j, N) if(f(a[idx], b[idx], c[idx], mid) < f(a[j], b[j], c[j], mid)){
        idx = j;
      }
      if(!(f(a[idx], b[idx], c[idx], lb + EPS) >= - EPS)) continue;
      if(!(f(a[idx], b[idx], c[idx], ub - EPS) >= - EPS)) continue;
      //printf("[%lf, %lf] idx = %d\n", lb, ub, idx);
      double A = 2 * a[idx];
      double B = b[idx];
      const int N = 10000;
      double delta = (ub - lb) / N;
      double S = 0;
      S += g(A, B, lb) + g(A, B, ub);
      for(int i = 1; i < N; i += 2){
        S += 4.0 * g(A, B, lb + delta * i);
      }
      for(int i = 2; i < N; i += 2){
        S += 2.0 * g(A, B, lb + delta * i);
      }
      ans += S * delta / 3.0;
    }
    assert(!isnan(ans));
    printf("%.16lf\n", ans);
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 1304
final_rank: 6

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

struct Point{
	double x,y;
	Point(){}
	Point(double x,double y):x(x),y(y){}
	bool operator<(const Point& p)const{
		return x<p.x || (x==p.x && y<p.y);
	}
};
ostream& operator<<(ostream& os,const Point& p)
{
	printf("(%.2f,%.2f)",p.x+EPS,p.y+EPS);
	return os;
}

// f(x)=a*(x-p)^2+q
double f(double a,double p,double q,double x)
{
	return a*(x-p)*(x-p)+q;
}

// \int{sqrt(1+x^2) dx}
double integral(double x)
{
	return (x*sqrt(1+x*x)+log(x+sqrt(1+x*x)))/2;
}
// \int{sqrt(1+(2a(x-p))^2)}
double integral(double a,double p,double x)
{
	return integral(2*a*(x-p))/(2*a);
}
// y=a(x-p)^2の[x1,x2]間の曲線長
double curvelen(double a,double p,double x1,double x2)
{
	return integral(a,p,x2)-integral(a,p,x1);
}

int main()
{
	int w,h,n; cin>>w>>h>>n;
	vi as(n),ps(n),qs(n);
	rep(i,n) cin>>as[i]>>ps[i]>>qs[i];
	
	vector<Point> cs; // 交点
	cs.emplace_back(0,0);
	cs.emplace_back(w,0);
	
	rep(i,n){
		double a=as[i];
		double b=-2*as[i]*ps[i];
		double c=as[i]*ps[i]*ps[i]+qs[i];
		
		cs.emplace_back(0,f(as[i],ps[i],qs[i],0)); // 左枠との交点
		cs.emplace_back(w,f(as[i],ps[i],qs[i],w)); // 右枠との交点
		
		double x1=(-b-sqrt(b*b-4*a*c))/(2*a);
		double x2=(-b+sqrt(b*b-4*a*c))/(2*a);
		cs.emplace_back(x1,f(as[i],ps[i],qs[i],x1)); // 下枠との交点1
		cs.emplace_back(x2,f(as[i],ps[i],qs[i],x2)); // 下枠との交点2
	}
	
	rep(i,n) rep(j,n){ // 2つの放物線の交点
		// ax^2+bx+c=0をxについて解く
		double a=as[j]-as[i];
		double b=-2*(as[j]*ps[j]-as[i]*ps[i]);
		double c=(as[j]*ps[j]*ps[j]+qs[j])-(as[i]*ps[i]*ps[i]+qs[i]);
		
		if(abs(a)<EPS){
			if(abs(b)>EPS){
				double x=-c/b;
				if(EPS<x && x<w-EPS)
					cs.emplace_back(x,f(as[i],ps[i],qs[i],x));
			}
		}
		else{
			double d=b*b-4*a*c;
			if(abs(d)<EPS){ // 1点で交わる
				double x=-b/(2*a);
				cs.emplace_back(x,f(as[i],ps[i],qs[i],x));
			}
			else if(d>EPS){ // 2点で交わる
				double x1=(-b-sqrt(b*b-4*a*c))/(2*a);
				double x2=(-b+sqrt(b*b-4*a*c))/(2*a);
				cs.emplace_back(x1,f(as[i],ps[i],qs[i],x1));
				cs.emplace_back(x2,f(as[i],ps[i],qs[i],x2));
			}
		}
	}
	
	sort(all(cs));
	for(int i=0;i<cs.size();){
		if(cs[i].x<-EPS || w+EPS<cs[i].x)
			cs.erase(cs.begin()+i);
		else
			i++;
	}
	for(int i=0;i+1<cs.size();){
		if(cs[i].x+EPS>cs[i+1].x)
			cs.erase(cs.begin()+i);
		else
			i++;
	}
	
	double res=0;
	rep(i,cs.size()-1){
		int j=-1;
		rep(k,n){
			double y1=f(as[k],ps[k],qs[k],cs[i].x);
			double y2=f(as[k],ps[k],qs[k],cs[i+1].x);
			double y3=f(as[k],ps[k],qs[k],(cs[i].x+cs[i+1].x)/2);
			if(j==-1 && y3>EPS
			|| j!=-1 && y3>f(as[j],ps[j],qs[j],(cs[i].x+cs[i+1].x)/2)+EPS)
				j=k;
		}
		if(j!=-1)
			res+=curvelen(as[j],ps[j],cs[i].x,cs[i+1].x);
	}
	printf("%.10f\n",res);
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1392
final_rank: 2

code_snippet: #include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

#define double long double
double EPS = 1e-12;


double g(double x,double A,double B,double C){

	return A*x*x+B*x+C;
}
double f(double x,double A,double B){
	double t = 2*A*x+B;
	return sqrt(1+t*t);
	//return 0.5*(t*sqrt(t*t+1)+log(abs(t+sqrt(1+t*t))))/(2.*A);
}
double integral(double a,double b,double A,double B){
	double S = 1000;
	double d = (b-a) / S;
	double ans = 0;
	for(int i = 0 ; i < S ; i++){
		double aa = a + d * i;
		double bb = a + d * (i+1);
		ans += d * ( f(aa,A,B) + f((aa+bb)*0.5,A,B) + f(bb,A,B) ) / 3.0;
	}
	return ans;
	return f(b,A,B) - f(a,A,B);
}
vector<double> cp;
void crosspoint(double a,double b,double c){
	if( fabs(a) < EPS ){ cp.push_back(-c/b); return; }
	if( b*b-4*a*c < EPS ) return;
	cp.push_back((-b-sqrt(b*b-4*a*c))/(2*a));
	cp.push_back((-b+sqrt(b*b-4*a*c))/(2*a));
}
double A[100] = {} , P[100] = {} , Q[100] = {};
double B[100] , C[100] = {};
int main(){
	double W,H;
	int N;
	cin >> W >> H >> N;
	for(int i = 0; i < N; i++){
		cin >> A[i] >> P[i] >> Q[i];
		B[i] = -2.0*P[i]*A[i];
		C[i] = A[i] * P[i] * P[i] + Q[i];
		
	}
	for(int i = 0 ; i < N ; i++){
		crosspoint(A[i],B[i],C[i]);
	}
	
	for(int i = 0 ; i < N ; i++)
		for(int j = i+1 ; j < N ; j++)
			crosspoint(A[i]-A[j],B[i]-B[j],C[i]-C[j]);
	cp.push_back(0);
	cp.push_back(W);
	sort(cp.begin(),cp.end());
	double previous_x = 0;
	double ans = 0;
	for(int i = 0 ; i < cp.size() ; i++){
		if( cp[i] < -EPS || cp[i] > W + EPS) continue;
		double height = 0;
		int idx = -1;
		double a = previous_x;
		double b = cp[i];
		for(int j = 0 ; j < N ; j++){
			if( g(a+(b-a)*0.1,A[j],B[j],C[j]) > height + EPS ){
				idx = j;
				height = g(a+(b-a)*0.1,A[j],B[j],C[j]);
			}
		}
		if( idx >= 0 ){
			//cout <<  integral(a,b,A[idx],B[idx]) << endl;
			ans += integral(a,b,A[idx],B[idx]);
		}
		previous_x = cp[i];
	}
	printf("%.15Lf\n",ans);
}
accuracy: 1
time_ms: 50
memory_kb: 1380
final_rank: 4

code_snippet: #include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

#define double long double
double EPS = 1e-12;


double g(double x,double A,double B,double C){

	return A*x*x+B*x+C;
}
double f(double x,double A,double B){
	double t = 2*A*x+B;
	return sqrt(1+t*t);
	//return 0.5*(t*sqrt(t*t+1)+log(abs(t+sqrt(1+t*t))))/(2.*A);
}
double integral(double a,double b,double A,double B){
	double S = 1000;
	double d = (b-a) / S;
	double ans = 0;
	for(int i = 0 ; i < S ; i++){
		double aa = a + d * i;
		double bb = a + d * (i+1);
		ans += d * ( f(aa,A,B) + f((aa+bb)*0.5,A,B) + f(bb,A,B) ) / 3.0;
	}
	return ans;
	return f(b,A,B) - f(a,A,B);
}
vector<double> cp;
void crosspoint(double a,double b,double c){
	if( fabs(a) < EPS ){ cp.push_back(-c/b); return; }
	if( b*b-4*a*c < EPS ) return;
	cp.push_back((-b-sqrt(b*b-4*a*c))/(2*a));
	cp.push_back((-b+sqrt(b*b-4*a*c))/(2*a));
}
double A[100] = {} , P[100] = {} , Q[100] = {};
double B[100] , C[100] = {};
int main(){
	double W,H;
	int N;
	cin >> W >> H >> N;
	for(int i = 0; i < N; i++){
		cin >> A[i] >> P[i] >> Q[i];
		B[i] = -2.0*P[i]*A[i];
		C[i] = A[i] * P[i] * P[i] + Q[i];
		
	}
	for(int i = 0 ; i < N ; i++){
		crosspoint(A[i],B[i],C[i]);
	}
	
	for(int i = 0 ; i < N ; i++)
		for(int j = i+1 ; j < N ; j++)
			crosspoint(A[i]-A[j],B[i]-B[j],C[i]-C[j]);
	cp.push_back(0);
	cp.push_back(W);
	sort(cp.begin(),cp.end());
	double previous_x = 0;
	double ans = 0;
	for(int i = 0 ; i < cp.size() ; i++){
		if( cp[i] < -EPS || cp[i] > W + EPS) continue;
		double height = 0;
		int idx = -1;
		double a = previous_x;
		double b = cp[i];
		for(int j = 0 ; j < N ; j++){
			if( g(a+(b-a)*0.5,A[j],B[j],C[j]) > height + EPS ){
				idx = j;
				height = g(a+(b-a)*0.1,A[j],B[j],C[j]);
			}
		}
		if( idx >= 0 ){
			//cout <<  integral(a,b,A[idx],B[idx]) << endl;
			ans += integral(a,b,A[idx],B[idx]);
		}
		previous_x = cp[i];
	}
	printf("%.15Lf\n",ans);
}
accuracy: 0.14035087719298245
time_ms: 40
memory_kb: 1332
final_rank: 17

code_snippet: #include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

#define double long double
double EPS = 1e-12;


double g(double x,double A,double B,double C){

	return A*x*x+B*x+C;
}
double f(double x,double A,double B){
	double t = 2*A*x+B;
	return sqrt(1+t*t);
	//return 0.5*(t*sqrt(t*t+1)+log(abs(t+sqrt(1+t*t))))/(2.*A);
}
double integral(double a,double b,double A,double B){
	double S = 1000;
	double d = (b-a) / S;
	double ans = 0;
	for(int i = 0 ; i < S ; i++){
		double aa = a + d * i;
		double bb = a + d * (i+1);
		ans += d * ( f(aa,A,B) + f((aa+bb)*0.5,A,B) + f(bb,A,B) ) / 3.0;
	}
	return ans;
	return f(b,A,B) - f(a,A,B);
}
vector<double> cp;
void crosspoint(double a,double b,double c){
	if( fabs(a) < EPS ){ cp.push_back(-c/b); return; }
	if( b*b-4*a*c < EPS ) return;
	cp.push_back((-b-sqrt(b*b-4*a*c))/(2*a));
	cp.push_back((-b+sqrt(b*b-4*a*c))/(2*a));
}
double A[100] = {} , P[100] = {} , Q[100] = {};
double B[100] , C[100] = {};
int main(){
	double W,H;
	int N;
	cin >> W >> H >> N;
	for(int i = 0; i < N; i++){
		cin >> A[i] >> P[i] >> Q[i];
		B[i] = -2.0*P[i]*A[i];
		C[i] = A[i] * P[i] * P[i] + Q[i];
		
	}
	for(int i = 0 ; i < N ; i++){
		crosspoint(A[i],B[i],C[i]);
	}
	
	for(int i = 0 ; i < N ; i++)
		for(int j = i+1 ; j < N ; j++)
			crosspoint(A[i]-A[j],B[i]-B[j],C[i]-C[j]);
	cp.push_back(0);
	cp.push_back(W);
	sort(cp.begin(),cp.end());
	double previous_x = 0;
	double ans = 0;
	for(int i = 0 ; i < cp.size() ; i++){
		if( cp[i] < -EPS || cp[i] > W + EPS) continue;
		double height = 0;
		int idx = -1;
		double a = previous_x;
		double b = cp[i];
		for(int j = 0 ; j < N ; j++){
			if( g(a+(b-a)*0.5,A[j],B[j],C[j]) > height + EPS ){
				idx = j;
				height = g(a+(b-a)*0.5,A[j],B[j],C[j]);
			}
		}
		if( idx >= 0 ){
			//cout <<  integral(a,b,A[idx],B[idx]) << endl;
			ans += integral(a,b,A[idx],B[idx]);
		}
		previous_x = cp[i];
	}
	printf("%.15Lf\n",ans);
}
accuracy: 0.5614035087719298
time_ms: 40
memory_kb: 1376
final_rank: 15