题意:给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. domain logic approaches

    领域逻辑组织可以分为三种主要的模式:事务脚本(Transaction Script).领域模型(Domain Model)和表模块(Table Module)” 1.domain logic appr ...

  2. NodeJS笔记(四) NPM 指令--- npm start

    在上一节中使用我们使用下面的指令启动了Express的demo APP项目 npm start 这个指令具体执行了哪些内容呢? Node.js新版本改变了启动方式,npm start  会执行  bi ...

  3. new和malloc区别,delete和delete []区别

    面试被问到上述问题,所以特地总结一下: 一.new和malloc的区别. 1.new可以返回指定类型的指针,并且自动分配内存大小:malloc需要计算手动计算分配空间的大小,并且返回值需要强转为实际类 ...

  4. 2016(4)数据库系统,ER模型,规范化理论,并发控制

    试题四(共25分) 阅读以下关于数据库设计的叙述,在答题纸上回答问题1至问题3. 某航空公司要开发一个订票信息处理系统,以方便各个代理商销售机票.开发小组经过设计,给出该系统的部分关系模式如下: 航班 ...

  5. 20.C# 创建自己的泛型类型

    1.定义泛型类 可以使用以下语法创建泛型类,T可以是任意符合C#标识符命名规范的任意标识符 class MyGenericClass<T> { //.... } 泛型类可以包含任意多个类型 ...

  6. Xamarin Forms error MSB6006: “java.exe”已退出,代码为 2 解决办法

    https://vicenteguzman.mx/2017/08/20/error-java-exe-exited-with-code-2-xamari-forms/

  7. 201808_summary

    @Consumes @Produces分别表示入参和出参数吗 可以这样讲.但是不是很到位.是限定作用,类似于filterconsumes: 指定处理请求的提交内容类型(Content-Type),例如 ...

  8. django--orm对象关系映射之常用的增删改查

    1.查询表里所有数据 book=models.Book.objects.all() 2.条件查询 book = models.Book.objects.filter(id=1).first() # 查 ...

  9. git之reset图解

    https://blog.csdn.net/longintchar/article/details/81843048 1.三棵树. 此时如果我们运行 git status,会发现没有任何改动,因为现在 ...

  10. 279. Perfect Squares(动态规划)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...