Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const double EPS = 1e-9;
- struct PT {
- double x, y;
- PT() {}
- PT(double x, double y) : x(x), y(y) {}
- PT(const PT &p) : x(p.x), y(p.y) {}
- PT operator + (const PT &p) const { return PT(x+p.x, y+p.y); }
- PT operator - (const PT &p) const { return PT(x-p.x, y-p.y); }
- PT operator * (double c) const { return PT(x*c, y*c ); }
- PT operator / (double c) const { return PT(x/c, y/c ); }
- };
- double dot(PT p, PT q) { return p.x*q.x+p.y*q.y; }
- double dist2(PT p, PT q) { return dot(p-q,p-q); }
- double cross(PT p, PT q) { return p.x*q.y-p.y*q.x; }
- PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
- b=b-a; d=c-d; c=c-a;
- assert(dot(b, b) > EPS && dot(d, d) > EPS);
- return a + b*cross(c, d)/cross(b, d);
- }
- const int N = 101010;
- int n;
- PT a[N];
- double area(vector<PT>pol){
- double sum=0.0;
- for(int i=0;i<pol.size();i++)
- sum += cross( pol[i] , pol[ (i+1)%pol.size() ] );
- return fabs(sum)/2.0;
- }
- void solve(){
- double mxY=-1;
- scanf("%d",&n);
- for(int i=1;i<=n;i++){
- int _xx,_yy;
- scanf("%d %d",&_xx,&_yy);
- a[i]=PT(_xx,_yy);
- mxY=max(mxY,a[i].y);
- }
- int idx1,idx2;
- for(int i=1;i<=n;i++)
- if( a[i].y == mxY ){
- idx1=i;
- break;
- }
- for(int i=n;i>0;i--)
- if( a[i].y==mxY ){
- idx2=i;
- break;
- }
- double ans=0.0;
- int j=1;
- for(int i=2;i<=idx1;i++){
- if( a[j].y <= a[i].y ){
- if( j+1<i ){
- vector<PT>pol;
- for(int k=j;k<i;k++)pol.push_back( a[k] );
- pol.push_back( ComputeLineIntersection(a[j],PT(1e9,a[j].y),a[i-1],a[i]) );
- ans += area(pol);
- }
- j=i;
- }
- }
- j=n;
- for(int i=n-1;i>=idx2;i--){
- if( a[j].y <= a[i].y ){
- if( j>i+1 ){
- vector<PT>pol;
- for(int k=j;k>i;k--)pol.push_back( a[k] );
- pol.push_back( ComputeLineIntersection(a[j],PT(-1e9,a[j].y),a[i],a[i+1]) );
- ans += area(pol);
- }
- j=i;
- }
- }
- j=idx1;
- for(int i=idx1+1;i<=idx2;i++)
- if( a[i].y == mxY ){
- if( j+1<i ){
- vector<PT>pol;
- for(int k=j;k<=i;k++)pol.push_back( a[k] );
- ans += area(pol);
- }
- j=i;
- }
- printf("%.8f\n",ans);
- }
- int main(){
- int t;
- cin>>t;
- while( t-- )solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement