hdu 1542 Atlantis(扫描线)

Atlantis

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


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
 

Source

线段树扫描线的第一题,
下面附上一个感觉讲的比较好的链接
http://blog.csdn.net/xingyeyongheng/article/details/8927732
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=4*200+100;//数组大小要比原来大八倍
double X[maxn],sumv[maxn];
int mark[maxn],L,R;//记录某个区间的下底边个数
//以横坐标作为线段(区间),对横坐标线段进行扫描
//扫描的作用是每次更新下底边总长度和下底边个数,增加新面积
struct node{
    double l,r,h;
    int d;
    bool operator <(const node& rhs) const{
        return h<rhs.h;
    }
}a[maxn];

int search(double num,int l,int r){
    while(l<=r){
        int mid=(l+r)>>1;
        if(X[mid]==num)
            return mid;
        if(X[mid]<num)
            l=mid+1;
        else
            r=mid-1;
    }
    return -1;
}

void pushup(int rt,int l,int r){
    if(mark[rt])    //表示该区间整个线段长度可以作为底边
        sumv[rt]=X[r+1]-X[l];
    else if(l==r)//叶子结点则底边长度为0(区间内线段长度为0)
        sumv[rt]=0;
    else
        sumv[rt]=sumv[rt<<1]+sumv[rt<<1|1];
}

void update(int l,int r,int rt,int d){
    if(L<=l&&R>=r){//该区间是当前扫描线段的一部分,则该区间下底边总长以及上下底边个数差更新
        mark[rt]+=d;//更新底边相差差个数
        pushup(rt,l,r);//更新底边长
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m)
        update(lson,d);
    if(R>m)
        update(rson,d);
    pushup(rt,l,r);
}

int main(){
    int n,case1=0;
    double x1,x2,y1,y2;
    while(scanf("%d",&n)!=EOF){
        if(n==0)
            break;
        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            a[i*2-1]=node{x1,x2,y1,1},a[i*2]=node{x1,x2,y2,-1};
            X[i*2-1]=x1,X[i*2]=x2;
        }
        sort(a+1,a+2*n+1);
        sort(X+1,X+2*n+1);
        int m=unique(X+1,X+2*n+1)-X;//去重
        double ans=0;
        memset(mark,0,sizeof(mark));
        memset(sumv,0,sizeof(sumv));
        for(int i=1;i<=2*n;i++){
            L=search(a[i].l,1,m),R=search(a[i].r,1,m)-1;
            update(1,m,1,a[i].d);
            ans=ans+sumv[1]*(a[i+1].h-a[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++case1,ans);
    }
    return 0;
}
/*
这里注意下扫描线段时r-1:int R=search(s[i].l,hash,m)-1;
计算底边长时r+1:if(mark[n])sum[n]=hash[right+1]-hash[left];
解释:假设现在有一个线段左端点是l=0,右端点是r=m-1
则我们去更新的时候,会算到sum[1]=hash[mid]-hash[left]+hash[right]-hash[mid+1]
这样的到的底边长sum是错误的,why?因为少算了mid~mid+1的距离,由于我们这利用了
离散化且区间表示线段,所以mid~mid+1之间是有长度的,比如hash[3]=1.2,hash[4]=5.6,mid=3
所以这里用r-1,r+1就很好理解了
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值