poj1151 Atlantis (线段树+扫描线+离散化)
有点难,扫描线易懂,离散化然后线段树处理有点不太好理解。
因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了。
AC代码
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = + ; struct Line{
double x, y1, y2;
int flag;
Line(double a, double b, double c, int d) {
x = a; y1 = b; y2 = c; flag = d;
}
bool operator < (const Line& a) const {
return x < a.x;
}
}; vector<Line> line;
map<double, int> Hash;
vector<double> y; double p[maxn << ]; // 区间长度
int covers[maxn << ]; // 覆盖次数
double len[maxn << ]; // 覆盖长度 void buildTree(int o, int l, int r) {
len[o] = covers[o] = ;
p[o] = y[r] - y[l];
if(l + != r) {
int m = (l+r) / ;
buildTree(o*, l, m);
buildTree(o*+, m, r);
}
} int ul, ur;
void update(int o, int l, int r, int f) {
if(l+ == r) {
covers[o] += f;
if(covers[o] == ) len[o] = ;
else len[o] = p[o];
}
else {
int m = (l+r) / ;
if(ul < m) update(o*, l, m, f);
if(m < ur) update(o*+, m, r, f);
// pushUp
len[o] = len[o*] + len[o*+];
}
} int main() {
int n, kase = ;
while(scanf("%d", &n) == && n) {
Hash.clear(); line.clear(); y.clear();
double x1, y1, x2, y2;
for(int i = ; i < n; i++) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line.push_back(Line(x1, y1, y2, ));
line.push_back(Line(x2, y1, y2, -));
y.push_back(y1);
y.push_back(y2);
} sort(line.begin(), line.end());
sort(y.begin(), y.end());
y.erase(unique(y.begin(), y.end()), y.end());
for(int i = ; i < (int)y.size(); i++) {
Hash[y[i]] = i;
} buildTree(, , y.size()-);
double ans = ;
for(int i = ; i < line.size(); i++) {
if(i > ) ans += (line[i].x - line[i-].x) * len[];
ul = Hash[line[i].y1]; ur = Hash[line[i].y2];
update(, , y.size()-, line[i].flag);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", kase++, ans);
}
return ;
}
如有不当之处欢迎指出!
poj1151 Atlantis (线段树+扫描线+离散化)的更多相关文章
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ1151 Atlantis 线段树扫描线
扫描线终于看懂了...咕咕了快三个月$qwq$ 对于所有的横线按纵坐标排序,矩阵靠下的线权值设为$1$,靠上的线权值设为$-1$,然后执行线段树区间加减,每次的贡献就是有效宽度乘上两次计算时的纵坐标之 ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- 【42.49%】【hdu 1542】Atlantis(线段树扫描线简析)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
随机推荐
- 随手科技(随手记)2017招聘Java工程师笔试题
一 如何解决多台web服务器粘性会话的问题? 粘性session:web服务器会把某个用户的请求,交给tomcat集群中的一个节点,以后此节点就负责该保存该用户的session,如果此节点挂掉,那么 ...
- Extjs6 grid 导出excel功能类,支持renderer
/* grid 导出excel扩展(纯客户端,提交到后台再导的可以自己改改代码也在) 参考自 https://blog.csdn.net/tianxiaode/article/details/4596 ...
- 运维自动化之系统部署 PXE(二)
PXE介绍 Preboot Excution Environment 预启动执行环境 Intel公司研发 基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持 ...
- Linux下利用文件描述符恢复的成功失败实验
1.测试环境准备[oracle@redhat3 ~]$ uname -aLinux redhat3 2.6.32-573.el6.x86_64 #1 SMP Wed Jul 1 18:23:37 ED ...
- redis----------基本命令使用
1.查看全部缓存数据的key keys * 2.清空当前redis数据库缓存 flushdb (redis默认由16个库(0~15号). 且默认使用的是0号库.库之间的切换使用select命令例如: ...
- python小程序--one
#!/usr/bin/env python # _*_ coding:utf8 _*_ import sys user_lock_file="user_lock.txt" # 用户 ...
- BigDecimal源码
1 public BigDecimal(char[] in, int offset, int len, MathContext mc) {// 使用字符数组的构造方法,一般我们推荐使用的是一Strin ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [app2.xml]: Instantiation of bean failed; nested exception is org.spr
在学习spring整合hubernate时遇到的问题.c3p0遇到了一个问题,老连不上,显示java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indi ...
- centos上发布部署python的tornado网站项目完整流程
先说下大体上的做法,开发环境上要新弄一个 virtualenv的环境,在这个里面放你的开发调试,当然这个其实也不是必须的,但是这样会方便管理一些. 再在centos上也弄一个 virtualenv虚拟 ...
- tomcat+nginx实现
这里采用tomcat安装包 tomcat 版本说明: 9.0.17 nginx 版本说明: 1.14.2 jdk 版本说明: 1.8.0 创建目录 [root@web02 /]# m ...