描述
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. java中常用工具类

    目录 一. org.apache.commons.io.IOUtils 二. org.apache.commons.io.FileUtils 三. org.apache.commons.lang.St ...

  2. elasticsearch相关

  3. 推荐一款idea 翻译插件 ECTranslation

    无意中看到一款idea翻译插件, ECTranslation,才知道有这么个东西,推荐给看到的人吧,使用简单,值得拥有. 参考:http://p.codekk.com/detail/Android/S ...

  4. vue:vue页面刷新vuex数据消失问题

    vuex中数据刷新页面消失问题:a页面请求的数据保存在vuex中,只要不刷新,那跳转到b页面里也可以用,但如果b页面刷新,那vuex里的数据就会消失,可以得解决方法:a页面用的数据a页面的生命周期函数 ...

  5. 高并发架构技术|缓存失效、缓存穿透问题 PHP 代码解决

    问题描述 缓存失效: 引起这个原因的主要因素是高并发下,我们一般设定一个缓存的过期时间时,可能有一些会设置5分钟啊,10分钟这些:并发很高时可能会出在某一个时间同时生成了很多的缓存,并且过期时间在同一 ...

  6. ReactiveX 学习笔记(15)使用 Rx.NET + Json.NET 调用 REST API

    JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...

  7. Nginx使用GZIP来压缩网页

    HTTP协议上的GZIP编码是一种用来改进web应 用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中 ...

  8. (转)关闭win10的Skype

    https://blog.csdn.net/qq_38285661/article/details/86663849 使用win10的小伙伴们,有没有发现一个不用的功能Skype,假如你想卸载又怕卸不 ...

  9. C++ 关于MFC多线程编程中的一些注意事项 及自定义消息的处理

    在多线程编程中,最简单的方法,无非就是利用 AfxBeginThread  来创建一个工作线程,看一下这个函数的说明: CWinThread* AFXAPI AfxBeginThread( AFX_T ...

  10. spark LBFGS 设置参数

    http://blog.csdn.net/bon_mot/article/details/72461318