POJ 1389 Area of Simple Polygons (선분 수 + 스캐닝 라인 + 이산 화)

There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates. 
Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point. 
Example: Consider the following three rectangles: 
rectangle 1: < (0, 0) (4, 4) >, 
rectangle 2: < (1, 1) (5, 2) >, 
rectangle 3: < (1, 1) (2, 5) >. 
The total area of all simple polygons constructed by these rectangles is 18. 
Input
The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.
Output
For each test case, output the total area of all simple polygons in a line. 
Sample Input
0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1  
Sample Output
18
10 

문제 풀이:
스캐닝 선의 누 드 문제.근 데 이 문 제 는 데이터 범위 가 작은 것 같 아서 이산 화 안 해도 돼 요.어차피 다 썼 으 니 면적 을 구 하 는 틀 커버 는 한 문제 의 쓰기 와 거의 같다.여 기 는 설명 안 할 게 요.
원 리 를 모 르 면
코드:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct node
{
    int l,r;
    int len;
    int s;
}t[2005*4];
int x[2005];//        
struct bian
{
    int l,r;//      
    int h;//    
    int d;//    ,      ,   1,   -1
}a[2005];
int cmp(bian x,bian y)//         
{
    return x.hmid)
        update(l,r,k*2+1,d);
    else
    {
        update(l,mid,k*2,d);
        update(mid+1,r,k*2+1,d);
    }
    pushup(k);
}
int main()
{
    int i,j,k,n,m,x1,x2,y1,y2,tot=0;
    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
    {
        if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
            break;
        a[tot].l=x1;
        a[tot+1].l=x1;
        a[tot].r=x2;
        a[tot+1].r=x2;
        a[tot].h=y1;
        a[tot+1].h=y2;
        a[tot].d=1;
        a[tot+1].d=-1;
        x[tot]=x1;
        x[tot+1]=x2;
        tot+=2;//      
        while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
        {
            if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
            {
                sort(a,a+tot,cmp);
                sort(x,x+tot);//       
                k=unique(x,x+tot)-x;//     
                Build(0,k-1,1);
                int s=0;
                for(i=0;i

좋은 웹페이지 즐겨찾기