HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并
解法:跟求矩形面积并差不多,不过线段树节点记录的为:
len: 此区间线段长度
cover: 此区间是否被整个覆盖
lmark,rmark: 此区间左右端点是否被覆盖
num: 此区间分离开的线段的条数
重点在转移的地方,不难理解。
代码:
#include <iostream>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 10007 struct node
{
int cov,len,num,lmark,rmark;
}tree[*N]; struct Line
{
int y1,y2,x;
int cov;
}line[*N];
int yy[*N]; int cmp(Line ka,Line kb)
{
return ka.x < kb.x;
} int bsearch(int l,int r,int x)
{
while(l <= r)
{
int mid = (l+r)/;
if(x == yy[mid])
return mid;
if(x > yy[mid])
l = mid+;
else
r = mid-;
}
return l;
} void build(int l,int r,int rt)
{
tree[rt].cov = tree[rt].len = tree[rt].num = tree[rt].lmark = tree[rt].rmark = ;
if(l == r-) return;
int mid = (l+r)/;
build(l,mid,*rt);
build(mid,r,*rt+);
} void pushup(int l,int r,int rt)
{
if(tree[rt].cov)
{
tree[rt].len = yy[r]-yy[l];
tree[rt].lmark = tree[rt].rmark = ;
tree[rt].num = ;
}
else if(l+ == r)
tree[rt].len = tree[rt].num = tree[rt].lmark = tree[rt].rmark = ;
else
{
tree[rt].len = tree[*rt].len + tree[*rt+].len;
tree[rt].lmark = tree[*rt].lmark;
tree[rt].rmark = tree[*rt+].rmark;
tree[rt].num = tree[*rt].num+tree[*rt+].num-tree[*rt].rmark*tree[*rt+].lmark;
//如果左右区间连续,那么可以合并,减少一个区间
}
} void update(int l,int r,int aa,int bb,int cover,int rt)
{
if(aa <= l && bb >= r)
{
tree[rt].cov += cover;
pushup(l,r,rt);
return;
}
if(l+ == r) return;
int mid = (l+r)/;
if(aa <= mid)
update(l,mid,aa,bb,cover,*rt);
if(bb > mid)
update(mid,r,aa,bb,cover,*rt+);
pushup(l,r,rt);
} int main()
{
int n,m,cs = ,i,j;
int x1,y1,x2,y2;
while(scanf("%d",&n)!=EOF)
{
m = ;
for(i=;i<n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[m].x = x1,line[m].y1 = y1,line[m].y2 = y2,line[m].cov = ,yy[m++] = y1;
line[m].x = x2,line[m].y1 = y1,line[m].y2 = y2,line[m].cov = -,yy[m++] = y2;
}
m--;
sort(yy+,yy+m+);
int cnt = ;
for(i=;i<=m;i++)
{
if(yy[i] != yy[i-])
yy[cnt++] = yy[i];
}
cnt--;
build(,cnt,);
sort(line+,line+m+,cmp);
int ans = ;
int last = ;
for(i=;i<=m;i++)
{
int L = bsearch(,cnt,line[i].y1);
int R = bsearch(,cnt,line[i].y2);
update(,cnt,L,R,line[i].cov,);
ans += abs(last - tree[].len);
if(i == m) break;
ans += tree[].num**(line[i+].x-line[i].x);
last = tree[].len;
}
printf("%d\n",ans);
}
return ;
}
HDU 1828 / POJ 1177 Picture --线段树求矩形周长并的更多相关文章
- POJ 1151 Atlantis 线段树求矩形面积并 方法详解
第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- HDU 1828 POJ 1177 Picture
矩形周长并 POJ上C++,G++都能过,HDU上C++过了,G++WA ,不知道为什么 #include<cstdio> #include<cstring> #include ...
- 【hdu1828/poj1177】线段树求矩形周长并
题意如图 题解:这题非常类似与矩形面积并,也是维护一个被覆盖了一次以上的线段总长. 但是周长要算新出现的,所以每次都要和上一次做差求绝对值. x轴做一遍,y轴做一遍. 但是有个问题:矩形边界重合的时候 ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- POJ 1177 Picture(线段树周长并)
描述 A number of rectangular posters, photographs and other pictures of the same shape are pasted on ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
随机推荐
- 启动Tomcat出现“Bad version number in .class file (unable to load class XXX)”解决
转载自:http://blog.csdn.net/justdb/article/details/8067887 主要是jdk版本的不搭配 保证tomcat 的jdk版本 项目的jdk版本 还有就是编译 ...
- linux下socket编程
相关结构 //下边这两个结构定义在<sys/types.h>里 //一般的地址结构,只能用于覆盖(把其他地址转换为此类型),且只能引用该地址的sa_family字段 struct sock ...
- Mysql的简单使用(三)
接上文Mysql的简单使用(二) mysql中结构相同的两个表进行合并:(注意需要两个表的结构是一样的) 有如下结构的两个表father和person. 合并的步骤为: 1.把person表和fath ...
- mysql启用慢日志查询
查询超时时间:long_query_time 启动慢查日志:log_slow_queries={YES|NO} 启动慢查日志 : slow_query_log ...
- 非线性数据拟合-nls
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- Angular框架
Angular 框架 Angular介绍 库和框架的区别 jQuery:库 库一般都是封装了一些常用的方法 自己手动去调用这些方法,来完成我们的功能 code $('#txt').val('我是小明' ...
- Force.com微信开发系列(四)申请Access Token及自定义菜单之创建菜单
在微信接口开发中,许多服务的使用都离不开Access Token,Access Token相当于打开这些服务的钥匙,正常情况下会在7200秒内失效,重复获取将导致上次获取的Token失效,本文将首先介 ...
- android XMl 解析神奇xstream 二: 把对象转换成xml
前言:对xstream不理解的请看:android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 1.Javabeen 代码 packa ...
- linux+jre+apache+mysql+tomcat调优
一.不再为Apache进程淤积.耗尽内存而困扰 0. /etc/my.cnf,在mysqld那一段加上如下一行: log-slow-queries=queries-slow.log 重启MySQL 酌 ...
- linux NFS服务器安装与配置 思路
一,nfs服务优缺点 NFS 是Network File System的缩写,即网络文件系统,可以让不同的客户端挂载使用同一个目录,作为共享存储使用,这样可以保证不同的节点客户端数据一致性,在集群架构 ...