地址: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. 第十六篇:初探IO复用

    前言 在之前的文章中,我具体实现了一个并发回射服务器并给它加载了僵尸子进程的自动清理信号机制.在正常情况下,它已经可以很好地工作了,但它能否合理应对一些特殊情况呢? 问题发现 先来看看当服务器的客户子 ...

  2. LNMP redis 安装、PHPredis扩展配置、服务器自启动、redis认证密码

    背景: LNMP 环境(centos7) 一. 安装redis 1.下载,解压,编译 $ cd /usr/local$ wget http://download.redis.io/releases/r ...

  3. HTML - 分页效果布局

    <p class="jcFY"> 显示 <select name="" id=""> <option valu ...

  4. 【BZOJ4337】BJOI2015 树的同构 括号序列

    [BZOJ4337]BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱 ...

  5. 【BZOJ1834】[ZJOI2010]network 网络扩容 最大流+最小费用流

    [BZOJ1834][ZJOI2010]network 网络扩容 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不 ...

  6. Service简介 demos

    extends:http://blog.csdn.net/ithomer/article/details/7364024 一. Service简介 Service是android 系统中的四大组件之一 ...

  7. 微信小程序 --- 绘画

    cavans及context详解 绘画API的使用 游戏的制作

  8. 微信小程序 --- 获取网络状态

    获取网络状态:wx.getNetworkType btnclick:function(){ wx.getNetworkType({ success:function(res){ console.log ...

  9. numpy基础入门

    1.Numpy是什么 很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过nu ...

  10. ArcGIS API for JavaScript开发笔记(一)——ArcGIS for Javascript API 3.14本地部署

    堪称史上最详细的< ArcGIS forJavascript API 3.14本地部署>文档,有图有真相~~~ ---------环境:Windows server 2012R2,IIS ...