题目链接:http://poj.org/problem?id=1389
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
经典的矩形面积并的题,用到了上个题poj1151的代码。(https://blog.csdn.net/qq_43472263/article/details/101104965
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson i<<1,l,m
#define rson i<<1|1,m+1,r
const int maxn=2050;
int x[maxn];
struct node
{
int l,r,h;//左右坐标,高度
int d;//标记上位边还是下位边
node() {}
node(int l,int r,int h,int d):l(l),r(r),h(h),d(d) {}
bool operator < (const node &a)const{
return h<a.h;
}
}line[maxn];
int cnt[maxn<<2];
int sum[maxn<<2];
void pushup(int i,int l,int r)
{
if(cnt[i])
sum[i]=x[r+1]-x[l];
else
sum[i]=sum[i<<1]+sum[i<<1|1];
}
void update(int ql,int qr,int v,int i,int l,int r)
{
if(ql<=l && qr>=r)
{
cnt[i]+=v;
pushup(i,l,r);
return ;
}
int m=(l+r)>>1;
if(ql<=m)update(ql,qr,v,lson);
if(qr>m)update(ql,qr,v,rson);
pushup(i,l,r);
}
int main()
{
int x1,y1,x2,y2;
while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2))
{
if(x1==-1&&y1==-1&&x2==-1&&y2==-1) break;
memset(cnt,0,sizeof(cnt));//相当于build
memset(sum,0,sizeof(sum));//相当于build
int n=0,m=0;
x[++n]=x1;
x[++n]=x2;
line[++m]=node(x1,x2,y1,1);
line[++m]=node(x1,x2,y2,-1);
while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)){
if(x1==-1&&y1==-1&&x2==-1&&y2==-1) break;
x[++n]=x1;
x[++n]=x2;
line[++m]=node(x1,x2,y1,1);
line[++m]=node(x1,x2,y2,-1);
}
sort(x+1,x+1+n);
sort(line+1,line+1+m);
int k=1;
k=unique(x+1,x+n+1)-x-1;//直接用STL中的unique函数。
int ans=0;
for(int i=1; i<m; i++)
{
int l=lower_bound(x+1,x+k+1,line[i].l)-x;
int r=lower_bound(x+1,x+k+1,line[i].r)-x;
r--;
if(l<=r)update(l,r,line[i].d,1,1,k-1);
ans+=sum[1]*(line[i+1].h-line[i].h);
}
printf("%d\n",ans);
}
}