Problem: One

Problem Statement

A beautiful mountain range can be seen from the train window.
The window is a rectangle with the coordinates of the lower left corner (0, 0) and the coordinates of the upper right corner (W, H).
N peaks can be seen from the window, and the i-th peak has the shape of an upwardly convex parabola y = a_i (x-p_i) ^ 2 + q_i.
Find the length of the boundary between the mountain and the sky.

The following three figures correspond to Sample Input. The part shown by the thick line is the boundary between the mountain and the sky.

<image> <image> <image>

Constraints

* 1 ≤ W, H ≤ 100
* 1 ≤ N ≤ 50
* -100 ≤ a_i ≤ -1
* 0 ≤ p_i ≤ W
* 1 ≤ q_i ≤ H
* If i \ neq j, then (a_i, p_i, q_i) \ neq (a_j, p_j, q_j)

Input

Input follows the following format. All given numbers are integers.


W H N
a_1 p_1 q_1
...
a_N p_N q_N

Output

Output the length of the boundary between the mountain and the sky on one line.
The output value must have an absolute or relative error of less than 10 ^ {-6} with the true value.

Examples

Input

20 20 1
-1 10 10


Output

21.520346288593280


Input

20 20 2
-1 10 10
-2 10 5


Output

21.520346288593280


Input

15 100 2
-2 5 100
-2 10 100


Output

126.921542730127873

Solution (in CPP):

Solution:
#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;
}