题目链接

题意

给出n个矩形,求面积并。

思路

使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去。离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积。这里要注意线段树上结点维护的是线段的信息,而不是点的信息。

参考资料

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
#define lson l, m, rt<<1
#define rson m + 1, r, rt<<1|1
struct Node {
int st;
double l, r, id;
bool operator < (const Node &rhs) const {
return id < rhs.id;
}
} p[N];
double y[N], tree[N<<2];
int cnt[N<<2]; void PushUp(int l, int r, int rt) {
if(cnt[rt] > 0) tree[rt] = y[r+1] - y[l]; // r + 1是因为线段树上结点是线段,映射成点就要+1
else if(l == r) tree[rt] = 0; // 当这个线段没有cnt的时候就代表消失了
else tree[rt] = tree[rt<<1] + tree[rt<<1|1];
} void Update(int L, int R, int w, int l, int r, int rt) {
if(L <= l && r <= R) {
cnt[rt] += w;
PushUp(l, r, rt);
return ;
}
int m = (l + r) >> 1;
if(L <= m) Update(L, R, w, lson);
if(m < R) Update(L, R, w, rson);
PushUp(l, r, rt);
} int main() {
int cas = 1, n;
while(scanf("%d", &n), n) {
int cnt = 0, m = 0;
for(int i = 1; i <= n; i++) {
double x1, x2, y1, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
y[++cnt] = y1, y[++cnt] = y2;
p[++m] = (Node) { 1, y1, y2, x1 };
p[++m] = (Node) { -1, y1, y2, x2 };
}
sort(y + 1, y + 1 + cnt);
sort(p + 1, p + 1 + m);
cnt = unique(y + 1, y + 1 + cnt) - y - 1;
double ans = 0;
for(int i = 1; i <= m; i++) {
ans += tree[1] * (p[i].id - p[i-1].id);
int L = lower_bound(y + 1, y + 1 + cnt, p[i].l) - y;
int R = lower_bound(y + 1, y + 1 + cnt, p[i].r) - y - 1;
// R - 1是因为线段树上的结点是线段
Update(L, R, p[i].st, 1, cnt, 1);
printf("%d : %d - %d , %.2f\n", i, L, R, tree[1]);
}
printf("Test case #%d\n", cas++);
printf("Total explored area: %.2f\n\n", ans);
}
return 0;
}

最新文章

  1. Git入门
  2. 解决JQuery.ajax.post乱码问题
  3. CoreLocation框架的使用---定位,求两地距离
  4. Go语言_时间篇
  5. MXML的一些基本语法
  6. hdoj 2568 前进
  7. Java ConcurrentHashmap 解析
  8. poj 2513 连接火柴 字典树+欧拉通路 好题
  9. 第十一篇:Map/Reduce 工作机制分析 - 错误处理机制
  10. java中的并发工具类
  11. pycrypto 安装 Crypto 报错 error: Microsoft Visual C++ 14.0 is required. Get it with &quot;Microsoft Visual C++ Build Tools&quot;: http://landinghub.visualstudio.com/visual-cpp-build-tools
  12. C#,单元测试
  13. Lua 变量名词
  14. Codeforces 841D Leha and another game about graph - 差分
  15. 转 java反射详解
  16. 各版本.NET委托的写法回顾(转)
  17. 在没有任何投票节点情况下将从节点转换为Primary节点脚本
  18. [转]Android--多线程之Handler
  19. 杂: PYTHON上数据储存:推荐h5py
  20. PHP多进程学习(三)__代码案例来了解父进程与子进程的执行顺序

热门文章

  1. WPF Path和图形
  2. Convert和RelativeSource
  3. Win8 Metro(C#)数字图像处理--2.37Wallis图象锐化
  4. 浅谈Android高通(Qualcomm)和联发科(MTK)平台
  5. 百度官方wormHole后门检测记录
  6. Linux ACL对某一些文件有管理权限
  7. C#有哪几种定时器
  8. Mysql下载(on windows-noinstall zip archive)
  9. Ext5.1日期控件仅显示年月
  10. 在Azure中新建Linux