# Ellipse

## 题目描述

Math is important!! Many students failed in 2+2’s mathematical test, so let’s AC this problem to mourn for our lost youth..
Look at this sample picture:

An ellipse in a plane centered on point $O$. The $L, R$ lines will be vertical through the $X$-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have to turn to you, a talent of programmer. Your task is telling me the result of calculation. (defined $\pi=3.14159265$, the area of an ellipse $A=\pi ab$)

## 算法分析

$$\int_a^b f(x) \, {\rm d}x \approx {b-a \over 6} \left(f(a)+4f\left({a+b \over 2}\right)+f(b)\right)$$

$$g(l, r)=\int_l^r f(x) \, {\rm d}x, \; h(l, r)={r-l \over 6} \left(f(l)+4f\left({l+r \over 2}\right)+f(r)\right)$$

$$g(l, r)=h(l, r)$$

$$g(l, r)=g\left(l, {l+r \over 2}\right)+g\left({l+r \over 2}, r\right)$$

## 代码

/*
* Today is what happened to yesterday.
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

static double const EPS = 1e-8;
int a, b;

double get_y(double x) { return 2 * b * sqrt((1 - x * x / a / a)); }

double get_s(double l, double r) {
return (get_y(l) + 4 * get_y((l + r) / 2) + get_y(r)) * (r - l) / 6;
}

double calc(double l, double r) {
double mid = (l + r) / 2;
if (fabs(get_s(l, r) - get_s(l, mid) - get_s(mid, r)) < EPS)
return get_s(l, r);
return calc(l, mid) + calc(mid, r);
}

int main() {
int T;
scanf("%d", &T);
for (; T--;) {
int l, r;
scanf("%d%d%d%d", &a, &b, &l, &r);
printf("%.3lf\n", calc(l, r));
}
return 0;
}


418 I'm a teapot