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 <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

vector<double> seg;
int w,h,n;
int a[110],p[110],q[110];

vector<double> solve(double a,double b,double c){
	vector<double> result;
	// a == 0
	if(abs(a)<eps){
		if(abs(b)<eps) return result;
		result.push_back(-c/b);
		return result;
	}
	// a !=0
	double d=b*b-4.0*a*c;
	if(d>eps){
		double dd=sqrt(d);
		double ans1=(-b+dd)/(2.0*a);
		if(ans1>eps) result.pb(ans1);
		double ans2=(-b-dd)/(2.0*a);
		if(ans2>eps) result.pb(ans2);
	}else if(abs(d)<eps){
		double ans=-b/(2.0*a);
		if(ans>eps) result.pb(ans);
	}
	return result;
}

void cross(int i,int j){
	double aa=1.0*a[i]-1.0*a[j];
	double bb=-2.0*a[i]*p[i]+2.0*a[j]*p[j];
	double cc=1.0*a[i]*p[i]*p[i]-1.0*a[j]*p[j]*p[j]+q[i]-q[j];
	vector<double> res=solve(aa,bb,cc);
	for(auto &e:res) seg.push_back(e);
}

int top(double x){
	double cmax=0.0;
	int idx=-1;
	rep(i,n){
		double f=1.0*a[i]*(x-p[i])*(x-p[i])+q[i];
		if(cmax<f) cmax=f,idx=i;
	}
	return idx;
}

double calc(int idx,double x){
	x=abs(x-p[idx]);
	double res=0.0;
	res+=0.5*x*sqrt(4.0*a[idx]*a[idx]*x*x+1.0);
	res+=asinh(x*sqrt(4.0*a[idx]*a[idx]))/(2.0*sqrt(4.0*a[idx]*a[idx]));
	return res;
}

int main(void){
	cin >> w >> h >> n;
	rep(i,n) cin >> a[i] >> p[i] >> q[i];
	seg.push_back(0);
	seg.push_back(w);
	rep(i,n) seg.push_back(p[i]);
	rep(i,n){
		double d=sqrt(-1.0*q[i]/a[i]);
		seg.push_back(1.0*p[i]+d);
		seg.push_back(1.0*p[i]-d);
	}
	rep(j,n)rep(i,j) cross(i,j);
	sort(seg.begin(),seg.end());
	seg.erase(unique(seg.begin(),seg.end()),seg.end());
	int m=seg.size();
	double ans=0.0;
	rep(i,m-1){
		double left=seg[i],right=seg[i+1];
		if(right<=0||w<=left) continue;
		int idx=top((left+right)/2);
		if(idx==-1) continue;
		double cur=abs(calc(idx,left)-calc(idx,right));
		//cerr << idx << endl;
		//cerr << left << " " << right << " " << cur << endl;
		ans+=cur;
	}
	cout.precision(20);
	cout << fixed << ans << endl;
	return 0;
}