地址:http://poj.org/problem?id=1177

题目:

Picture
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 12905   Accepted: 6817

Description

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

Source

 
思路:
  复习下。。。
 

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath> using namespace std; #define MP make_pair
#define PB push_back
#define lc (o<<1)
#define rc (o<<1|1)
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=5e3+;
const int mod=1e9+; struct node
{
int l,r,y,f;
bool operator < (const node &ta)const
{
return y<ta.y;
}
}seg[K*];
int cover[K*],sum[K*],lp[K*],rp[K*],cnt[K*];
int hs[K*];
void push_up(int o,int l,int r)
{
if(cover[o])
sum[o]=hs[r+]-hs[l],lp[o]=rp[o]=cnt[o]=;
else if(l==r)
sum[o]=lp[o]=rp[o]=cnt[o]=;
else
{
sum[o]=sum[lc]+sum[rc];
lp[o]=lp[lc],rp[o]=rp[rc];
cnt[o]=cnt[lc]+cnt[rc]-(rp[lc]&lp[rc]);
}
}
void update(int o,int l,int r,int nl,int nr,int f)
{
if(l==nl&&r==nr)
cover[o]+=f,push_up(o,l,r);
else
{
int mid=l+r>>;
if(nr<=mid) update(lc,l,mid,nl,nr,f);
else if(nl>mid) update(rc,mid+,r,nl,nr,f);
else update(lc,l,mid,nl,mid,f),update(rc,mid+,r,mid+,nr,f);
push_up(o,l,r);
}
}
int main(void)
{
int n;
while(~scanf("%d",&n)&&n)
{
int tot=,ans=;
memset(cover,,sizeof cover);
memset(lp,,sizeof lp);
memset(rp,,sizeof rp);
memset(cnt,,sizeof cnt);
memset(sum,,sizeof sum);
for(int i=,lx,ly,rx,ry;i<=n;i++)
{
scanf("%d%d%d%d",&lx,&ly,&rx,&ry);
seg[tot+]=(node){lx,rx,ly,};
seg[tot+]=(node){lx,rx,ry,-};
hs[tot+]=lx,hs[tot+]=rx;
tot+=;
}
sort(seg+,seg++tot);
sort(hs+,hs++tot);
int sz=unique(hs+,hs++tot)-hs,ls=;
for(int i=;i<=tot;i++)
{
int l=lower_bound(hs+,hs+sz,seg[i].l)-hs;
int r=lower_bound(hs+,hs+sz,seg[i].r)-hs;
update(,,sz,l,r-,seg[i].f);
ans+=abs(ls-sum[]);
if(i!=tot)
ans+=*cnt[]*(seg[i+].y-seg[i].y);
ls=sum[];
}
printf("%d\n",ans);
}
return ;
}

poj1177 Picture 矩形周长并的更多相关文章

  1. 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线

    51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...

  2. POJ-1177 Picture 矩形覆盖周长并

    题目链接:http://poj.org/problem?id=1177 比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)..求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行 ...

  3. P1856 [USACO5.5]矩形周长Picture

    P1856 [USACO5.5]矩形周长Picture $len$            $sum$              $num$             $flag\_l$ $flage\_ ...

  4. 扫描线矩形周长的并 POJ1177

    //扫描线矩形周长的并 POJ1177 // 我是按x轴 #include <iostream> #include <cstdio> #include <cstdlib& ...

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

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

  6. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  7. P1856 [USACO5.5]矩形周长Picture[扫描线]

    题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...

  8. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  9. hdu1828 Picture(线段树+扫描线+矩形周长)

    看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积))  解法一·:两次扫描线 如图我们可以 ...

随机推荐

  1. 1-0 superset的安装和配置

    Superset安装及教程官网(http://airbnb.io/superset/installation.html)讲解的已经够详细的了,本篇以官网教程为蓝本进行说明. 入门 Superset目前 ...

  2. laravel 控制器多个方法共用一个路由

    直接上代码: Route::get('Index/{action}', function(App\Http\Controllers\IndexController $index, $action){ ...

  3. 告别C#,进入了下一个阶段的学习啦

    嘿嘿,今天我们结束了C#的基础的学习,开始啦第二个阶段的学习,就是对SQL Server的学习.今天又是一个周一,又是一个新的开始,感觉我们都是一周一周的计算,而不是每天到这个点就是告别了今天的生活啦 ...

  4. PyQt4进度条QProgressBar

    当我们在处理一个好事较长的任务时,可能就会用到进度条部件.因为使用进度条可以形象告诉用户当前的人物正在进行中.PyQt4工具包提供了水平和垂直两种类型的进度条部件.我们可以设置进度条的最大和最小值,默 ...

  5. shell 强大的awk

    from here 小用法,使用awk来对文件随机抽取n行 awk 'BEGIN{srand()} {print rand()"\t"$0}' input_file | sort ...

  6. OpenStack Cinder 与各种后端存储技术的集成叙述与实践

    先说下下loop设备 loop设备及losetup命令介绍 1. loop设备介绍 在类 UNIX 系统里,loop 设备是一种伪设备(pseudo-device),或者也可以说是仿真设备.它能使我们 ...

  7. Docker源码分析(七):Docker Container网络 (上)

    1.前言(什么是Docker Container) 如今,Docker技术大行其道,大家在尝试以及玩转Docker的同时,肯定离不开一个概念,那就是“容器”或者“Docker Container”.那 ...

  8. ios 图片拉伸不变形的方法

    如果一个椭圆图片,原图大小为30*30,而我们让它显示100*30,那么这个图片就会被拉伸,而且效果很难看.用下边的方法可以创建一个局部不被拉伸的图片. UIImage * buttonBg = [[ ...

  9. 证书:数字签名和验签&加密和解密

    用的是湖北省数字证书认证管理中心的签名和加密 1.带私钥的证书,即p12格式证书(后缀为.pfx) 2.不带私钥的证书,有多种格式,通常我们使用的是cer格式证书(后缀为.cer) 一. 1.什么是对 ...

  10. ORA-01153: an incompatible media recovery is active

    ORA-01153: an incompatible media recovery is active Cause: Attempted to start an incompatible media ...