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) ...
随机推荐
- Hello Json(c#)
第一步:下载的DLL→Newtonsoft.Json 打开链接后下载这个(有可能版本有所更新,选前面点的就是了): 接下来是新建一个Console项目,然后是引用,然后上码 class Progra ...
- 一篇文章彻底弄懂Base64编码原理
在互联网中的每一刻,你可能都在享受着Base64带来的便捷,但对于Base64的基础原理又了解多少?今天这篇博文带领大家了解一下Base64的底层实现. Base64的由来 目前Base64已经成为网 ...
- VMVare的窗口自适应
啊!好久没来博客园了.原因很简单,我把密码丢了. 最近才从系统申请重置了密码,这不,又能登录了.你可能好奇,是的,我也在疑惑:我是不是搞IT的啊?因为只要密码丢失,我就认为世界完蛋了,我完蛋了:) 这 ...
- Cocos Creator JS 时间戳日期转换
/*** 时间戳换算日期* */function formatDateTime (timeStamp) { var date = new Date(); date.setTime(timeStamp ...
- IDEA开发工具的学习
1.设置jdk的版本 ,快捷键:ctrl + shirt +alt + s 打开项目的设置,选择Project 进行 jdk版本的设置. 2.鼠标移到项目上,右键,Show in Explorer 定 ...
- 短信外部浏览器H5链接一键跳转微信打开任意站
今天讲讲微信跳转的那些事情,这项技术最早出现在在线广告上面,可以从外部引流到微信并打开微信内置浏览器然后打开一个指定的网页地址,在这个网页里面可以放任何想推广的内容,可以是引导文案.活动内容,或者是一 ...
- Sqlite3入门简记
一,安装Sqlite3 1.入门时看http://www.runoob.com/sqlite/sqlite-intro.html,说的简单,但是适合入门 2.在终端输入sqlite3,没有返回信息,表 ...
- Python学习笔记(Ⅰ)——Python程序结构与基础语法
作为微软的粉丝,最后终于向Python低头了,拖了两三个月终于下定决心学习Python了.不过由于之前受到C/C#等语言影响的思维定式,前期有些东西理解起来还是很费了些功夫的. 零.先抄书: 1.Py ...
- Python文件操作中的方法:.write()换行
active =Truewhile active: message =input("\nPlease input your name:\n") if message =='q': ...
- 读取FTP 图片文件,并显示,非下载
关于FTP,先从怎么创建开始说起,很简单,步骤就两个 ① 电脑-右键管理--本地用户组--添加用户名,密码(用于FTP的用户名.密码) ② IIS 新建FTP站点,命名,指定端口号.FTP文件路径 接 ...