POJ 1151 Atlantis (扫描线+线段树)
题目链接:http://poj.org/problem?id=1151
题意是平面上给你n个矩形,让你求矩形的面积并。
首先学一下什么是扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html
这是别人的blog,写的挺好的。然后明白扫描线之后呢,接下来就很简单了,只需要一次一次求面积然后累加就好了。这题离散化之后,数据的范围更小了(因为n只有100),单点更新就行了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 2e2 + ;
struct data {
double x1 , x2;
double y;
int xx1 , xx2 , flag; //flag: 1表示加 -1表示减 xx1 xx2是离散化之后的值
bool operator <(const data& cmp) const {
return y < cmp.y;
}
}edge[MAXN << ];
struct segtree {
int l , r , lazy; //lazy表示累加的次数
double val; //val表示长度
}T[MAXN << ];
double x[MAXN];
map <double , int> mp; double min(double a , double b) {
return a > b ? b : a;
} double max(double a , double b) {
return a > b ? a : b;
} void init(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r , T[p].val = T[p].lazy = ;
if(r - l == ) {
return ;
}
init(p << , l , mid);
init((p << )| , mid , r);
} void updata(int p , int l , int r , int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].r - T[p].l == ) {
if(val == -) {
T[p].lazy--;
T[p].val = T[p].lazy ? (x[T[p].r] - x[T[p].l]) : ;
}
else {
T[p].lazy++;
T[p].val = (x[T[p].r] - x[T[p].l]);
}
return ;
}
if(r <= mid) {
updata(p << , l , r , val);
}
else if(l >= mid) {
updata((p << )| , l , r , val);
}
else {
updata(p << , l , mid , val);
updata((p << )| , mid , r , val);
}
T[p].val = T[p << ].val + T[(p << )|].val;
} int main()
{
int n , ca = ;
double x1 , x2 , y1 , y2;
while(~scanf("%d" , &n) && n) {
int cont = ;
mp.clear();
for(int i = ; i < n ; i++) {
scanf("%lf %lf %lf %lf" , &x1 , &y1 , &x2 , &y2);
edge[i * ].x1 = min(x1 , x2) , edge[i * ].x2 = max(x1 , x2);
edge[i * ].y = min(y1 , y2);
edge[i * ].flag = ;
edge[i * + ].x1 = edge[i * ].x1 , edge[i * + ].x2 = edge[i * ].x2;
edge[i * + ].y = max(y1 , y2);
edge[i * + ].flag = -;
if(!mp[x1]) {
x[++cont] = x1;
mp[x1]++;
}
if(!mp[x2]) {
x[++cont] = x2;
mp[x2]++;
}
}
sort(edge , edge + n * );
sort(x + , x + cont + );
for(int i = ; i < n * ; i++) {
int pos = (lower_bound(x + , x + cont + , edge[i].x1) - x);
edge[i].xx1 = pos;
pos = (lower_bound(x + , x + cont + , edge[i].x2) - x);
edge[i].xx2 = pos;
}
init( , , cont);
double res = ;
updata( , edge[].xx1 , edge[].xx2 , edge[].flag);
for(int i = ; i < n * ; i++) {
res += T[].val * (edge[i].y - edge[i - ].y);
updata( , edge[i].xx1 , edge[i].xx2 , edge[i].flag);
}
printf("Test case #%d\n" , ca++);
printf("Total explored area: %.2f\n\n" , res);
}
}
POJ 1151 Atlantis (扫描线+线段树)的更多相关文章
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- poj 1151 (未完成) 扫描线 线段树 离散化
#include<iostream> #include<vector> #include<cmath> #include<algorithm> usin ...
- POJ 1151 Atlantis(扫描线)
题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS Memory Limit: 10 ...
- poj1151 Atlantis——扫描线+线段树
题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...
- POJ 1542 Atlantis(线段树 面积 并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...
- 【POJ】1151 Atlantis(线段树)
http://poj.org/problem?id=1151 经典矩形面积并吧.....很简单我就不说了... 有个很神的地方,我脑残没想到: 将线段变成点啊QAQ这样方便计算了啊 还有个很坑的地方, ...
- [HDU1542]Atlantis(扫描线+线段树)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
随机推荐
- C/C++内存存储
#include <stdio.h> #include "string.h" #include "malloc.h" void Swap(int a ...
- asp.net实现GZip压缩和GZip解压
最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...
- 如何让Vim显示dos下的^M符号
/*********************************************************************** * 如何让Vim显示dos下的^M符号 * 声明: * ...
- Azure SQL 数据库最新版本现已提供预览版
Tiffany Wissner 数据平台营销高级总监 我们之前在11月宣布将提供新的预览版,在该预览版中我们引入了接近完整的 SQL Server 引擎兼容性和更为高级的性能,这些都代表了下一代的 ...
- VS2010下编译安装DarwinStreamingServer5.5.5
源码下载链接:http://dss.macosforge.org/源码版本: 5.5.5版本电脑环境:visual studio2010,window 7 x64系统.用VS2010打开WinNTSu ...
- shell -Z- d等等代表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [ -a FILE ] ...
- 【转】traits技术及模板偏特化
#include <iostream> using namespace std; struct __xtrue_type { }; // define two mark-type stru ...
- ASP.NET MVC+Bootstrap个人博客之praise.js点赞特效插件(二)
1. 为啥要做这个点赞插件? praise.js是一款小巧的jQuery点赞插件,使用简便,效果美观. 在做个人博客时遇到了文章点赞问题.联想到各大社交网络中的点赞特效:手势放大.红心放大等等, ...
- HDU 5874 Friends and Enemies
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- Oracle EM 不能访问
zwt2001267 原文 Oracle EM 不能访问 1. cmd控制启动EM: C:\Users\Administrator>emctl start dbconsoleEnvironmen ...