POJ 1389 (扫描线)
2015-11-24 01:15
369 查看
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> using namespace std; #define maxn 2222 #define pl c<<1 #define pr (c<<1)|1 #define lson tree[c].l,tree[c].mid,c<<1 #define rson tree[c].mid+1,tree[c].r,(c<<1)|1 int n; double pos[maxn]; //竖线 struct node { int l, r, mid; int cover; double len; }tree[maxn<<4]; struct segment { double l, r, h; int flag; bool operator < (segment a) const { return h < a.h; } }p[maxn]; double a, b, c, d; int cnt, m; void build_tree (int l, int r, int c) { tree[c].l = l, tree[c].r = r, tree[c].mid = (l+r)>>1; tree[c].cover = 0, tree[c].len = 0; if (l == r) return ; build_tree (lson); build_tree (rson); return ; } void push_up (int c) { if (tree[c].cover > 0) { tree[c].len = pos[tree[c].r+1] - pos[tree[c].l]; } else if (tree[c].l == tree[c].r) tree[c].len = 0; else tree[c].len = tree[pl].len + tree[pr].len; } void update (int l, int r, int c, int x, int y, int v) { if (l == x && r == y) { tree[c].cover += v; push_up (c); return ; } if (tree[c].mid >= y) { update (lson, x, y, v); } else if (tree[c].mid < x) { update (rson, x, y, v); } else { update (lson, x, tree[c].mid, v); update (rson, tree[c].mid+1, y, v); } push_up (c); return ; } void debug (int c) { cout << "l:" << tree[c].l << " r:" << tree[c].r << " len:" <<tree[c].len << " cover:" <<tree[c].cover <<endl; if (tree[c].l == tree[c].r) return ; debug (pl); debug (pr); } int main () { //freopen ("in", "r", stdin); int kase = 0; while (scanf ("%d", &n) == 1 && n) { cnt = 0; for (int i = 1; i <= n; i++) { scanf ("%lf%lf%lf%lf", &a, &b, &c, &d); p[cnt].l = a, p[cnt].r = c, p[cnt].h = b, p[cnt].flag = 1, pos[cnt] = a, cnt++; p[cnt].l = a, p[cnt].r = c, p[cnt].h = d, p[cnt].flag = -1, pos[cnt] = c, cnt++; } sort (pos, pos+cnt); sort (p, p+cnt); m = 0; pos[m++] = pos[0]; for (int i = 1; i < cnt; i++) { if (pos[i] != pos[i-1]) { pos[m++] = pos[i]; } } build_tree (0, m-1, 1); double ans = 0.0; for (int i = 0; i < cnt-1; i++) { int l = lower_bound (pos, pos+m, p[i].l) - pos; int r = lower_bound (pos, pos+m, p[i].r) - pos - 1; update (0, m-1, 1, l, r, p[i].flag); ans += tree[1].len * (p[i+1].h - p[i].h); } printf ("Test case #%d\n", ++kase); printf ("Total explored area: %.2f\n\n", ans); } return 0; }
相关文章推荐
- android客户端访问服务器端(SSH框架)
- 用JavaScript实现动态省市县三级联动
- 用JavaScript实现动态省市县三级联动
- iOS.swift 纯代码 关于如何提取TableViewCell视图作为view
- JAVA程序调用MYSQL存储过程
- Unix网络编程---第二次作业
- POJ 1936 DP(公共序列+滚动数组)
- 文档检索和倒排索引
- Xcode:Foundation框架找不到,或者是自动提示出现问题
- hdu 1863 畅通工程(prim+kruskal)
- Unix网络编程---第一次作业
- 删除排序数组中的重复数字 II
- 团队作业M1反思
- MyEclipse插件不能安装原因
- MySQL存储过程 事务transaction
- JAVA笔记二
- Java多线程开发系列之番外篇:事件派发线程---EventDispatchThread
- JAVA笔记一环境变量配置
- *LeetCode-Meeting Rooms II
- nagios安装学习一