NC51111 Atlantis
题目
题目描述
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.
输入描述
The input consists of several test cases. Each test case starts with a line containing a single integer n \((1 \leq n \leq 100)\) of available maps. The n following lines describe one map each. Each of these lines contains four numbers \(x_1;y_1;x_2;y_2\) \((0 \leq x_1 \lt x_2 \leq 100000;0 \leq y_1 \lt y_2 \leq 100000)\) , not necessarily integers. The values \((x_1; y_1)\) and \((x_2;y_2)\) 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.
输出描述
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.
示例1
输入
2
10 10 20 20
15 15 25 25.5
0
输出
Test case #1
Total explored area: 180.00
题解
知识点:扫描线,线段树,离散化。
线段树+扫面线处理面积并问题,是板子题。
更新时,通过到上次更新的距离与线段覆盖长度,来计算面积。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T>
struct Discretization {
vector<T> uniq;
Discretization() {}
Discretization(const vector<T> &src) { init(src); }
void init(const vector<T> &src) {
uniq = src;
sort(uniq.begin() + 1, uniq.end());
uniq.erase(unique(uniq.begin() + 1, uniq.end()), uniq.end());
}
int get(T x) { return lower_bound(uniq.begin() + 1, uniq.end(), x) - uniq.begin(); }
};
template<class T>
class ScanlineA {
struct Segment {
int l, r;
int cover;
T len;
};
int n;
vector<T> dot;
vector<Segment> node;
void push_up(int rt) {
if (node[rt].cover) node[rt].len = dot[node[rt].r + 1] - dot[node[rt].l];
else if (node[rt].l == node[rt].r) node[rt].len = 0;
else node[rt].len = node[rt << 1].len + node[rt << 1 | 1].len;
}
void update(int rt, int l, int r, int x, int y, int cover) {
if (r < x || y < l) return;
if (x <= l && r <= y) return node[rt].cover += cover, push_up(rt);
int mid = l + r >> 1;
update(rt << 1, l, mid, x, y, cover);
update(rt << 1 | 1, mid + 1, r, x, y, cover);
push_up(rt);
}
public:
ScanlineA() {}
ScanlineA(const vector<T> &_dot) { init(_dot); }
void init(const vector<T> &_dot) {
assert(_dot.size() >= 2);
n = _dot.size() - 2;
dot = _dot;
node.assign(n << 2, { 0,0,0,0 });
function<void(int, int, int)> build = [&](int rt, int l, int r) {
node[rt] = { l,r,0,0 };
if (l == r) return;
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
};
build(1, 1, n);
}
void update(int x, int y, int cover) { update(1, 1, n, x, y, cover); }
Segment query() { return node[1]; }
};
/// 面积并扫描线特化线段树,O(logn),配合离散化可以处理任意精度覆盖长度并问题
/// 求面积并,O(nlogn),面积并 = sum(两次扫描的距离*覆盖长度并)
//* 其中n代表线段数,并非端点数,端点数应为n+1
//* 端点编号从1开始,线段编号也从1开始
//* 任何区间(如l,r或x,y)都代表线段编号而非端点编号,即表示dot[l]到dot[r + 1],使用时注意
template<class T>
struct pk {
T val;
friend bool operator<(const pk &a, const pk &b) {
if (abs(a.val - b.val) < 1e-6) return false;//! 浮点型注意相等条件
return a.val < b.val;
}
friend bool operator==(const pk &a, const pk &b) { return !(a < b) && !(b < a); }
};
//* 专门处理浮点型比较判断的封装类
template<class T>
struct edge {
T x;
pk<T> y1, y2;
int rky1, rky2;
int flag;
};
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
int cnt = 0;
cout << fixed << setprecision(2);
while (cnt++, cin >> n, n) {
if (cnt > 1) cout << '\n';
vector<edge<double>> e(2 * n + 1);
vector<pk<double>> y_src(2 * n + 1);
for (int i = 1;i <= n;i++) {
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
e[2 * i - 1] = { x1,{y1},{y2},0,0,1 };
e[2 * i] = { x2,{y1},{y2},0,0,-1 };
y_src[2 * i - 1] = { y1 };
y_src[2 * i] = { y2 };
}
Discretization<pk<double>> dc(y_src);
for (int i = 1;i <= n;i++) {
e[2 * i - 1].rky1 = dc.get({ e[2 * i - 1].y1 });
e[2 * i - 1].rky2 = dc.get({ e[2 * i - 1].y2 });
e[2 * i].rky1 = dc.get({ e[2 * i].y1 });
e[2 * i].rky2 = dc.get({ e[2 * i].y2 });
}
sort(e.begin() + 1, e.end(), [&](const auto &a, const auto &b) {return a.x < b.x;});
vector<double> dot(dc.uniq.size());
for (int i = 1;i < dot.size();i++) dot[i] = dc.uniq[i].val;
ScanlineA<double> sla(dot);
double ans = 0;
sla.update(e[1].rky1, e[1].rky2 - 1, e[1].flag);
for (int i = 2;i <= 2 * n;i++) {
ans += (e[i].x - e[i - 1].x) * sla.query().len;
sla.update(e[i].rky1, e[i].rky2 - 1, e[i].flag);
}
cout << "Test case #" << cnt << '\n';
cout << "Total explored area: " << ans << '\n';
}
return 0;
}
/*
2
10 10 20 20
15 15 25 25.5
2
10 10 20 20
15 15 25 25.5
0
Test case #1
Total explored area: 180.00
Test case #2
Total explored area: 180.00
*/
NC51111 Atlantis的更多相关文章
- [POJ1151]Atlantis
[POJ1151]Atlantis 试题描述 There are several ancient Greek texts that contain descriptions of the fabled ...
- 线段树---Atlantis
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A Description There are se ...
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 【POJ】1151 Atlantis(线段树)
http://poj.org/problem?id=1151 经典矩形面积并吧.....很简单我就不说了... 有个很神的地方,我脑残没想到: 将线段变成点啊QAQ这样方便计算了啊 还有个很坑的地方, ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ 1542 Atlantis(线段树 面积 并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...
- [POJ 1151] Atlantis
一样的题:HDU 1542 Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18148 Accepted ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- 【POJ1151】【扫描线+线段树】Atlantis
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- json 解析:marshal 和 unmarshal
Go 使用 encoding/json 包的 marshal 和 unmarshal 实现 json 数据的编解码.分别记录如下: 1. marshal 定义结构体: type OCP struct ...
- MySQL 覆盖索引详解
本文转载自:MySQL 覆盖索引详解,作者 Sevn 1. 什么是索引? 索引(在 MySQL 中也叫"键key")是存储引擎快速找到记录的一种数据结构,通俗来说类似书本的目录. ...
- Could not get a resource from the pool 异常定位和解决
最近在服务中经常看到以下错误,进行下定位和问题解决分析: 2023-12-08 00:10:58.248 WARN [terra-sr-server,a9006fd27ccb81d0,a9006fd2 ...
- Makeflie脚本使用
1.目标 2.Makefile的作用 自动化编译仿真 文件有引用层级关系,Tb会引用RTL顶层,RTL顶层也会引用一些其他的小的模块,编译的时候被引用的文件需要先进行编译. 脚本有两种模式,debug ...
- 问题--去除CSDN水印
1.问题如上 有时候需要使用其中的图片,但是水印很让人烦恼 确实可以用PS中的修复画笔工具,修复工具等进行处理 但是当水印覆盖到字体时,就会破坏到原有字体 2.解决方式 从CSDN添加水印的方式入手 ...
- [转帖]shell 把以空格分隔的变量 分割后的每个字段赋值给变量
比如我有一个变量 "123 456 789",要求以空格为分隔符把这个变量分隔,并把分隔后的字段分别赋值给变量,即a=123:b=456:c=789 共有3中方法: 法一:先定义一 ...
- [转帖]Percolator - 分布式事务的理解与分析
https://zhuanlan.zhihu.com/p/261115166 Percolator - 分布式事务的理解与分析 概述 一个web页面能不能被Google搜索到,取决于它是否被Googl ...
- [转帖]Nginx 使用与异常处理
http://jartto.wang/2017/04/15/nginx-exception-handling/ 以前总是偷懒使用 Http-Server 来启动一个本地服务,后来花时间学习了一下 Ng ...
- [转帖]JVM(3)之垃圾回收(GC垃圾收集器+垃圾回收算法+安全点+记忆集与卡表+并发可达性分析......)
<深入理解java虚拟机>+宋红康老师+阳哥大厂面试题2总结整理 一.堆的结构组成 堆位于运行时数据区中是线程共享的.一个进程对应一个jvm实例.一个jvm实例对应一个运行时数据区.一个运 ...
- [转帖]初识SkyWalking
https://zhuanlan.zhihu.com/p/361579294 一.SkyWalking 是什么? 一个开源的可观测平台,用于从服务和云原生基础设施收集,分析, 聚合及可视化数据. Sk ...