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(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
随机推荐
- ArcGIS ArcPy Python处理数据
1.使用搜索游标查看行中的字段值.import arcpy # Set the workspace arcpy.env.workspace = "c:/base/data.gdb" ...
- 打开Linux ftp服务,如:vsftpd: unrecognized service
打开Linux ftp服务,如:vsftpd: unrecognized service [root@BZXXDBS02 ~]# service vsftpd start vsftpd: unre ...
- 嵌入式Linux下Qt的中文显示
一般情况下,嵌入式Qt界面需要中文显示,下面总结自己在项目中用到的可行的办法 1,下载一种中文简体字体,比如我用的是”方正准圆简体“,把字体文件放在ARM开发板系统的Qt字库中,即/usr/lib/f ...
- Effective C++ —— 定制new和delete(八)
STL容器所使用的heap内存是由容器所拥有的分配器对象管理,不是被new和delete直接管理.本章并不讨论STL分配器. 条款49 : 了解new-handler的行为 当operator new ...
- tableView删除功能小记
由于项目需要,做一个UITableView来实现删除功能. 效果如图: 功能思路其实不难: 交代一下,我自己要实现的效果: 1.TableView是分组的. 2.点击删除按钮后,某行被删除. 写完 ...
- jquery单选框radio绑定click事件实现和是否选中的方法
使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项: 1. ...
- 【Android M】预制的 Google GMS包
目录:android/vendor/google/apps .├── AndroidPay│ ├── Android.mk│ ├── AndroidPay_arm64.apk│ ├── A ...
- vux版本升级
一开始用的笨办法, 先卸载npm uninstall vux --save; 然后在安装npm install vux --save; 卸载的还是蛮快的,安装是在下班的时候,让电脑待机2个小时,第二 ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- myBatis调用postgreSQL存储过程
1.调用没有OUT参数的存储过程: 创建存储过程: create or replace function get_code(a1 varchar(32)) returns varchar(32) as ...