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;
    }
    
    
  • 相关阅读:
    华为云薛浩:媒体业务进入全面云化时代,云原生成为必然选择
    Python 绑定:从 Python 调用 C 或 C++
    不藏了,这些Java反射用法总结都告诉你们
    云图说|华为HiLens云上管理平台 花样管理多种端侧设备
    论文解读丨图神经网络应用于半结构化文档的命名实体识别和关系提取
    动手实操丨基于随机森林算法进行硬盘故障预测
    教你几招HASH表查找的方法
    MindSpore模型精度调优实战:如何更快定位精度问题
    云图说|应用魔方AppCube:揭秘码农防脱神器
    java算法易筋经:常见java-API使用技巧
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7490636.html
Copyright © 2011-2022 走看看