Atlantis

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

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
 

题意

  给出n个矩形的左下角和右上角的坐标,求矩形面积的并

解题思路

  离散化x轴,然后扫描线,具体实现看代码

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=+;
int cnt[maxn*],n;//记录某个区间的下底边比上底边多的个数
double sum[maxn*];//记录某个区间下底边比上底边多的个数总长度
double Hash[maxn];//对横坐标x离散化 struct node{
double l,r,h;
int f;
node(){}
node(double x1,double x2,double hh,int ff):l(x1),r(x2),h(hh),f(ff){}
bool operator<(const node &a)const{
return h<a.h;
}
}s[maxn*]; void init(){
memset(sum,,sizeof(sum));
memset(cnt,,sizeof(cnt));
} void pushup(int x,int l,int r){
if(cnt[x]) sum[x]=Hash[r+]-Hash[l];//表示该区间整个线段长度可作为底边
else if(l==r) sum[x]=;//叶子结点区间长度为0,则底边长度为0
else sum[x]=sum[x*]+sum[x*+];
} void update(int x,int L,int R,int f,int l,int r){
if(L<=l&&r<=R){
cnt[x]+=f;
pushup(x,l,r);
return ;
}
int mid=(l+r)/;
if(L<=mid) update(x*,L,R,f,l,mid);
if(R>mid) update(x*+,L,R,f,mid+,r);
pushup(x,l,r);
} int main(){
double x1,x2,y1,y2; int p=;
while(cin>>n&&n){
init();int k=;
while(n--){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
Hash[k]=x1;
s[k++]=node(x1,x2,y1,);
Hash[k]=x2;
s[k++]=node(x1,x2,y2,-);
}
sort(s,s+k);//把线段按高度h从小到大排序
sort(Hash,Hash+k);//把x坐标从小到大排序 int ans=unique(Hash,Hash+k)-Hash;//去重复端点
double SUM=;
printf("Test case #%d\n",++p);
for(int i=;i<k;i++){
int l=lower_bound(Hash,Hash+ans,s[i].l)-Hash;
int r=lower_bound(Hash,Hash+ans,s[i].r)-Hash;
update(,l,r-,s[i].f,,ans-);//扫描线段更新可用底边长
SUM+=sum[]*(s[i+].h-s[i].h);//新增加面积 }
printf("Total explored area: %.2f\n\n",SUM);
}
return ;
}
 

HDU1542 扫描线(矩形面积并)的更多相关文章

  1. [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...

  2. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  3. HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

  4. HDU1542 Atlantis(矩形面积并)

    #pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...

  5. [Codevs] 矩形面积求并

    http://codevs.cn/problem/3044/ 线段树扫描线矩形面积求并 基本思路就是将每个矩形的长(平行于x轴的边)投影到线段树上 下边+1,上边-1: 然后根据线段树的权值和与相邻两 ...

  6. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  7. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  8. hdu 1542 扫描线求矩形面积的并

    很久没做线段树了 求矩形面积的并分析:1.矩形比较多,坐标也很大,所以横坐标需要离散化(纵坐标不需要),熟悉离散化后这个步骤不难,所以这里不详细讲解了,不明白的还请百度2.重点:扫描线法:假想有一条扫 ...

  9. HDU 3265 扫描线(矩形面积并变形)

    Posters Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. nginx测试小结

    最近在工作当中需要使用nginx,就对nginx进行进一步的了解,测试.        工作需求是在微服务架构的基础上,客户端通过nginx反向代理访问服务端,确保当一个服务端出现问题时能及时切换到正 ...

  2. 电商项目中使用Redis实现秒杀功能

    参与过抢购活动就知道,很明显的一点是商即便商品实际没有了也是可以下单成功的,但是在支付的时候会提示你商品没有了. 实现原理:list双向链表 使用redis队列,因为pop操作是原子的,即使有很多用户 ...

  3. Spring再接触 注入类型

    共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...

  4. docker-compose学习

    该实践是在已经安装了docker的基础上,如果还未安装docker,请先安装docker : https://www.cnblogs.com/theRhyme/p/9813019.html docke ...

  5. php 二维数组按照某个键排序

    $date = array_column($arr, 'run_date'); //上面得到的结果:array(0=>'2017-11-21',1=>'2017-11-20',3=> ...

  6. ucore-lab1-练习5report

    实验5--实现函数调用堆栈跟踪函数 需要完成kdebug.c中函数print_stackframe的实现,可以通过函数print_stackframe来跟踪函数调用堆栈中记录的返回地址. 一.函数堆栈 ...

  7. Easyui datagrid 绑定本地Json数据

    var jsonstr = '{"total":1,"rows":[{"id":"M000005","name ...

  8. 50-用Python监听鼠标和键盘事件

    转自:https://www.cnblogs.com/qiernonstop/p/3654021.html 用Python监听鼠标和键盘事件 PyHook是一个基于Python的“钩子”库,主要用于监 ...

  9. Swift 闭包即OC中的Block

    - 闭包的定义 1.提前准备好的代码        2.在需要的时候执行        3.可以当做参数传递 // 1.最简单的闭包 // () -> () 没有参数,没有返回值的函数 // 如 ...

  10. RNA-seq数据综合分析教程 AKAP95

    https://blog.csdn.net/l_yivs?t=1 RNA-seq数据综合分析教程 2 4,055 A+ 所属分类:Transcriptomics   收  藏 2     RNA-se ...