码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1151 Atlantis

时间:2017-05-08 23:18:45      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:double   uniq   blog   values   mission   plm   iss   bool   坐标   

Atlantis
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

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

Source

 
题意:求几个矩形覆盖的总面积
线段树+扫描线
离散化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

标签:double   uniq   blog   values   mission   plm   iss   bool   坐标   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/6828162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!