POJ 1177 Picture(线段树周长并)
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.
Input
contains the number of rectangles pasted on the wall. In each of the
subsequent lines, one can find the integer coordinates of the lower left
vertex and the upper right vertex of each rectangle. The values of
those coordinates are given as ordered pairs consisting of an
x-coordinate followed by a y-coordinate.
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Output
contain a single line with a non-negative integer which corresponds to
the perimeter for the input rectangles.
Sample Input
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int N=;
int col[][N<<],sum[][N<<],x[][N<<];
struct seg
{
int l,r,h,s;
seg() {}
seg(int l,int r,int h,int s):l(l),r(r),h(h),s(s) {}
bool operator<(const seg &ob)const
{
return h<ob.h;
}
}s[][N<<];
void PushUp(int rt,int l,int r,int flag)
{
if(col[flag][rt])sum[flag][rt]=x[flag][r+]-x[flag][l];
else if(l==r)sum[flag][rt]=;
else sum[flag][rt]=sum[flag][rt<<]+sum[flag][rt<<|];
}
void Update(int L,int R,int flag,int C,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[flag][rt]+=C;
PushUp(rt,l,r,flag);
return;
}
int mid=(l+r)>>;
if(L<=mid)Update(L,R,flag,C,l,mid,rt<<);
if(R>mid)Update(L,R,flag,C,mid+,r,rt<<|);
PushUp(rt,l,r,flag);
}
int main()
{
int n,x1,x2,y1,y2;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
s[][i]=seg(x1,x2,y1,);
x[][i]=x1;
s[][n+i]=seg(x1,x2,y2,-);
x[][n+i]=x2; s[][i]=seg(y1,y2,x1,);
x[][i]=y1;
s[][n+i]=seg(y1,y2,x2,-);
x[][n+i]=y2;
}
n<<=;
sort(x[],x[]+n);
sort(s[],s[]+n);
sort(x[],x[]+n);
sort(s[],s[]+n);
int ans=,pre=,pre1=;
for(int i=;i<n;i++)
{
int l=lower_bound(x[],x[]+n,s[][i].l)-x[];
int r=lower_bound(x[],x[]+n,s[][i].r)-x[]-;
Update(l,r,,s[][i].s,,n-,);
ans+=abs(sum[][]-pre);
pre=sum[][];
}
for(int i=;i<n;i++)
{
int l=lower_bound(x[],x[]+n,s[][i].l)-x[];
int r=lower_bound(x[],x[]+n,s[][i].r)-x[]-;
Update(l,r,,s[][i].s,,n-,);
ans+=abs(sum[][]-pre1);
pre1=sum[][];
}
printf("%d\n",ans);
}
return ;
}
POJ 1177 Picture(线段树周长并)的更多相关文章
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)
题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...
- Picture POJ - 1177 (线段树-扫描线)
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- Buy Tickets POJ - 2828 思维+线段树
Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...
- POJ 1177 Picture(线段树:扫描线求轮廓周长)
题目链接:http://poj.org/problem?id=1177 题目大意:若干个矩形,求这些矩形重叠形成的图形的轮廓周长. 解题思路:这里引用一下大牛的思路:kuangbin 总体思路: 1. ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
随机推荐
- Mycat 水平拆分
一致性Hash理解 https://blog.csdn.net/cywosp/article/details/23397179?utm_source=blogxgwz1 十种 水平拆分 https:/ ...
- jquery实现增删改(伪)-老男孩作业day13
使用jquery进行,文件的编写,实现自增id,删除,添加,编辑模式. jquery放在本地,src="jquery_js.js" 可以改成其他,或者在线的路径 readme &l ...
- U3D 编辑器中sceneview下相机操作相关
前几天在项目中想要实现一个编辑器模式下的3D空间画线功能,几经周折,还是作废. 原因有:相机空间到世界空间转换问题对于Z值不清楚,U3D自定义坐标轴控制问题,射线与平面求交点不对, 一个关键问题是:编 ...
- PHP使用redis防止大并发下二次写入(如如何防止重复下订单)
php调用redis进去读写操作,大并发下会出现:读取key1,没有内容则写入内容,但是大并发下会出现同时多个php进程写入的情况,这个时候需要加一个锁,即获取锁的php进程有权限写. $lock_k ...
- wx小程序自定义组件与页面之间参数传递
在开发中,很多页面中会使用相同的组件,这时可以将具有相同信息的部分封装成一个组件,方便开发中调用.在调用中可能会涉及到数据的传递问题,例如页面与组件,组件与组件直接的数据传递. 首先看看页面与组件直接 ...
- 转: 日期格式参考extjs api文档中的Date类型
var md = new Ext.form.DateField({ //下面的格式是:2000-01-01 00:00:00 format: 'Y-m-d H:i:s', ............ } ...
- JAVAWEB 一一 框架整合(SSH,Spring+Struts2+Hibernate IOC/DI AOP声明式事务处理 定时任务)
package org.springframework.orm.hibernate3; import java.io.Serializable; import java.util.List; impo ...
- 浏览器唤起APP的思路(本文转载)
在做 h5 页面中,会遇到这样一个需求,有一个立即打开的按钮,如果本地安装了我们的 app,那么点击就直接唤起本地 app,如果没有安装,则跳转到下载. 首先想到的是两个问题:一是如何唤起本地 app ...
- h5做列表 水平分割
移动端H5各种各样的列表的制作方法(三) by FungLeo 移动端H5各种各样的列表的制作方法(三) by FungLeo 前情回顾 在上一篇博文<移动端各种各样的列表的制作方法(二)> ...
- 利用等概率Rand5产生等概率Rand3(转)
问题本身很明确,但不知道起个什么题目好,姑且先这么说吧. 问题描述:现在有一个叫做Rand5的函数,可以生成等概率的[0, 5)范围内的随机整数,要求利用此函数写一个Rand3函数(除此之外,不能再使 ...