HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8998 Accepted Submission(s): 3856
total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
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.
(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.
2
10 10 20 20
15 15 25 25.5
0
Test case #1
Total explored area: 180.00所谓的离散化,大家能够简单的理解为,将一组非常大的数据,浓缩为一组非常小的数据,用这组数据来取代原数据的作用,比方给你1000个数,数的范围为(1,1e18)我们这里就能够用离散化,因为仅仅有1000个数。我们能够用一个数组的下标代表提供的每一数。假设须要这个数据了,因为是下标,能够直接通过下标获得,如此就是离散化。讲得非常基础,是个非常不错的博客然后提醒一下这个扫描线要注意的问题就是区间的问题一般的线段树以及我们的区间改动合并。都有一个共同点,就是不会出现区间缺失的现象,什么叫区间缺失。顾名思义,区间缺失就是缺少一些区间没有进行运算,这里的扫描线就会遇到这个问题。普遍的,我们的线段树以及数据区间分布是这种:[1, a][a + 1, b][b + 1, c][c + 1, d][d + 1, e].......可是假设仅仅是简简单单的用这个来解决扫描线的问题会导致错误,为什么因为。他没有涉及到[a,a + 1],在扫描线中会出现[a,a + 1]中的数据,而经常使用的线段树的区间概念是无法解决这种问题的,出现了所谓的区间缺失,如何解决,以下的代码给出了解决方式,这里简单的提一下,就是利用[ , ),这个区间性质,左闭右开。就可以解决区间缺失问题
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
const int MAXN = 2000 + 5;
int Col[MAXN << 2], n, cnt, res;
double X[MAXN << 2], Sum[MAXN << 2];
struct seg {
double l,r,h;
int s;
seg() {}
seg(double l,double r,double h,int s):l(l),r(r),h(h),s(s) {}
bool operator < (const seg & object) const {
return h < object.h;
}
} S[MAXN]; void pushup(int rt,int l,int r) {
if (Col[rt]) Sum[rt] = X[r+1] - X[l];//利用[ , ),这个区间性质。左闭右开
else if (l == r) Sum[rt] = 0;
else Sum[rt] = Sum[rt<<1] + Sum[rt<<1|1];
} void update(int L, int R, int c,int rt,int l, int r) {
if(L <= l && r <= R) {
Col[rt] += c;
pushup(rt,l,r);
return ;
}
int mid = (l + r) >> 1;
if(L <= mid) update(L, R, c, lson);
if(R > mid) update(L, R, c, rson);
pushup(rt,l,r);
} int binary_find(double x){
int lb = -1,ub = res - 1;
while(ub - lb > 1){
int mid = (lb + ub) >> 1;
if(X[mid] >= x) ub = mid;
else lb = mid;
}
return ub;
} int main() {
int cas = 1;
while(~ scanf("%d", &n), n) {
cnt = res = 0;
for(int i = 0 ; i < n; i ++) {
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a, &b, &c,&d);
S[cnt] = seg(a, c, b, 1);
X[cnt ++] = a;
S[cnt] = seg(a, c, d, -1);
X[cnt ++] = c;
}
sort(X, X + cnt);
sort(S, S + cnt);
res ++;
for(int i = 1; i < cnt; i ++) {
if(X[i] != X[i - 1]) X[res ++] = X[i];
} memset(Sum, 0, sizeof(Sum));
memset(Col, 0, sizeof(Col));
double ans = 0;
for(int i = 0;i < cnt - 1;i ++){
int l = binary_find(S[i].l);
int r = binary_find(S[i].r) - 1;//利用[ , ),这个区间性质,左闭右开
update(l, r, S[i].s, 1, 0, res - 1);
ans += Sum[1] * (S[i + 1].h - S[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++ , ans);
}
return 0;
}
HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)的更多相关文章
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 Atlantis(线段树面积并)
描述 There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. S ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- Atlantis HDU - 1542 (线段树扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu 1542(线段树+扫描线 求矩形相交面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- Vue beaforeCreate时获取data中的数据
异步获取即:通过 $this.$nextTick或者settimeout,这连dom都可以拿出来 beforeCreate() { this.$nextTick(function() { con ...
- react link引入外部css样式的坑
刚开始的代码是这样的,使用react router4.x写的demo路由跳转后,页面的没有渲染,是因为没有引入外部css文件(或者说引入外部文件路径错误) <!DOCTYPE html> ...
- CAD参数绘制样条线(网页版)
在CAD设计时,需要绘制样条线,用户可以设置样条线线重及颜色等属性. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定位置.详细说明如下: 参数 说明 DOUBL ...
- Vue打包之后部署到 express 服务器上
Part.1 安装 express npm install express body-parer --save Part.2 在项目根目录下创建 app.js 文件作为启动 express 服务器代码 ...
- 第3节 mapreduce高级:7、自定义outputformat实现输出到不同的文件夹下面
2.1 需求 现在有一些订单的评论数据,需求,将订单的好评与差评进行区分开来,将最终的数据分开到不同的文件夹下面去,数据内容参见资料文件夹,其中数据第九个字段表示好评,中评,差评.0:好评,1:中评, ...
- 第2节 mapreduce深入学习:10、手机号码进行分区
需求三:手机号码分区 在需求一的基础上,继续完善,将不同的手机号分到不同的数据文件的当中去,需要自定义分区来实现,这里我们自定义来模拟分区,将以下数字开头的手机号进行分开 135 开头数据到一个分区文 ...
- Go:工厂模式
Go的结构体没有构造函数,通常可以使用工厂模式来解决这个问题. 一个结构体的声明是这样的: package model type Student struct { Name string } 因为 S ...
- C#基础学习(二)
---恢复内容开始--- 面向对象 (类是不占内存,实例占内存) C#与python不用可以直接从另一个文件直接实例化一个类,不需要导包: ...
- NodeJs中数据库的使用
另一遍通用的NODEJS数据库方法koa,express,node 通用方法连接MySQL 1.Node.js 连接 MySQL $ cnpm install mysql 连接mysql: var m ...
- ZOJ 2561 Order-Preserving Codes
Order-Preserving Codes Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on ZJU. ...