Advertisement
Guest User

Untitled

a guest
May 26th, 2018
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const double EPS = 1e-9;
  6.  
  7. struct PT {
  8.   double x, y;
  9.   PT() {}
  10.   PT(double x, double y) : x(x), y(y) {}
  11.   PT(const PT &p) : x(p.x), y(p.y)    {}
  12.   PT operator + (const PT &p)  const { return PT(x+p.x, y+p.y); }
  13.   PT operator - (const PT &p)  const { return PT(x-p.x, y-p.y); }
  14.   PT operator * (double c)     const { return PT(x*c,   y*c  ); }
  15.   PT operator / (double c)     const { return PT(x/c,   y/c  ); }
  16. };
  17.  
  18. double dot(PT p, PT q)     { return p.x*q.x+p.y*q.y; }
  19. double dist2(PT p, PT q)   { return dot(p-q,p-q); }
  20. double cross(PT p, PT q)   { return p.x*q.y-p.y*q.x; }
  21.  
  22. PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
  23.   b=b-a; d=c-d; c=c-a;
  24.   assert(dot(b, b) > EPS && dot(d, d) > EPS);
  25.   return a + b*cross(c, d)/cross(b, d);
  26. }
  27.  
  28. const int N = 101010;
  29. int n;
  30. PT a[N];
  31.  
  32. double area(vector<PT>pol){
  33. double sum=0.0;
  34. for(int i=0;i<pol.size();i++)
  35.     sum += cross( pol[i] , pol[ (i+1)%pol.size() ] );
  36. return fabs(sum)/2.0;
  37. }
  38.  
  39. void solve(){
  40.  
  41. double mxY=-1;
  42.  
  43. scanf("%d",&n);
  44. for(int i=1;i<=n;i++){
  45.         int _xx,_yy;
  46.         scanf("%d %d",&_xx,&_yy);
  47.         a[i]=PT(_xx,_yy);
  48.         mxY=max(mxY,a[i].y);
  49.     }
  50. int idx1,idx2;
  51. for(int i=1;i<=n;i++)
  52.     if( a[i].y == mxY ){
  53.         idx1=i;
  54.         break;
  55.     }
  56.  
  57. for(int i=n;i>0;i--)
  58.     if( a[i].y==mxY ){
  59.         idx2=i;
  60.         break;
  61.     }
  62.  
  63. double ans=0.0;
  64.  
  65. int j=1;
  66. for(int i=2;i<=idx1;i++){
  67.  
  68.     if( a[j].y <= a[i].y ){
  69.  
  70.         if( j+1<i ){
  71.           vector<PT>pol;
  72.           for(int k=j;k<i;k++)pol.push_back( a[k] );
  73.           pol.push_back( ComputeLineIntersection(a[j],PT(1e9,a[j].y),a[i-1],a[i]) );
  74.           ans += area(pol);
  75.         }
  76.         j=i;
  77.     }
  78. }
  79.  
  80. j=n;
  81. for(int i=n-1;i>=idx2;i--){
  82.  
  83.     if( a[j].y <= a[i].y ){
  84.  
  85.         if( j>i+1 ){
  86.           vector<PT>pol;
  87.           for(int k=j;k>i;k--)pol.push_back( a[k] );
  88.           pol.push_back( ComputeLineIntersection(a[j],PT(-1e9,a[j].y),a[i],a[i+1]) );
  89.           ans += area(pol);
  90.         }
  91.         j=i;
  92.     }
  93. }
  94.  
  95. j=idx1;
  96. for(int i=idx1+1;i<=idx2;i++)
  97. if( a[i].y == mxY ){
  98.  
  99.     if( j+1<i ){
  100.           vector<PT>pol;
  101.           for(int k=j;k<=i;k++)pol.push_back( a[k] );
  102.           ans += area(pol);
  103.     }
  104.     j=i;
  105. }
  106. printf("%.8f\n",ans);
  107. }
  108.  
  109. int main(){
  110.  
  111. int t;
  112. cin>>t;
  113. while( t-- )solve();
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement