| ||||||||||
如何参加“HDOJ暑期多校联合训练”?请加QQ群837241962咨询~ | ||||||||||
AtlantisTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22353 Accepted Submission(s): 8887 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.
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.
Sample Input 2 10 10 20 20 15 15 25 25.5 0
Sample Output Test case #1 Total explored area: 180.00
|
题意:给出一些矩形的左下坐标和右上坐标,求这些矩形的总面积。
多组测试用例。
使用线段树+扫描线 :
代码如下:
#include<bits/stdc++.h>
using namespace std;
int n;
double b[1000],ans;
struct point{
double l,r,h;
int f;
bool operator<(const point& p )const{
return h<p.h;
}
}e[2000];
struct Node{
int l,r,tag;
double len;
}t[2000];
void build(int k,int l,int r){
t[k].l=l;t[k].r=r;
t[k].len=0;t[k].tag=0;
if(l==r)return ;
int mid=(l+r)/2;
build(k*2,l,mid);
build(k*2+1,mid+1,r);//建立线段树
}
void pushup(int k){
if(t[k].tag) t[k].len=b[t[k].r+1]-b[t[k].l];
else if(t[k].l==t[k].r)t[k].len=0;
else t[k].len=t[k*2].len+t[k*2+1].len;
}
void update(int k,int l,int r,int w){
// cout<<k<<":"<<l<<" "<<r<<" "<<t[k].l<<" "<<t[k].r<<" "<<w<<endl;
if(l<=t[k].l&&t[k].r<=r){
t[k].tag+=w;
pushup(k);
return ;
}
int mid=(t[k].l+t[k].r)/2;
if(l<=mid)update(k*2,l,r,w);
if(mid<r)update(k*2+1,l,r,w);
pushup(k);
}
int main(){
int tot,cas;
while(scanf("%d",&n) != EOF && n){
int tot=1,k=2;
ans=0;
double x1,x2,y1,y2;
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
e[tot].l=e[tot+1].l=x1;
e[tot].r=e[tot+1].r=x2;
e[tot].h=y1;
e[tot+1].h=y2;
e[tot].f=1;
e[tot+1].f=-1;
b[tot]=x1;
b[tot+1]=x2;
tot+=2;
}
sort(e+1,e+tot);
sort(b+1,b+tot);
// for(int i=1;i<tot;i++)cout<<e[i].l<<" "<<e[i].r<<" "<<e[i].h<<" "<<e[i].f<<" "<<endl;
for(int i=2;i<tot;i++)
if(b[i]!=b[i-1])
b[k++]=b[i];
// for(int i=1;i<k;i++)cout<<b[i]<<" ";cout<<endl;
build(1,1,k);
for(int i=1;i<tot;i++){
int l=(int)(lower_bound(b+1,b+k,e[i].l)-b),r=(int)(lower_bound(b+1,b+k,e[i].r)-b-1);
// cout<<i<<": "<<e[i].l<<" "<<e[i].r<<" "<<l<<" "<<r<<endl;
update(1,l,r,e[i].f);
// cout<<i<<":"<<t[1].len<<" "<<e[i].h<<" "<<e[i+1].h<<" "<<e[i].f<<endl;
ans+=(e[i+1].h-e[i].h)*t[1].len;
// for(int i=1;i<=10;i++)cout<<t[i].l<<" "<<t[i].r<<" "<<t[i].tag<<" "<<t[i].len<<" "<<endl;
}
printf("Test case #%d\n",++cas);
printf("Total explored area: %.2lf\n\n",ans);
}
return 0;
}