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(扫描线)的更多相关文章

  1. HDU1542--Atlantis(扫描线)

    给N个矩形的端点坐标,求矩形覆盖面积和. 原理很简单,从左到右扫描,线段树记录的是纵向覆盖的长度.区间更新.因为坐标是实数而且很大,所以需要离散化. WA+RE+CE+MLE+...一共错了二十多次. ...

  2. HDU1542-Atlantis【离散化&线段树&扫描线】个人认为很全面的详解

    刚上大一的时候见过这种题,感觉好牛逼哇,这都能算 如今已经不打了,不过适当写写题保持思维活跃度还是不错的,又碰到这种题了,想把它弄出来 说实话,智商不够,看了很多解析,花了4.5个小时才弄明白 网上好 ...

  3. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  4. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  5. HUD 4007 [扫描线][序]

    /* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...

  6. Atitit 图像扫描器---基于扫描线

    Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...

  7. 扫描线+堆 codevs 2995 楼房

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...

  8. 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 16 ...

  9. 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)

    4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 61  Solved: 26[Submi ...

随机推荐

  1. ProxyFactory

    Spring定义了org.springframework.aop.framework.AopProxy接口,并提供了两个final类型的实现类. AopProxy类结构:

  2. 《发际线总是和我作队》第八次团队作业:Alpha冲刺 第五天

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺实验十一 团队作业7:团队项目设计完善&编码 团队名称 发际线总和我作队 作业学 ...

  3. github下拉刷新与上拉加载地址

    https://github.com/chrisbanes/Android-PullToRefresh

  4. 浅谈stiring数

    在组合数学,Stirling数可指两类数,第一类Stirling数和第二类Stirling数. stirling常应用于许多组合枚举问题中. 第一类stirling数: 对第一类Stirling数   ...

  5. Python自动化测试框架——概述

    #使用import import unittest #测试用例TestCase ''' 一个测试用例时一个完整的测试流程,包括了环境准备SetUp,测试执行Run,测试环境还原TearDown 一个测 ...

  6. 不同子系统采用不同MySQL编码LATIN1和UTF8的兼容

    程序处理 这是一个历史遗留系统, 旧的系统是C++开发的, 插入数据的时候, 没有统一MYSQL各个层次(服务器, 数据库, 表, 列)的编码, 这个情况基本上是MYSQL的默认安装导致的, 实际的数 ...

  7. pyquery 基本使用笔记

    安装 pip install pyquery 导入 from pyquery import PyQuery as pq 初始化: from pyquery import PyQuery as pq h ...

  8. SpringMVC修改功能

    articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  9. 大数据学习——flume日志分类采集汇总

    1. 案例场景 A.B两台日志服务机器实时生产日志主要类型为access.log.nginx.log.web.log 现在要求: 把A.B 机器中的access.log.nginx.log.web.l ...

  10. lr函数之lr_eval_string()函数的使用学习

    lr_eval_string() 函数的主要作用:返回脚本中的一个参数当前的值(从参数中取得对应的值,并且转换为一个字符串). 格式:lr_eval_string("{参数名}") ...