[bzoj2618] [Half plane turn] [Cqoi2006] Convex polygon __bzoj

Source: Internet
Author: User

2618: [Cqoi2006] Convex polygon

Time Limit:5 Sec Memory limit:128 MB
submit:1920 solved:955
[Submit] [Status] [Discuss]
Description

The vertex coordinates of n convex polygons are given counterclockwise, and the area of their intersection is obtained. For example, when n=2, two convex polygons are shown below:

The area of the intersecting part is 5.233.
Input

The first line has an integer n, which indicates the number of polygons, and the following describes each polygon in turn. The first line of the I polygon contains an integer mi that represents the number of sides of the polygon, the following MI line is two integers per line, and the coordinates of each vertex are counterclockwise.

Output

The output file contains only a real number that represents the area of the intersection and retains three decimal places.

Sample Input

2

6

-2 0

-1-2

1-2

2 0

1 2

-1 2

4

0-3

1-1

2 2

-1 0

Sample Output

5.233
HINT

100% Data satisfaction: 2<=n<=10,3<=mi<=50, integers in each dimension coordinates [ -1000,1000]

Source Sol:

Specific to see this blog.
Https://www.cnblogs.com/XDJjy/archive/2013/09/13/3320205.html

So these are the points that are OK, if only a number of ax+by+c>=0 are given. First of all, you take a random point, >=0 time vector to take b,-a,<=0 vector-b,a. This from

In fact, it is similar to the process of seeking a convex package. is to judge the two vectors of the right hand when not wrong on the line.

#include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <
cstdlib> #include <cmath> #include <iostream> using namespace std;
typedef double S64;
int n,m;
    inline int read () {char C;
    int res,flag=0; while ((C=getchar ()) > ' 9 ' | |
    c< ' 0 ') if (c== '-') flag=1;
    res=c-' 0 ';
    while ((C=getchar ()) >= ' 0 ' &&c<= ' 9 ') res= (res<<3) + (res<<1) +c-' 0 ';
return flag?-res:res;
const int n=510;
    struct P {s64 x,y;
    Friend inline P operator + (const p &AMP;A,CONST p &b) {return (P) {A.X+B.X,A.Y+B.Y};
    Friend inline P operator-(const p &AMP;A,CONST p &b) {return (P) {A.X-B.X,A.Y-B.Y};
    Friend inline P operator * (const P &AMP;A,S64 b) {return (P) {a.x*b,a.y*b};
    Friend Inline S64 operator * (const p &AMP;A,CONST p &b) {return a.x*b.y-a.y*b.x; Friend inline S64 operator/(const p &AMP;A,CONST p &b) {return a. x*b.x+a.y*b.y;
}}a[n];
    struct L {P a,b,v;
    S64 ang; Friend inline bool operator < (const l &AMP;A,CONST l &b) {return A.ang<b.ang| |
    a.ang==b.ang&&a.v* (B.B-A.A) >0;
        Friend Inline P Inter (const L &AMP;A,CONST l &b) {p nw=b.a-a.a;
        S64 tt= (NW*A.V)/(A.V*B.V);
    return B.A+B.V*TT;
    Friend inline bool Jud (const P &a,const L &b) {return b.v* (A-B.A) <0;
}}l[n],q[n];
int cnt;
int tot;
    inline void Solve () {n=read ();
        for (int i=1;i<=n;++i) {m=read ();
        P ST,LA,NW;
        scanf ("%lf%lf", &AMP;ST.X,&AMP;ST.Y);
        la=st;
            for (int j=2;j<=m;++j) {scanf ("%lf%lf", &nw.x,&nw.y);
            L[++cnt]= (L) {La,nw,nw-la};
        LA=NW;
    } l[++cnt]= (L) {La,st,st-la};
    for (int i=1;i<=cnt;++i) l[i].ang=atan2 (l[i].v.y,l[i].v.x);
    Sort (l+1,l+1+cnt); for (int i=1;i<=cnt;++i) {
        if (L[i].ang!=l[i-1].ang) ++tot;
    L[tot]=l[i];
    } Cnt=tot;
    tot=0;
    int l=1,r=2;
    Q[1]=L[1];Q[2]=L[2];
        for (int i=3;i<=cnt;++i) {while (L<r&&jud (Inter (Q[R-1],Q[R)), l[i])--r;
        while (L<r&&jud (Inter (Q[L+1],Q[L), l[i]) ++l;
    Q[++r]=l[i];
    while (L<r&&jud (Inter (Q[R-1],Q[R)), q[l])--r;
    while (L<r&&jud (Inter (Q[L+1],Q[L), q[r]) ++l;
    Q[R+1]=Q[L];
for (int i=l;i<=r;++i) A[++tot]=inter (q[i],q[i+1]);
S64 ans;
    inline void Calc () {for (int i=1;i<tot;++i) ans+=a[i]*a[i+1];
    ANS+=A[TOT]*A[1];
    if (tot<3) ans=0; 
printf ("%.3lf", ANS/2);
    int main () {//Freopen ("2618.in", "R", stdin);//Freopen (". Out", "w", stdout);
    Solve ();
Calc ();
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.