题意:给n个可能相交的矩形,问你不重复的总面积

思路:扫描线,一边扫一边加。

扫描线:图片来源:理解扫描线

假设我们要算以下四个矩形面积,显然中间深色的是重复的。我们按照x的大小,从左往右扫,然后用线段树维护y轴向的长度就可以了。但是,我们不能用点去维护y轴坐标,而是抽象成把点i看成y[i]到y[i+1]这个区间,不然会有错。

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long longll;
using namespace std;
const int maxn = + ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
struct Bian{
double x, y1, y2;
int flag;
bool operator < (const Bian &a) const{
return x < a.x;
}
}bian[maxn << ];
double y[maxn << ]; //离散化定位
int n, cnt;
int Find(double x){ //在y中离散化后位置
int l = , r = cnt, ans = ;
while(l <= r){
int m = (l + r) >> ;
if(fabs(y[m] - x) < 1e-) ans = m;
if(y[m] > x) r = m - ;
else l = m + ;
}
return ans;
}
int cover[maxn << ]; //y的覆盖次数
double sum[maxn << ];
void pushUp(int l, int r, int rt){
if(cover[rt] > ) sum[rt] = y[r + ] - y[l];
//全覆盖了
else if(l == r) sum[rt] = ;
//没覆盖的叶子结点
else sum[rt] = sum[rt << ] + sum[rt << | ];
//部分覆盖
}
void build(int l, int r, int rt){
if(l == r){
cover[rt] = sum[rt] = ;
return;
}
int m = (l + r) >> ;
build(l, m, rt << );
build(m + , r, rt << | );
cover[rt] = sum[rt] = ;
}
void update(int L, int R, int l, int r, int v, int rt){
if(L <= l && R >= r){
cover[rt] += v;
pushUp(l, r, rt);
return;
}
int m = (l + r) >> ;
if(L <= m)
update(L, R, l, m, v, rt << );
if(R > m)
update(L, R, m + , r, v, rt << | );
pushUp(l, r, rt);
}
int main(){
int ca = ;
while(scanf("%d", &n) && n){
for(int i = ; i <= n; i++){
double x1, x2, y1, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
int ll = * i - , rr = * i;
bian[ll].x = x1, bian[ll].y1 = y1, bian[ll].y2 = y2;
bian[ll].flag = ;
bian[rr].x = x2, bian[rr].y1 = y1, bian[rr].y2 = y2;
bian[rr].flag = -;
y[ll] = y1, y[rr] = y2; //定位用的
}
n = n + n;
sort(bian + , bian + n + );
sort(y + , y + n + ); cnt = ; //unique
for(int i = ; i <= n; i++){
if(y[i] != y[i - ]){
y[++cnt] = y[i];
}
} double ans = ;
build(, cnt, );
for(int i = ; i < n; i++){
update(Find(bian[i].y1), Find(bian[i].y2) - , , cnt, bian[i].flag, );
ans += sum[] * (bian[i + ].x - bian[i].x);
}
printf("Test case #%d\n", ca++);
printf("Total explored area: %.2lf\n\n", ans);
}
return ;
}

HDU 1542 Atlantis(扫描线)题解的更多相关文章

  1. POJ 1151 HDU 1542 Atlantis(扫描线)

    题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角.地图有些地方是重叠的.所以让你求出被描写叙述的地方的总面积. 扫描线的第一道题,想了又想,啸爷 ...

  2. (HDU 1542) Atlantis 矩形面积并——扫描线

    n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...

  3. HDU 1542 Atlantis(矩形面积并)

    HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...

  4. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  6. hdu 1542 Atlantis(线段树,扫描线)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. hdu 1542 Atlantis(段树&amp;扫描线&amp;面积和)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. (中等) HDU 1542 Atlantis,扫描线。

    Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...

  9. HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. Java基础语法入门01

    Java基础语法入门01 学习java你要先进行去了解JDK,JRE,JVM JDK Java开发工具包 JRE Java语言开发的运行环境 JVM Java虚拟机,用于Java语言的跨平台所用. 当 ...

  2. Spring cloud 之Ribbon(一)基本使用

    简介 Spring cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它是基于Netflix的Riboon实现的.Ribbon是客户端负载均衡器,这有别语例如Nginx服务端负载 ...

  3. linx下对文件权限设置

    语法格式:chmod u+/-rwx,g+/-rwx,o+/-rwx filename 如:1. 给主人添加读权限,并减去执行权限:chmod u+r,u-x filename2. 给所有用户(主人. ...

  4. tensorflow-用DASC结合Inception-v3对imagenet2012聚类实现

    一.目的 以imagenet2012作为数据集,用Inception-v3对图像提取特征作为输入,来训练一个自编码器. 以上作为预训练模型,随后在该自编码器的基础上,中间加入一个自表示层,将最终学习到 ...

  5. 3.1.3 Spring之AOP

    三.Spring之AOP 1. 代理模式 (1) 什么是代理模式? 代理模式是面向对象编程的23种基础设计模式之一.为其他对象(代理对象)提供一种代理以控制对这个对象(源对象)的访问. 就是说,声明一 ...

  6. ThinkPHP安全规范指引

    流年 发布于 ThinkPHP官方博客: https://blog.thinkphp.cn/789333 本文主要和大家探讨一下ThinkPHP的安全注意事项,可以作为ThinkPHP建议的安全规范实 ...

  7. IMU 标定 | 工业界和学术界有什么不同?

    点击"计算机视觉life"关注,置顶更快接收消息! 由于格式问题最好在公众号上观看<IMU 标定-工业界和学术界有什么不同?> 本文主要介绍了IMU基本结构原理和误差的 ...

  8. eclipse myeclipse中的一些配置

    1.显示.setting 点击三角号 选择customsize view 取消.*resources myeclipse如何更改项目名 点击项目名->alt+enter(properties)

  9. react16实战总结

    实战总结 react实战基础 遇到的一些坑 li里要带key值否则会警告,这个问题在vue中也存在, 考虑到虚拟DOM中diff,所以不要用index作为key值,而要用item. 2.immutab ...

  10. 功能比较全的StackExchange.Redis封装帮助类(.Net/C#)

    Redis官网https://redis.io/ 以下内容未全部验证,如有问题请指出 //static NewtonsoftSerializer serializer = new Newtonsoft ...