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坐标去重,不过我试了一下不去重 也不 ...
随机推荐
- tornado中使用torndb,连接数过高的问题
问题背景 最近新的产品开发中,使用了到了Tornado和mysql数据库.但在基本框架完成之后,我在开发时候发现了一个很奇怪的现象,我在测试时,发现数据库返回不了结果,于是我在mysql中输入show ...
- [020] Android模拟器访问本地Web应用
本篇文章试图解决这样一个问题:如何在Android模拟器上访问本地的Web应用? 例如,在你的开发机器上启动一个Tomcat服务,接着打开电脑上的浏览器,默认情况下输入http://localhost ...
- 【 D3.js 高级系列 — 6.0 】 值域和颜色
在[入门 - 第 10 章]作了一张中国地图,其中各省份的颜色值都是随意赋值的.如果要将一些值反映在地图上,可以利用颜色的变化来表示值的变化. 1. 思路 例如,有值域的范围为: [10, 500] ...
- erlang reduction
“首先明确一点,Erlang的process的调度是抢占式的,而非couroutine的协作式的.其次,Erlang早期版本是只有一个调度器,运行在一个线程上,随着erts的发展,现在erlang的调 ...
- Oracle Analyze 命令 详解
官网的链接如下: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_4005.htm#SQLRF01105 使用DBMS ...
- 【jQuery】鼠标接触按钮后改变图片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- $( ).focus()与$( )[0].focus()区别
$( #id).focus()与$( #id)[0].focus()没有区别,因为id必须是唯一的.如果同一页面出现多个相同的ID(这是不符合w3c规范的),$(#id)也只会拿到第一个该ID,后面的 ...
- 在stm32上移植wpa_supplicant(一)
wifi芯片为88w8686,已经写好了驱动,用的是SPI方式,接下来准备移植wpa_supplicant.参考的资料为一篇论文----<基于微控制器的WPA技术研究与应用>. wpa_s ...
- C++的笔记学习第一篇,认识C++
在一个类中包含两种成员: 数据和函数,分别称为C++数据成员和成员函数. 关于类: 类是C++新增加的重要数据类型,有了类,就就可以实现面向对象程序设计方法中的封装.信息隐蔽.继承.派生.多态等功能. ...
- css优先级判断
概念 浏览器是通过判断优先级,来决定到底哪些属性值是与元素最相关的,从而应用到该元素上.优先级是由选择器组成的匹配规则决定的. 如何计算? 优先级是根据由每种选择器类型构成的级联字串计算而成的. 它不 ...