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) ...
随机推荐
- #20175201 实验二:Java面向对象程序设计
20175201 实验二:Java面向对象程序设计 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L ...
- reduce函数
python中的reduce python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是 ...
- Android 崩溃优化
Android 两种崩溃 android崩溃分为Java崩溃和Nativie崩溃 java崩溃就是java代码中,出现未捕获的异常,导致程序异常退出 Native崩溃一般是因为在Native代码中访问 ...
- localforage调用setItem时出现DOMException错误的解决方法
今天使用localforage时出现下面的错误: Uncaught (in promise) DOMException transaction.onabort.transaction.onerror ...
- jdk安装路径
问题:jdk1.4.2安装目录设成D盘,为什么装完后,C盘也有了一个200多兆的JAVA文件夹?里面我看了一下,有j2re1.4.2_13 jdk1.5.0_09 jr ...
- zabbix监控实战<1>
第一章 监控家族 1.1 为什么选择监控? 因为在一个IT集群中或者是一个大环境中,包括各种硬件设备.软件设备等系统的构成也是极其复杂的. 多种应用构成负载的IT业务系统,保证这些资源的正常运转,是一 ...
- hdu4779 组合计数+dp
提交 题意:给了n*m的网格,然后有p个重型的防御塔,能承受1次攻击,q个轻型防御塔不能接受任何攻击,然后每个防御搭会攻击他所在的行和所在的列,最后求在这个网格上放至少一个防御塔的方案数, 我们枚举 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- MyBatis探究-----为实体类Bean取别名,配置typeAliases
1.单个实体类设置别名 1.1 不使用alias <typeAliases> <!-- typeAlias:为某个java类型起别名 ; type:指定要起别名的类型全类名; 默认别 ...
- 如何在Anoconda Prompt 安装pytorch
一.首先需要安装好Anoconda,具体安装步骤可通过https://www.cnblogs.com/chenfeifen/p/10266012.html查看 由于官方下载更新工具包的速度很慢,因此添 ...