HDU1542Atlantis(扫描线)
HDU1542Atlantis(扫描线)
题目大意:给你n个覆盖矩形,问最后覆盖的面积。
解题思路:将每一个矩形拆成两条线段,一条是+1的,还有一条是减1的。然后扫描先从上往下扫描,碰到加1的那条线段,那么这条线段范围内的节点的覆盖信息就+1,直到碰到减1这个线段范围内的节点的覆盖信息都须要减1。
这样说可能理解不了,就能够画画矩形然后画下扫描线在理解理解。然后就是须要离散化的建树,由于这里是都double型的。所谓离散化建树的意思就是如今每一个叶子节点代表的是一段区间。而不是单独的一点。
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 205;
#define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1)
struct Node {
int l, r, add;
double s;
void set (int l, int r, double s, int add) {
this->l = l;
this->r = r;
this->s = s;
this->add = add;
}
}node[4 * maxn];
struct Line {
double x, y1, y2;
int flag;
Line (double x, double y1, double y2, int flag) {
this->x = x;
this->y1 = y1;
this->y2 = y2;
this->flag = flag;
}
bool operator < (const Line &l) const {
return x < l.x;
}
};
int n;
vector<Line> L;
vector<double> pos;
void pushup(int u) {
if (node[u].add)
node[u].s = pos[node[u].r + 1] - pos[node[u].l];
else if (node[u].l == node[u].r)
node[u].s = 0;
else
node[u].s = node[lson(u)].s + node[rson(u)].s;
}
void build (int u, int l, int r) {
node[u].set (l, r, 0, 0);
if (l == r)
return;
int m = (l + r)>>1;
build(lson(u), l, m);
build(rson(u), m + 1, r);
}
void update (int u, int l, int r, int v) {
if (node[u].l >= l && node[u].r <= r) {
node[u].add += v;
pushup(u);
return;
}
int m = (node[u].l + node[u].r)>>1;
if (l <= m)
update (lson(u), l, r, v);
if (r > m)
update (rson(u), l, r, v);
pushup(u);
}
void init () {
pos.clear();
L.clear();
double x1, y1, x2, y2;
for (int i = 0; i < n; i++) {
scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
pos.push_back(y1);
pos.push_back(y2);
L.push_back(Line(x1, y1, y2, 1));
L.push_back(Line(x2, y1, y2, -1));
}
sort (pos.begin(), pos.end());
sort (L.begin(), L.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end());
build(1, 0, (int)pos.size() - 1);
}
double solve() {
init();
double ans = 0;
for (int i = 0; i < (int)L.size() - 1; i++) {
int l = lower_bound(pos.begin(), pos.end(), L[i].y1) - pos.begin();
int r = lower_bound(pos.begin(), pos.end(), L[i].y2) - pos.begin();
update (1, l, r - 1, L[i].flag);
// printf ("%.2lf\n", node[1].s);
ans += node[1].s * (L[i + 1].x - L[i].x);
}
return ans;
}
int main () {
int T = 0;
double x1, y1, x2, y2;
while (scanf ("%d", &n) && n) {
printf ("Test case #%d\n", ++T);
printf ("Total explored area: %.2lf\n\n", solve());
}
return 0;
}
HDU1542Atlantis(扫描线)的更多相关文章
- HDU1542--Atlantis(扫描线)
给N个矩形的端点坐标,求矩形覆盖面积和. 原理很简单,从左到右扫描,线段树记录的是纵向覆盖的长度.区间更新.因为坐标是实数而且很大,所以需要离散化. WA+RE+CE+MLE+...一共错了二十多次. ...
- HDU1542-Atlantis【离散化&线段树&扫描线】个人认为很全面的详解
刚上大一的时候见过这种题,感觉好牛逼哇,这都能算 如今已经不打了,不过适当写写题保持思维活跃度还是不错的,又碰到这种题了,想把它弄出来 说实话,智商不够,看了很多解析,花了4.5个小时才弄明白 网上好 ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- HUD 4007 [扫描线][序]
/* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...
- Atitit 图像扫描器---基于扫描线
Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...
- 扫描线+堆 codevs 2995 楼房
2995 楼房 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...
- 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 440 Solved: 16 ...
- 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)
4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 61 Solved: 26[Submi ...
随机推荐
- JavaSE-02 变量 数据类型和运算符
学习要点 掌握变量的概念 掌握常用数据类型 掌握赋值运算符.算术运算符 掌握boolean数据类型和关系运算符 掌握变量的概念 面向过程程序的定义 程序的定义:程序=数据+算法+文档 程序要操作的数据 ...
- L2-2 社交集群 (25 分)(一个写挫的并查集)
题目: 思路: 就是一个并查集的裸题,不过在数据查找方面可能不好处理,暴力完全可以解决这个问题啊!! #include <bits/stdc++.h> #include <cstdi ...
- Django框架基础知识02-路由及渲染
1.URL(Uniform Resoure Locator)统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL ...
- 数据结构实验7:实现二分查找、二叉排序(查找)树和AVL树
实验7 学号: 姓名: 专业: 7.1实验目的 (1) 掌握顺序表的查找方法,尤其是二分查找方法. (2) 掌握二叉排序树的建立及查找. 查找是软件设计中的最常用的运算,查找所涉及到 ...
- luogu4035 [JSOI2008]球形空间产生器
如果单按照距离相等的话既是高次也没有半径,所以因为给了 \(n+1\) 组点就想到两两做差. 假如一组点是 \(\{a_i\}\) 一组是 \(\{b_i\}\),我们能轻易地得出 \[\sum_{i ...
- SQL Server 上关于同一张表里的三级联动
或许这并不能叫做三级联动,三级联动是很容易实现的东西,有明确的层级关系,一般分开三张表存储.我在公司的项目里遇到这样一个问题,同一张表里面,有分公司,客户,项目3种关系,他们的层级关系是这样:分公司- ...
- python的cache修饰器
简单的memory cache.可以用来内存缓存任意函数方法. #!/usr/bin/python import functools from threading import RLock impor ...
- 学渣乱搞系列之扩展KMP的那点事
扩展KMP牵涉了一些相对运动的姿势,比较费解!本学渣看了一天的扩展KMP,打算写点东西...本文看后,出现的后果本人一概不负责.毕竟我不是很会表达. 扩展KMP是搞什么灰机的?本学渣所知道的扩展KMP ...
- Android渐变GradientDrawable叠加组合环ring
Android渐变GradientDrawable叠加组合环ring 写一个Android环形shape之间的叠加组合形成新图像的例子.代码: <?xml version="1. ...
- C#排序1(冒泡排序、直接排序、快速排序)
冒泡排序:就是两个两个的这个比较好理解,代码也比较好写出来. 它的原理就是相邻的两个两个的比较,如果前面的数比后面的大,那么交换,它这个在比较完一次的时候可以得到最大的一个数,然后接着循环,每次外循环 ...