HDU 1542 Atlantis(线段树扫描线,面积并)
2015-08-20 18:59
405 查看
题意:
给你nn (n<=100)(n <= 100)个矩阵,问你矩阵并后的面积。解析:
http://www.cnblogs.com/kane0526/archive/2013/02/26/2934214.html参考了这篇题解报告,终于学会了基本的线段树扫描线。
mymy codecode
[code]#include <cstdio> #include <cstring> #include <algorithm> #define ls o<<1 #define rs o<<1|1 #define lson ls, L, M #define rson rs, M+1, R using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 205; double X[maxn]; int n, m, tot; struct Line { double l, r, h; int d; Line() {} Line(double x1, double x2, double h, int d) : l(x1), r(x2), h(h), d(d) {} bool operator < (const Line& rhs) const { return h < rhs.h; } } line[maxn]; double sumv[maxn<<2]; int cover[maxn<<2]; inline void pushUp(int o, int L, int R) { if(cover[o]) sumv[o] = X[R+1] - X[L]; else if(L == R) sumv[o] = 0; else sumv[o] = sumv[ls] + sumv[rs]; } void build(int o, int L, int R) { sumv[o] = cover[o] = 0; if(L == R) return ; int M = (L + R)/2; build(lson); build(rson); } void modify(int o, int L, int R, int ql, int qr, int d) { if(ql <= L && R <= qr) { cover[o] += d; pushUp(o, L, R); return ; } int M = (L + R)/2; if(ql <= M) modify(lson, ql, qr, d); if(qr > M) modify(rson, ql, qr, d); pushUp(o, L, R); } int main() { double x1, y1, x2, y2; int cas = 1; while(~scanf("%d", &n) && n) { m = tot = 0; for(int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); line[m] = Line(x1, x2, y1, 1); X[m++] = x1; line[m] = Line(x1, x2, y2, -1); X[m++] = x2; } //discrete sort(X, X+m); tot = unique(X, X+m) - X; sort(line, line+m); build(1, 0, tot-1); int ql, qr; double ans = 0; for(int i = 0; i < m; i++) { ql = lower_bound(X, X+tot, line[i].l) - X; qr = lower_bound(X, X+tot, line[i].r) - X - 1; modify(1, 0, tot-1, ql, qr, line[i].d); ans += sumv[1] * (line[i+1].h - line[i].h); } printf("Test case #%d\n", cas++); printf("Total explored area: %.2lf\n\n", ans); } return 0; }
相关文章推荐
- 汤姆猫实现代码
- jQuery Ajax 实例
- 数据结构中排序方法基本概念 及 分类
- 分享一些不错的学习IOS资源网站
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
- Android中的IPC通信(一)
- 7 Strace Examples to Debug the Execution of a Program in Linux
- JDBC基本知识
- 深入浅出 EJB3.1(下)
- 【Java】-基础-数据库操作
- 机房收费系统之结账
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
- iOS Cell异步图片加载优化,缓存机制详解
- PHP模板引擎的原理与实践
- HDU 5414 CRB and String (2015 Multi-University Training Contest 10 2015多校联合)
- 图片编辑器
- using gdb to debug c program
- 深入浅出 EJB3.1(上)
- 为什么“高大上”的算法工程师变成了数据民工?
- Android如何判断一个应用在运行