zoukankan      html  css  js  c++  java
  • 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.

    题目大意:给出N个矩形,求所有矩形覆盖到的面积
    解题报告:
    扫描线SBT,我们按y离散化一边,我们把矩形拆成两条线段,进和出,对应线段树中的加减,然后按找x从小大到顺序枚举,每次贡献的答案就是:((a[i].x-a[i-1].x)*len[1])其中(len[1])为处于(a[i-1].x)(a[i].x)之间的线段长度总和,len用线段树维护即可

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define ls (node<<1)
    #define rs (node<<1|1)
    using namespace std;
    const int N=2005;
    int n,m=0,num=0;double b[N];
    struct node{
    	double l,r,x;int d;
    	node(){}
    	node(double _l,double _r,int _d,double _x){
    		l=_l;r=_r;d=_d;x=_x;
    	}
    	bool operator <(const node &pp)const{
    		return x<pp.x;
    	}
    }a[N];
    int Tree[N<<2];double len[N<<2];
    void upd(int node,int l,int r){
    	if(Tree[node])len[node]=b[r+1]-b[l];
    	else if(l==r)len[node]=0;
    	else len[node]=len[ls]+len[rs];
    }
    void updata(int l,int r,int node,int sa,int se,int to){
    	if(l>se || r<sa)return ;
    	if(sa<=l && r<=se){
    		Tree[node]+=to;upd(node,l,r);
    		return ;
    	}
    	int mid=(l+r)>>1;
    	updata(l,mid,ls,sa,se,to);updata(mid+1,r,rs,sa,se,to);
    	upd(node,l,r);
    }
    void Clear(){
    	m=num=0;int tmp=N<<2;
    	for(RG int i=0;i<tmp;i++)Tree[i]=len[i]=0;
    }
    void work(int kase)
    {
    	Clear();
    	double x,y,x2,y2;
    	for(int i=1;i<=n;i++){
    		scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
    		a[++m]=node(y,y2,1,x);
    		a[++m]=node(y,y2,-1,x2);
    		b[++num]=y;b[++num]=y2;
    	}
    	sort(a+1,a+m+1);sort(b+1,b+num+1);
    	int tot=unique(b+1,b+num+1)-b-1;
    	for(int i=1;i<=m;i++){
    		a[i].l=lower_bound(b+1,b+tot+1,a[i].l)-b;
    		a[i].r=lower_bound(b+1,b+tot+1,a[i].r)-b;
    	}
    	double ans=0;tot--;
    	for(int i=1;i<=m;i++){
    		if(i>1)ans+=(a[i].x-a[i-1].x)*len[1];
    		updata(1,tot,1,a[i].l,a[i].r-1,a[i].d);
    	}
    	printf("Test case #%d
    ",kase);
    	printf("Total explored area: %.2lf
    
    ",ans);
    }
    
    int main()
    {
    	int kase=0;
    	while(~scanf("%d",&n)){
    		if(!n)break;
    		work(++kase);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    获取一个目录下的所有文件 (转载)
    成为一个合格程序员的十三条原则(转载)
    VC消息机制总结
    域名是http和https都可以访问;但是http访问,就没法存储session:https就可以存储session
    扫描关注公众号(搜索公众号:码农编程进阶笔记),获取更多视频教程
    MySQL最常用分组聚合函数
    正确使用AWS S3的方式之打造自己的https图床
    消息队列 能做成 websocket 那样推送消息到客户端吗
    ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregate
    IT视频资源分享列表
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7490636.html
Copyright © 2011-2022 走看看