2017 ACM-ICPC 亚洲区(南宁赛区)网络赛: F. Overlapping Rectangles(线段树)

来源:互联网 发布:淘宝卖家钱怎么到账 编辑:程序博客网 时间:2024/05/20 03:43

There are nn 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 AA with the bottom left corner located at (0, 0)(0,0) and the top right corner at (2, 2)(2,2), and the other rectangle BB with the bottom left corner located at (1,1)(1,1) and the top right corner at (3,3)(3,3), it follows that the area of the union of AA and BB should be 77, instead of 88.

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,0001,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 10001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a, b)(a,b), and the top right corner of the rectangle is located at (c, d)(c,d). Note that integers aabbccdd can be as large as 1,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n = 0n=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.

样例输入

20 0 2 21 1 3 330 0 1 12 2 3 34 4 5 50

样例输出

73*
思路:线段树求多个矩形合并的面积。

#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.0lf\n",ans);    }    return 0;}



阅读全文
1 0
原创粉丝点击