poj1177 Picture 矩形周长并
地址:http://poj.org/problem?id=1177
题目:
| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 12905 | Accepted: 6817 |
Description
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
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Output
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 矩形周长并的更多相关文章
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- POJ-1177 Picture 矩形覆盖周长并
题目链接:http://poj.org/problem?id=1177 比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)..求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行 ...
- P1856 [USACO5.5]矩形周长Picture
P1856 [USACO5.5]矩形周长Picture $len$ $sum$ $num$ $flag\_l$ $flage\_ ...
- 扫描线矩形周长的并 POJ1177
//扫描线矩形周长的并 POJ1177 // 我是按x轴 #include <iostream> #include <cstdio> #include <cstdlib& ...
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并
题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- P1856 [USACO5.5]矩形周长Picture[扫描线]
题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
随机推荐
- GIS-013-Cesium Terrain 数据生成
一.Python 1.修改Python脚本文件 if __name__=='__main__': #sys.argv = ['F:\\000_Terrain\\T7-gdal2srtmtiles-de ...
- #pragam预处理分析
#pragma是编译器指示字,用域指示编译器完成一些特定动作, #pragma所定义的很多指示字是编译器和操作系统特有的 #pragma在不同的编译器间是不可移植的 预处理器将忽略它不认识的#prag ...
- c语言基础知识要点
C语言程序的构成 与C++.Java相比,C语言其实很简单,但却非常重要.因为它是C++.Java的基础.不把C语言基础打扎实,很难成为程序员高手. 一.C语言的结构 先通过一个简单的例子,把C语言的 ...
- C++说明符和限定符
有些被称为存储说明符(storage class specifier)或cv-限定符(cv-qualifier)的C++关键字提供了一些有关存储的信息.下面是存储所说明符:* auto (在C++11 ...
- [译] 关于CSS中的float和position
原文 http://learn.shayhowe.com/advanced-html-css/detailed-css-positioning 当构建页面排版时,有不同的方法可以使用.使用哪一种方法取 ...
- Androidの共享登录之方案研究
由于最近公司提到了一个需求是,一个应用登录成功了,另一个自动登录. 绞尽脑汁想了好几天,看起来很容易但是想深点就漏洞百出,有的时候代码都写完了测试都成功了突然发现给一个假设就完全失效. 先前几个同事之 ...
- django 自定模板标签的注册
首先注册方法一般都是先实例化一个template.Library.如: from django import template register = template.Library() 1.注册自定 ...
- linux如何设置用户权限
linux与用户权限设置: 1.添加用户 首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy //修改密码 ...
- 310实验室OTL问题(2)
1.PyOptimization代码学习心得 (1).该部分由三个分块组成(按完成的相应的任务).第一部分,运行函数. 以optimization.py为入口程序,其思路是:首先获取路径,然后,初始化 ...
- 【php】---mysql---基本操作及使用---【巷子】
1.数据库简介 (1).什么是数据库? 一个文件 一个文件夹 一个u盘 一个硬盘......都叫做数据库 存放数据的仓库 (2).常见的数据库? mySql sql ...