POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151
题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积
题目思路:矩形面积并。
代码如下:
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int N = ;
struct Line
{
int l, r, flag;
double h;
Line(){}
Line(int l, int r, int flag, double h):l(l), r(r), flag(flag), h(h) {}
bool operator<(Line &b) const
{
return h<b.h;
}
}; int n;
vector<double> vec;
vector<Line>line; struct node
{
int l, r, flag;
double len;
};
node tree[N << ]; void build(int l, int r, int k)
{
tree[k].l = l, tree[k].r = r, tree[k].len = , tree[k].flag = ;
if(l == r - )
return;
int mid = (l + r) >> ;
build(l, mid, k<<);
build(mid, r, k<<|);
} void updata_up(int k)
{
if(tree[k].flag)
tree[k].len = vec[tree[k].r-] - vec[tree[k].l-];
else if (tree[k].l == tree[k].r - )
tree[k].len = ;
else
tree[k].len = tree[k<<].len + tree[k<<|].len;
} void updata(int l, int r, int k, int x)
{
if(tree[k].l >= l && tree[k].r <= r)
{
tree[k].flag += x;
updata_up(k);
return;
} if(tree[k<<].r > l)
updata(l, r, k<<, x);
if(tree[k<<|].l < r)
updata(l, r, k<<|, x); updata_up(k);
} int main()
{
int cases = ;
while(scanf("%d", &n),n)
{
vec.clear();
line.clear();
double x1[], y1[], x2[], y2[];
for(int i=; i<n; ++ i)
{
scanf("%lf%lf%lf%lf", &x1[i], &y1[i], &x2[i], &y2[i]);
vec.push_back(x1[i]);
vec.push_back(x2[i]);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for(int i=; i<n; ++ i)
{
int x = lower_bound(vec.begin(), vec.end(), x1[i]) - vec.begin() + ;
int y = lower_bound(vec.begin(), vec.end(), x2[i]) - vec.begin() + ; line.push_back(Line(x, y, , y1[i]));
line.push_back(Line(x, y, -, y2[i]));
}
sort(line.begin(), line.end());
build(, vec.size() + , );
double ans = ;
for(int i=; i<line.size(); ++ i)
{
if(i != )
ans += (line[i].h - line[i-].h)*tree[].len;
updata(line[i].l, line[i].r, , line[i].flag);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++ cases, ans);
}
}
POJ 1151 Atlantis(线段树-扫描线,矩形面积并)的更多相关文章
- POJ 1151 Atlantis 线段树求矩形面积并 方法详解
第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ 1151 - Atlantis 线段树+扫描线..
离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- POJ 1151 Atlantis 线段树+离散化+扫描线
这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
随机推荐
- centos7使用传统网卡名
http://serverfault.com/questions/692897/centos-7-disable-predictable-network-interface-names-during- ...
- oracle_exp_query_where_clause
每次都忘记这个斜线,记录一下 exp img/123@orclxxx file=d:\bbb.dmp table=(ccc_tab) query=\"where id > 999\&q ...
- C++ 随机数
#include <iostream> #include <stdlib.h> #include <time.h> #define random(a,b) (ran ...
- bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-fil ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- 用浏览器模拟各种User Agent
转至:http://www.cnblogs.com/top5/archive/2012/06/07/2540686.html 测试页面的时候经常需要不同的User Agent,Firefox.Chro ...
- 从一个复杂的json格式的String内获取某key的值
如题,如何简单的从一个复杂的String格式内获取某个key的值. 例如:从下面String下取到status的值. {"response":{"info":{ ...
- win10 剪贴板 拒绝访问
win10 Cannot open clipboard:拒绝访问.
- Python自动化 【第十八篇】:JavaScript 正则表达式及Django初识
本节内容 JavaScript 正则表达式 Django初识 正则表达式 1.定义正则表达式 /.../ 用于定义正则表达式 /.../g 表示全局匹配 /.../i 表示不区分大小写 /.../m ...
- 使用Windows上的Eclipse 远程调试 linux下的Tomcat
1:修改Linux上Tomcat的catalina.sh,第一行添加declare -x CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_sock ...