poj 1389 Area of Simple Polygons(线段树+扫描线)

Area of Simple Polygons
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3499   Accepted: 1805

Description

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 

Source

Taiwan 2001

[Submit]   [Go Back]   [Status]   [Discuss]



题解:线段树+扫描线

 将矩形的左右边界按横坐标排序,对于纵坐标用线段树维护被覆盖的点数。

但是覆盖的时候不标记下放,这样小线段还保留着之前的结果,所以删线段的时候不会影响之前的小线段,就可以直接用小线段更新答案。

#include
#include
#include
#include
#include
#define N 100000
#define LL long long
using namespace std;
int tree[N*4],delta[N*4],tot,cnt;
LL len[N*4];
struct data
{
	int l,r,x,pd;
}a[N];
void update(int now,int l,int r)
{
	if (tree[now]>0)
	 len[now]=(LL)r-l+1;
	else  if (l==r)  len[now]=0;
    else len[now]=len[now<<1]+len[now<<1|1]; 
}
void qjchange(int now,int l,int r,int ll,int rr,int v)
{
	if (l>=ll&&r<=rr)
	 {
	 	tree[now]+=v; update(now,l,r);
	 	return;
	 }
	int mid=(l+r)/2;
	if (ll<=mid) qjchange(now<<1,l,mid,ll,rr,v);
	if (rr>mid) qjchange(now<<1|1,mid+1,r,ll,rr,v);
	update(now,l,r);
}
int cmp(data a,data b)
{
	return a.x


你可能感兴趣的:(线段树)