概述
There are n rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle A with the bottom left corner located at (0,0) and the top right corner at (2,2), and the other rectangle B with the bottom left corner located at (1,1) and the top right corner at (3,3), it follows that the area of the union of A and B should be 7, instead of 8.
Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.
Note:
(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,000.
(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.
Input Format
Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 1000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b), and the top right corner of the rectangle is located at (c,d). Note that integers a, b, c, d can be as large as 1,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0 (zero) signifies the end of input.
Output Format
For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.
样例输入
2 0 0 2 2 1 1 3 3 3 0 0 1 1 2 2 3 3 4 4 5 5 0
样例输出
7 3 *
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+10;
//求面积
struct Point
{
double x1,y1,x2,y2;
}p[MAX];
struct edg
{
int in;
double x,y1,y2;
}e[MAX];
double s[MAX];
struct lenka
{
double l,r,sum;
int lazy;
//线段覆盖次数
}a[MAX<<4];
void build(int k,int l,int r)
{
a[k].l=s[l],a[k].r=s[r];
a[k].sum=0;a[k].lazy=0;
if(l+1==r)return;
build(2*k,l,(l+r)/2);
build(2*k+1,(l+r)/2,r);
}
void change(int k,double l,double r,int tag)
{
if(a[k].l==l&&a[k].r==r)
{
a[k].lazy+=tag;
if(a[k].lazy>0)a[k].sum=a[k].r-a[k].l;
else a[k].sum=a[2*k].sum+a[2*k+1].sum;
return;
}
if(r<=a[2*k].r)change(2*k,l,r,tag);
else if(l>=a[2*k+1].l)change(2*k+1,l,r,tag);
else change(2*k,l,a[2*k].r,tag),change(2*k+1,a[2*k+1].l,r,tag);
if(a[k].lazy>0)a[k].sum=a[k].r-a[k].l;
else a[k].sum=a[2*k].sum+a[2*k+1].sum;
}
int cmp(const edg& p,const edg& q){return p.x<q.x;}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0){puts("*");break;}
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
e[2*i].x=p[i].x1;
e[2*i].y1=p[i].y1;
e[2*i].y2=p[i].y2;
e[2*i].in=1;
//矩形的入边
e[2*i+1].x=p[i].x2; e[2*i+1].y1=p[i].y1; e[2*i+1].y2=p[i].y2; e[2*i+1].in=-1; //矩形的出边
s[2*i]=p[i].y1;
//将y坐标存储下来进行离散化
s[2*i+1]=p[i].y2;
}
sort(e,e+2*n,cmp);
sort(s,s+2*n);
build(1,0,2*n-1);
double ans=0;
change(1,e[0].y1,e[0].y2,e[0].in);
for(int i=1;i<2*n;i++)
{
ans+=a[1].sum*(e[i].x-e[i-1].x);
change(1,e[i].y1,e[i].y2,e[i].in);
}
printf("%0.0lfn",ans);
}
return 0;
}
最后
以上就是怕孤独香氛为你收集整理的2017 ACM-ICPC 亚洲区(南宁赛区)网络赛: F. Overlapping Rectangles(线段树)的全部内容,希望文章能够帮你解决2017 ACM-ICPC 亚洲区(南宁赛区)网络赛: F. Overlapping Rectangles(线段树)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复