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

HDU_1542 Atlantis[线段树扫描线]

时间:2014-12-17 09:11:23      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   ar   io   color   os   sp   

传送门:HDU_1542

Atlantis


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

题意:求矩形并。

思路:线段树扫描线。

线段树维护左右区间大小,矩形下边标记为1,上边标记为-1,从下往上扫描。

参考:lv

代码:

/************************************************
* Author: Ac_sorry
* File:
* Create Date:
* Motto: One heart One life
* CSDN: http://blog.csdn.net/code_or_code
*************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<string>
#include<map>
#include<utility>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define seed_ 131
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof a)
#define flag(i) tree[i].flag
#define ls(i) tree[i].ls
#define rs(i) tree[i].rs
using namespace std;
typedef long long LL;

const int N=220;

struct Edge{
    double lx,rx,y;
    int flag;
    void init(double a,double b,double c,int d)
    {
        lx=a;rx=b;y=c;flag=d;
    }
    bool operator <(const Edge e) const
    {
        return y<e.y;
    }
}edge[N<<2];

struct Tree{
    int ls,rs;
    double len;
    int flag;
}tree[N<<2];

double X[N<<2];

void pushup(int p)
{
    if(flag(p))
        tree[p].len=X[rs(p)]-X[ls(p)];
    else if(rs(p)==ls(p)+1)
        tree[p].len=0;
    else
        tree[p].len=tree[p<<1].len+tree[p<<1|1].len;
}

void build(int p,int l,int r)
{
    tree[p].len=0;
    flag(p)=0;
    ls(p)=l;
    rs(p)=r;
    if(l+1==r) return;

    int m=(l+r)>>1;
    build(p<<1,l,m);
    build(p<<1|1,m,r);

}

void update(int p,int l,int r,int v)
{
    //cout<<"---\n";
    if(ls(p)>r||rs(p)<l) return;
    if(l<=ls(p)&&rs(p)<=r)
    {
        flag(p)+=v;
        pushup(p);
        return;
    }
    //int m=(ls(p)+rs(p))>>1;
    //if(l<=m)
        update(p<<1,l,r,v);
    //if(r>m)
        update(p<<1|1,l,r,v);
    pushup(p);
}


int main()
{
    //int T;scanf("%d",&T);
    int AC=0;
    int n;
    while(scanf("%d",&n)==1,n)
    {
        double x1,y1,x2,y2;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            edge[i].init(x1,x2,y1,-1);
            edge[n+i].init(x1,x2,y2,1);
            X[i]=x1;X[i+n]=x2;
        }
        n<<=1;
        sort(edge+1,edge+1+n);
        sort(X+1,X+1+n);
        int cnt=unique(X+1,X+1+n)-(X+1);
        build(1,1,cnt);
        double ans=0;
        printf("Test case #%d\n",++AC);
        for(int i=1;i<=n;i++)
        {

            if(i!=1)
                ans+=(edge[i].y-edge[i-1].y)*tree[1].len;
            int mp_l,mp_r;
            mp_l=lower_bound(X+1,X+1+cnt,edge[i].lx)-X;
            mp_r=lower_bound(X+1,X+1+cnt,edge[i].rx)-X;
            //cout<<cnt<<mp_l<<"--"<<mp_r<<endl;
            update(1,mp_l,mp_r,edge[i].flag);
        }
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}








HDU_1542 Atlantis[线段树扫描线]

标签:des   style   blog   http   ar   io   color   os   sp   

原文地址:http://blog.csdn.net/code_or_code/article/details/41971471

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