线段树+扫描线
题目链接
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): 22435 Accepted Submission(s): 8919
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
0
Sample Output
Test case #1
Total explored area: 180.00
题解:
线段树+扫描线
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=200+5;
const double eps=1e-3;
double sum[maxn<<2],ha[maxn];
int tag[maxn<<2];
struct seg
{
double l,r,h;
int v;
seg(double a,double b,double c,int vl):l(a),r(b),h(c),v(vl){}
seg(){}
friend bool operator<(seg s1,seg s2)
{
return s1.h<s2.h;
}
}s[maxn];
int bs(double x,int n)
{
int l=0,r=n-1;
while(l<=r)
{
int m=l+r>>1;
if(fabs(ha[m]-x)<=eps)return m;
else if(ha[m]>x) r=m-1;
else l=m+1;
}
return -1;
}
void updatef(int o,int l,int r)
{
if(tag[o])sum[o]=ha[r+1]-ha[l];
else if(l==r)sum[o]=0;
else sum[o]=sum[o<<1]+sum[o<<1|1];
}
void update(int ql,int qr,int v,int o,int l,int r)
{
if(ql<=l&&qr>=r)
{
tag[o]+=v;
updatef(o,l,r);
return;
}
int m=l+r>>1;
if(ql<=m)update(ql,qr,v,o<<1,l,m);
if(qr>m)update(ql,qr,v,o<<1|1,m+1,r);
updatef(o,l,r);
}
int main()
{
int n,m,k,kase=0;
while(scanf("%d",&n)&&n)
{
memset(tag,0,sizeof(tag));
memset(sum,0,sizeof(sum));
m=1;k=0;
for(int i=0;i<n;i++)
{
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
ha[k]=a;
s[k++]=seg(a,c,b,1);
ha[k]=c;
s[k++]=seg(a,c,d,-1);
}
sort(ha,ha+k);
sort(s,s+k);
for(int i=1;i<k;i++)
{
if(ha[i]!=ha[i-1])ha[m++]=ha[i];
}
double ans=0;
for(int i=0;i<k;i++)
{
int l=bs(s[i].l,m);
int r=bs(s[i].r,m)-1;
update(l,r,s[i].v,1,0,m-1);
ans+=sum[1]*(s[i+1].h-s[i].h);
}
printf("Test case #%d\n",++kase);
printf("Total explored area: %.2lf\n\n",ans);
}
}
上一篇: phpcms QQ登录无法正常登录
下一篇: 【线段树+扫描线】