HDU 1542 Atlantis(扫描线)题解
题意:给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(扫描线)题解的更多相关文章
- POJ 1151 HDU 1542 Atlantis(扫描线)
题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角.地图有些地方是重叠的.所以让你求出被描写叙述的地方的总面积. 扫描线的第一道题,想了又想,啸爷 ...
- (HDU 1542) Atlantis 矩形面积并——扫描线
n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...
- HDU 1542 Atlantis(矩形面积并)
HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- (中等) HDU 1542 Atlantis,扫描线。
Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- Github 入门(“趣考网络”学习第一步)
目录 为什么要使用GitHub 下载Github Desktop fork 与 pull request git pull,fetch,merge,push的区别与联系 git clone 与 dow ...
- (2.1)mysql升级与降级
(2.1)mysql升级与降级 转自:深入浅出mysql数据库开发.优化与管理第二版 1.mysql升级 2.mysql降级
- 给opencart产品页添加额外信息
有时我们在开发opencart时需要给产品页添加一些额外的信息,第一种聪明的方法可以修改并调用已有字段,详细可以参考opencart3产品页调用upc/数量等信息:如果您的开发能力不错的话可以用第二种 ...
- 使用charles模拟慢速网络
1.设置慢速网络 点击导航栏的proxy---throttle setting来设置想要的网络情况, 其中有两种方法: (1)勾选Enable Throttling,在Throttle presett ...
- Angular+NodeJs+MongoDB搭建前后端程序
get请求: //angular 前端get请求 this.client.get('http://localhost:3000/id/tom').subscribe(data => { cons ...
- webpack(3)-管理资源
管理资源:(file-loader 和 url-loader 可以接收并加载任何文件,然后将其输出到构建目录) 加载css:style-loader.css-loader 以style的形式插入到he ...
- JDK8 HashMap--removeNode()移除节点方法
/*删除节点*/ final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, bo ...
- WPF中使用DataGrid时操作列按钮问题
在使用DataGrid的过程中,我们有时候需要对选取的某一行数据进行多个操作,这个时候操作列只有一个按钮显然无法满足我们的要求,我们需要多个按钮才能达到我们的目的. UI页面代码: <Grid& ...
- LNMP分离式部署
#### LNMP组合工作流程 在LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务,如果请求是静态资源,则由Nginx解析返回给用户:如果是动态请求(.php结尾),那么Ng ...
- windows10中修改环境变量的正确姿势
提到修改环境变量,我们可能自然而然想到:右键This PC->Properties->Advanced System Settings->Environment Variables ...