Atlantis(POJ1151+线段树+扫描线)
题目链接:http://poj.org/problem?id=1151
题目:
题意:求所有矩形的面积,重合部分只算一次。
思路:扫描线入门题,推荐几篇学扫描线的博客:
1.http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html
2.https://blog.csdn.net/qq_38786088/article/details/78633478
3.https://blog.csdn.net/lwt36/article/details/48908031(强烈推荐!)
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef unsigned long long ull; #define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; inline int read() {//读入挂
int ret = , c, f = ;
for(c = getchar(); !(isdigit(c) || c == '-'); c = getchar());
if(c == '-') f = -, c = getchar();
for(; isdigit(c); c = getchar()) ret = ret * + c - '';
if(f < ) ret = -ret;
return ret;
} int n, t;
double x1, y_1, x2, y_2;
double y[]; struct Line {
double y1, y2, x;
int flag;
bool operator < (const Line& a) const {
return x < a.x;
}
}line[]; struct node {
int l, r, cover;
double lf, rf, len;
}segtree[maxn*]; void push_up(int i) {
if(segtree[i].cover > ) {
segtree[i].len = segtree[i].rf - segtree[i].lf;
} else if(segtree[i].l + == segtree[i].r) {
segtree[i].len = ;
} else {
segtree[i].len = segtree[i*].len + segtree[i*+].len;
}
} void build(int i, int l, int r) {
segtree[i].l = l, segtree[i].r = r;
segtree[i].lf = y[l], segtree[i].rf = y[r];
segtree[i].cover = segtree[i].len = ;
if(l + == r) return;
int mid = (l + r) >> ;
build(i * , l, mid);
build(i * + , mid, r);
} void update(int i, double l, double r, int flag) {
if(segtree[i].lf == l && segtree[i].rf == r) {
segtree[i].cover += flag;
push_up(i);
return;
}
if(l >= segtree[i * + ].lf) {
update(i * + , l, r, flag);
} else if(r <= segtree[i * ].rf) {
update(i * , l, r, flag);
} else {
update(i * , l, segtree[i*].rf, flag);
update(i * + , segtree[i*+].lf, r, flag);
}
push_up(i);
} int main() {
//FIN;
int icase = ;
while(~scanf("%d", &n) && n) {
t = ;
printf("Test case #%d\n", ++icase);
for(int i = ; i <= n; i++,t++) {
scanf("%lf%lf%lf%lf", &x1, &y_1, &x2, &y_2);
line[t].x = x1;
line[t].y1 = y_1;
line[t].y2 = y_2;
line[t].flag = ;
y[t] = y_1;
line[++t].x = x2;
line[t].y1 = y_1;
line[t].y2 = y_2;
line[t].flag = -;
y[t] = y_2;
}
sort(line + , line + t);
sort(y + , y + t);
build(, , t - );
double ans = ;
update(, line[].y1, line[].y2, line[].flag);
for(int i = ; i < t; i++) {
ans += segtree[].len * (line[i].x - line[i-].x);
update(, line[i].y1, line[i].y2, line[i].flag);
}
printf("Total explored area: %.2f\n\n", ans);
}
return ;
}
Atlantis(POJ1151+线段树+扫描线)的更多相关文章
- Atlantis poj1151 线段树扫描线
Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ1151+线段树+扫描线
/* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- poj1151 Atlantis (线段树+扫描线+离散化)
有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...
- P - Atlantis (线段树+扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Som ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)
传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- 【POJ1151】Atlantis(线段树,扫描线)
[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...
随机推荐
- iOS开发SDWebImage源码解析之SDWebImageManager的注解
最近看了两篇博客,写得很不错,关于SDWebImage源码解析之SDWebImageManager的注解: 1.http://www.jianshu.com/p/6ae6f99b6c4c 2.http ...
- iOS-开发过程中应用间跳转问题
- TCP系列07—连接管理—6、TCP连接管理的状态机
经过前面对TCP连接管理的介绍,我们本小节通过TCP连接管理的状态机来总结一下看看TCP连接的状态变化 一.TCP状态机整体状态转换图(截取自第二版TCPIP详解) 二.TCP连接建立 ...
- 会话模型与SSO
关于会话模型其实网站已有很多帖子说明,其中有关于sessionid,cookie以及他们之间的关系,自己先了解吧 1 会话模型 会话模型是客户端和服务端交互的一种模型,会话模型友好的处理了客户端有无通 ...
- C# 执行bat文件
private void RunBat(string batPath) { Process pro = new Process(); FileInfo file = new FileInfo(batP ...
- Hadoop出现错误:WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable,解决方案
安装Hadoop的时候直接用的bin版本,根据教程安装好之后运行的时候发现出现了:WARN util.NativeCodeLoader: Unable to load native-hadoop li ...
- netbeans调试配置
apache端口8050,xdebug端口9000 1.把项目放到apache的htdocs下(一定要放在htdocs上,要么调试的时候xdebug会一直卡在“等待连接中”) 2.把php_xdebu ...
- Windows API封装:LoadLibrary/FreeLibrary
LoadLibrary/LoadLibraryEx用来加载DLL到自己的进程空间,使用完用FreeLibrary释放,一般使用方式如下: HINSTANCE hInstRich = ::Load ...
- Qt安装与入门
一.Qt SDK1.2安装 准备QtSdk-offline-win-x86-v1_2_1.exe离线安装包. 安装QtSDK时注意不要有中文路径,空格以及特殊字符.可以自定义选择组件安装,也可以默认安 ...
- Devc++编译系统分配给int多少字节
我看的是<C语言程序设计>..谭浩强的PDF版 里面只讲了VC和TC 的,没有Devc++的..(我的是5.10版) 还有这是什么意思? 经过查阅我进行了这样的测试: 得到了这样的结果: ...