描述
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

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

Your program is to read from standard input. The first line
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

Your program is to write to standard output. The output must
contain a single line with a non-negative integer which corresponds to
the perimeter for the input rectangles.

Sample Input

7
-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
Sample Output
228
题意
给你N个矩形,每个矩形给你左下和右上端点,求周长并
题解
很容易想到的就是离散化后,先扫一遍x,再扫一遍y
每次加上这次和上次的差即为新加线段的长度
代码
 #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(线段树周长并)的更多相关文章

  1. HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  2. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

  3. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  4. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

  5. Picture POJ - 1177 (线段树-扫描线)

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...

  6. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  7. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  8. POJ 1177 Picture(线段树:扫描线求轮廓周长)

    题目链接:http://poj.org/problem?id=1177 题目大意:若干个矩形,求这些矩形重叠形成的图形的轮廓周长. 解题思路:这里引用一下大牛的思路:kuangbin 总体思路: 1. ...

  9. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

随机推荐

  1. 24_ajax请求_使用axios

    前置说明: 1.React本身只关注页面,并不包含发送ajax请求的代码 2.前端应用需要通过ajax请求与后台进行交互(json数据) 3.React应用中需要集成第三方ajax库(或自己进行封装) ...

  2. Sql Server中日期时间格式化为字符串输出

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  3. 福州大学软件工程W班-助教总结

    背景 福州大学软件工程W班,总人数46人,讲师汪老师. 前期期望 希望自己能够在课程当中起到引导作用,发挥助教最大的用处. 实际执行情况 第一个问题是自动化测试工具,该工具主要是用来测试程序WordC ...

  4. oracle合并语句

    在sql server中的合并语句可以用xml path 详见http://www.cnblogs.com/codeyu/archive/2010/05/25/1743474.html 而oracle ...

  5. Nginx搭建hls流媒体服务器

    第一种方案:ffmpeg+nginx   新的ffmpeg已经支持HLS.(本人也参与了代码供献,给自己做个广告:))   点播:   生成hls分片:   ffmpeg -i <媒体文件> ...

  6. spring boot 集成Thymeleaf

                                           

  7. js中将类数组转换为数组的几种方法

    1.slice方法 最经典的方法,使用Array的slice方法,此方法如果不传参数的话会返回原数组的一个拷贝,因此可以用此方法转换类数组到数组: // 创建一个类数组对象 var alo = {0: ...

  8. 从后台获取的数据渲染到页面中的dom操作

    很多情况下页面dom都是从后台拼接字符串添加生成的新的dom元素,在编辑器中不能看到,只能通过检查看到页面的dom结构,但是这时候会发生一个问题,就是如果使用jQuery无法进行dom操作,事件和方法 ...

  9. hbase 调试各种报错

    1.master is initializing 怎么都不知道怎么回事,直接从hbase 2.0 换到了 hbase 2.1 2.java.lang.ClassNotFoundException: o ...

  10. 解题5(StringMerge1)

    题目描述 按照指定规则对输入的字符串进行处理. 详细描述: 将输入的两个字符串合并. 对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序.这里的下标意思是字符在字符串 ...