地址: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. 关于直播学习笔记-003-nginx-rtmp、srs、vlc、obs

    服务器 1.nginx-rtmp:https://github.com/illuspas/nginx-rtmp-win32 2.srs:https://github.com/illuspas/srs- ...

  2. 使用keytool生成密钥对

    1.首先要用KeyTool工具来生成私匙库:(-alias别名 –validity 3650表示10年有效) keytool -genkey -alias privatekey -keystore p ...

  3. C文件流

    在Linux系统中,系统默认认为每个进程打开了3个文件,即每个进程默认可以操作3 个流,即标准输入了流(/dev/stdin),标准输出流(/dev/stdout),标准错误输出流(/dev/stde ...

  4. android 仿微信聊天界面,以及语音录制功能

    extends:http://104zz.iteye.com/blog/1709840 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图:     第一:chat.xml设计 ...

  5. zeptojs使用及介绍,手机端为什么一定要用zeptojs?

    zepto是和jQuery和类似的框架,由于jQuery内置了很多解决浏览器兼容性的代码,导致jQuery过于臃肿,对于移动端来说,不需要太过于考虑浏览器的兼容,所以就诞生zeptojs. 总的来说: ...

  6. 学习认识Spring原理

    学习认识Spring原理 Spring 是一种业务层框架.搭建Spring框架需要Spring开发包和commons-logging包.Spring的核心思想是控制反转也称依赖注入(创建者--(实例) ...

  7. Lucene.net之解决锁的问题

    public sealed class SearchIndexManager { private static readonly SearchIndexManager searchManager=ne ...

  8. Oracle数据库误删文件导致rman备份报错RMAN-06169解决办法

    Oracle数据库误删文件导致rman备份报错RMAN-06169解决办法 可能是误删文件导致在使用rman备份时候出现以下提示 RMAN-06169: could not read file hea ...

  9. Systemd unit generators unit

    systemd.generator(7) - Linux manual page http://man7.org/linux/man-pages/man7/systemd.generator.7.ht ...

  10. GraphicsMagick 号称图像处理领域的瑞士军刀

    标签: librarydelegatesimage图像处理fontstiff 2012-09-13 10:15 2496人阅读 评论(0) 收藏 举报  分类: java技术(52)  简介      ...