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


你可能感兴趣的:(ACM,数据结构,线段树,扫描线)