HDU 1542 Atlantis

题目链接

题意:给定一些矩形,求面积并

思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了。假设数据大。就要用线段树

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int N = 205;
const int M = 100005;
const double eps = 1e-8; int n, vis[N], hn;
double hash[N]; struct Line {
double l, r, y;
int flag;
Line() {}
Line(double l, double r, double y, int flag) {
this->l = l;
this->r = r;
this->y = y;
this->flag = flag;
}
} line[N]; bool cmp(Line a, Line b) {
return a.y < b.y;
} int get(double x) {
return lower_bound(hash, hash + hn, x) - hash;
} int main() {
int cas = 0;
while (~scanf("%d", &n) && n) {
double x1, x2, y1, y2; hn = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[i * 2] = Line(x1, x2, y1, 1);
line[i * 2 + 1] = Line(x1, x2, y2, -1);
hash[hn++] = x1; hash[hn++] = x2;
}
n *= 2;
sort(line, line + n, cmp);
sort(hash, hash + hn);
hn = 1;
for (int i = 1; i < n; i++) {
if (fabs(hash[i] - hash[i - 1]) < eps) continue;
hash[hn++] = hash[i];
}
double ans = 0;
for (int i = 0; i < n; i++) {
int l = get(line[i].l), r = get(line[i].r);
double len = 0;
for (int j = 0; j < hn - 1; j++) if (vis[j] > 0) len += (hash[j + 1] - hash[j]);
if (i) ans += len * (line[i].y - line[i - 1].y);
for (int j = l; j < r; j++) vis[j] += line[i].flag;
}
printf("Test case #%d\n", ++cas);
printf("Total explored area: %.2lf\n\n", ans);
}
return 0;
}

最新文章

  1. 利用varnish做Discuz论坛的缓存服务器
  2. gpuimage的各种滤镜简介
  3. 内外分离接口依赖及UIScrollView知识点
  4. 前端学PHP之文件操作(认真读读)
  5. cookies的理解
  6. sql server 如何在一个数据库中操作另一个数据库中的数据
  7. [转]VGA、QVGA、CIF、QCIF 。。。的含义
  8. VS2015创建的Asp.net WebApi默认项目在CentOS7+Mono4.2.2+jexus5.8运行不起来的解决方案
  9. bnuoj 29375 Two Strings(字符串?)
  10. 使用WMI来连接远端计算机
  11. [Swust OJ 360]--加分二叉树(区间dp)
  12. PHP第一章学习——了解PHP(上)
  13. java自带的监控工具VisualVM一
  14. 扩充表字段长度,引发的意外KILLED/ROLLBACK
  15. 微信小程序与AspNetCore SignalR聊天实例
  16. class example of C++
  17. docker 操作镜像的基本操作
  18. 使用 DITA-OT 发布一份 CouchBase Server 手册
  19. JS设计模式——单例模式剖析
  20. 用GraphX分析伴生网络(一)

热门文章

  1. MyBatis学习总结(7)——Mybatis缓存
  2. leetcode第一刷_Subsets II
  3. python爬虫 分页获取图片并下载
  4. C++编写绚丽的界面
  5. Linux基础02
  6. DBMS_XPLAN详细说明
  7. 使用终端改变MAC默认截图存放地址
  8. 最长公共子序列(稀疏序列)nlogn解法
  9. Sublime Text 3 注册码 激活码 版本号 Build 3143
  10. SPA SEO SSR三者有什么区别