暑期集训4 I题
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1542
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10824 Accepted Submission(s): 4601
Problem Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
题意:给你n个矩形,求这n个矩形所覆盖的图形的面积。
题解:用线段树扫描线,注意坐标是小数所以要进行离散化
可以按y轴从上往下扫,对x轴进行离散化
遇到上边加一,遇到下边减一
注意线段树存的是边而不是点
(注意每组数据输出后加回车...)
下面代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
double x1,x2,y;
int l;
}xian[50005];
struct xiantree
{
int l,r,ans;
double sum;
}tree[50005];
double x[50005];
int k,m;
bool cmp(node a,node b)
{
return a.y<b.y;
}
void build(int l,int r,int root)
{
int mid;
tree[root].l=l;
tree[root].r=r;
tree[root].sum=0;
tree[root].ans=0;
if(l==r) return;
mid=(l+r)/2;
build(l,mid,root*2);
build(mid+1,r,root*2+1);
return;
}
void pushup(int root)
{
if(tree[root].ans!=0) tree[root].sum=x[tree[root].r+1]-x[tree[root].l];
else if(tree[root].l==tree[root].r) tree[root].sum=0;
else tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}
void update(int l,int r,int kk,int root)
{
if(l<=tree[root].l && r>= tree[root].r)
{
tree[root].ans+=kk;
pushup(root);
return;
}
int mid=(tree[root].l+tree[root].r)/2;
if(l<=mid) update(l,r,kk,root*2);
if(mid<r) update(l,r,kk,root*2+1);
pushup(root);
}
int main()
{
int n,i,t=0;
double x1,y1,x2,y2,ans;
while(~scanf("%d",&n))
{
t++;
if(n==0) break;
k=0;
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
xian[k].x1=x1;
xian[k].x2=x2;
xian[k].y=y1;
xian[k].l=1;
x[k++]=x1;
xian[k].x1=x1;
xian[k].x2=x2;
xian[k].y=y2;
xian[k].l=-1;
x[k++]=x2;
}
sort(x,x+k);
sort(xian,xian+k,cmp);
m=1;
for(i=1;i<k;i++)
if(x[i]!=x[i-1]) x[m++]=x[i];
build(0,m-2,1);
ans=0;
for(i=0;i<k-1;i++)
{
int l=lower_bound(x,x+m,xian[i].x1)-x;
int r=lower_bound(x,x+m,xian[i].x2)-x-1;
if(l<=r)
update(l,r,xian[i].l,1);
ans+=tree[1].sum*(xian[i+1].y-xian[i].y);
}
printf("Test case #%d\n",t);
printf("Total explored area: %.2lf\n\n",ans);
}
return 0;
}