zoukankan      html  css  js  c++  java
  • POJ 1151 Atlantis 线段树扫描线

    $ Rightarrow $ 戳我进POJ原题

    Atlantis

    Time Limit: 1000MS $ quad $ Memory Limit: 10000K
     

    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 le n le 100) $ of available maps.
    The $ n $ following lines describe one map each.
    Each of these lines contains four numbers $ x_1;y_1;x_2;y_2 (0 le x_1 < x_2 le 100000;0 le y_1 < y_2 le 100000) $ , not necessarily integers.
    The values $ (x_1; y_1) $ and $ (x_2;y_2) $ 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

    Mid-Central European Regional Contest 2000
     

    题目大意

    • 给 $ N $ 个矩形,求面积并。
       

    思路

    • $ N $ 个矩形,取出每个矩形的左右两条边(也就是一共 $ 2 imes n $ 条竖线),
      进行标记区分左右(例如左标 $ +1 $ 右标 $ -1 $ )。然后把 $ y $ 坐标离散化。

    • 把这些竖线按照 $ x $ 坐标递增顺序排序,然后扫描一遍,
      遇到 $ +1 $ 标记就把这条竖线段对应的区间覆盖次数加一
      遇到 $ -1 $ 标记的覆盖次数减一。

    • 每次统计出当前被覆盖的总长度,乘相邻两条竖线之间的距离差就是其中一部分面积了。
       

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define maxn 500
    struct tree{ double l,r,sum; }t[maxn<<3];
    struct data{ double x,y1,y2; int f; }p[maxn<<3];
    int n,cases,lazy[maxn<<3];
    double ans,s[maxn<<3];
    bool cmp(data a,data b){ return a.x<b.x; }
    void pushup(int o){
    	if(lazy[o]>0) t[o].sum=t[o].r-t[o].l;
    	else t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
    }
    void build(int o,int l,int r){
    	t[o].l=s[l]; t[o].r=s[r];
    	if(r-l<=1){ t[o].sum=0; return; }
    	int mid=l+r>>1;
    	build(o<<1,l,mid); build(o<<1|1,mid,r);
    	pushup(o);
    }
    void updata(int o,double y1,double y2,int flag){
    	if(t[o].l==y1&&t[o].r==y2){ lazy[o]+=flag; pushup(o); return; }
    	if(t[o<<1].r>y1) updata(o<<1,y1,min(t[o<<1].r,y2),flag);
    	if(t[o<<1|1].l<y2) updata(o<<1|1,max(t[o<<1|1].l,y1),y2,flag);
    	pushup(o);
    }
    int main(){
    	while(scanf("%d",&n)&&n){ 
    		++cases; ans=0;
    		double x1,x2,y1,y2;
    		for(int i=1;i<=n;++i){
    			scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
    			p[i].x=x1; p[i].y1=y1; p[i].y2=y2; p[i].f=1;
    			p[i+n].x=x2; p[i+n].y1=y1; p[i+n].y2=y2; p[i+n].f=-1;
    			s[i]=y1; s[i+n]=y2;
    		}
    		sort(s+1,s+1+2*n);
    		sort(p+1,p+1+2*n,cmp);
    		build(1,1,2*n);
    		memset(lazy,0,sizeof(lazy));
    		for(int i=1;i<=2*n;++i){
    			ans+=(p[i].x-p[i-1].x)*t[1].sum;
    			updata(1,p[i].y1,p[i].y2,p[i].f);
    		}
    		printf("Test case #%d
    Total explored area: %.2lf
    
    ",cases,ans);
    	}
    	return 0;
    }
    /*
    Problem: 1151
    
    User: potremz
    Memory: 184K
    
    Time: 16MS
    Language: C++
    
    Result: Accepted
    */
    
  • 相关阅读:
    解决IE 下div与img重叠无法触发鼠标事件的问题
    四边相同阴影效果
    dedecms 获取文章发布时间和获取文章最后更新时间
    局域网访问网站
    HTML 5 的data-* 自定义属性
    yum 安装 influxdb/telegraf
    zabbix 监控 AWS-SQS 队列
    解决阿里云部署 office web apps ApplicationFailedException 报错问题
    jira集成fisheye代码深度查看工具安装绿色版
    阿里云ecs开启x11图形化桌面
  • 原文地址:https://www.cnblogs.com/PotremZ/p/POJ1151.html
Copyright © 2011-2022 走看看