hdu 1542(线段树+扫描线 求矩形相交面积)

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12059    Accepted Submission(s): 5083

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.
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.
Sample Input
2
10 10 20 20
15 15 25 25.5
Sample Output
Test case #1
Total explored area: 180.00
Source
看了大神的解释 懂了一点 http://m.blog.csdn.net/article/details?id=52048323
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
double x[*N];
struct Edge{
double l,r;
double h;
int flag;
}edge[*N];
struct node{
int l,r;
int s;
double len;
}tree[N*];
bool cmp(Edge x,Edge y){
return x.h<y.h;
}
void pushup(int pos){
if(tree[pos].s)tree[pos].len=x[tree[pos].r+]-x[tree[pos].l];
else if(tree[pos].l==tree[pos].r)tree[pos].len=;
else{
tree[pos].len=tree[pos<<].len+tree[pos<<|].len;
}
}
void build(int l,int r,int pos){
tree[pos].l=l;tree[pos].r=r;
tree[pos].s=;tree[pos].len=;
if(tree[pos].l==tree[pos].r)return;
int mid=(tree[pos].l+tree[pos].r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
}
void update(int l,int r,int pos,int xx){
if(tree[pos].l==l&&tree[pos].r==r){
tree[pos].s=tree[pos].s+xx;
pushup(pos);
return;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(r<=mid)update(l,r,pos<<,xx);
else if(l>mid)update(l,r,pos<<|,xx);
else{
update(l,mid,pos<<,xx);
update(mid+,r,pos<<|,xx);
}
pushup(pos);
}
int main(){
int n;
int tt=;
while(scanf("%d",&n)!=EOF){
tt++;
if(n==)break;
int t=;
for(int i=;i<n;i++){
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Edge &t1=edge[t];Edge &t2=edge[t+];
t1.l=t2.l=x1;t1.r=t2.r=x2;
t1.h=y1;t2.h=y2;
t1.flag=;t2.flag=-;
x[t]=x1;x[t+]=x2;
t=t+;
}
sort(edge,t+edge,cmp);
sort(x,x+t);
int k=;
for(int i=;i<t;i++){
if(x[i]!=x[i-]){
x[k++]=x[i];
}
}
//cout<<1<<endl;
build(,k-,);
//cout<<2<<endl;
double ans=0.0;
//cout<<t<<endl;
for(int i=;i<t;i++){
int l=lower_bound(x,x+k,edge[i].l)-x;
int r=lower_bound(x,x+k,edge[i].r)-x-;
update(l,r,,edge[i].flag);
ans=ans+(edge[i+].h-edge[i].h)*tree[].len;
//cout<<ans<<endl;
}
printf("Test case #%d\n",tt);
printf("Total explored area: %.2f\n\n",ans);
//cout<<ans<<endl;
}
}
上一篇:Atlantis HDU - 1542 (线段树扫描线)


下一篇:asp.net core 2.0 api ajax跨域问题