首页 > 代码库 > poj 1151 Atlantis

poj 1151 Atlantis

Atlantis
http://poj.org/problem?id=1151
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22662 Accepted: 8478

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 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

210 10 20 2015 15 25 25.50

Sample Output

Test case #1Total explored area: 180.00 

Source

Mid-Central European Regional Contest 2000
 
题意:求几个矩形覆盖的总面积
线段树+扫描线
离散化x坐标,扫描线自下而上扫
标记矩形下边界为1,上边界为-1,线段树维护当前这根线的长度
线段树的每个节点代表的是这个节点右边一小段的长度
所以修改区间[l,r]是修改区间[l,r-1]
每次的修改up上去,计算使用1号点的sum,
这样可以避免标记的下传
解释一下up函数:
如果这个区间有标记,即区间完全被矩形边界覆盖,无论标记有多少,他的长度只能是区间长度
如果这个区间没有标记,即区间部分被矩形边界覆盖,就由子节点合并
叶子节点没有子节点,所以特判
#include<cstdio>#include<algorithm>using namespace std;int n,cnt;int opl,opr,w;double x1[101],x2[101],y1[101],y2[101],hash[201],ans;struct LINE{    int xl,xr,f;    double h;}line[201];struct node{    int l,r,f;    double sum;}tr[201*4];bool cmp(LINE p,LINE q){    return p.h<q.h;}void build(int k,int l,int r){    tr[k].l=l; tr[k].r=r;    if(l==r) return;    int mid=l+r>>1;    build(k<<1,l,mid);    build(k<<1|1,mid+1,r);}void up(int k){    if(tr[k].f) tr[k].sum=hash[tr[k].r+1]-hash[tr[k].l];    else if(tr[k].l==tr[k].r) tr[k].sum=0;    else tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;}void change(int k){    if(tr[k].l>=opl&&tr[k].r<=opr)    {        tr[k].f+=w;        up(k);        return;    }    int mid=tr[k].l+tr[k].r>>1;    if(opl<=mid) change(k<<1);    if(opr>mid) change(k<<1|1);    up(k);}int main(){    int T=0;    while(scanf("%d",&n)!=EOF)    {        if(!n) return 0;        T++; cnt=0; ans=0;        for(int i=1;i<=n;i++)         {            scanf("%lf%lf%lf%lf",&x1[i],&y1[i],&x2[i],&y2[i]);            hash[++cnt]=x1[i]; hash[++cnt]=x2[i];        }        sort(hash+1,hash+cnt+1);        cnt=unique(hash+1,hash+cnt+1)-(hash+1);        for(int i=1;i<=n;i++)        {            line[i*2-1].xl=lower_bound(hash+1,hash+cnt+1,x1[i])-hash;            line[i*2].xl=line[i*2-1].xl;            line[i*2-1].xr=lower_bound(hash+1,hash+cnt+1,x2[i])-hash;            line[i*2].xr=line[i*2-1].xr;            line[i*2-1].h=y1[i];             line[i*2].h=y2[i];            line[i*2-1].f=1;            line[i*2].f=-1;        }        sort(line+1,line+2*n+1,cmp);        build(1,1,cnt);        for(int i=1;i<=2*n;i++)        {            opl=line[i].xl; opr=line[i].xr-1;            w=line[i].f;            change(1);            ans+=tr[1].sum*(line[i+1].h-line[i].h);        }        printf("Test case #%d\nTotal explored area: %.2lf\n\n",T,ans);    }}

 

poj 1151 Atlantis