我是靠谱客的博主 跳跃万宝路,最近开发中收集的这篇文章主要介绍POJ - 1177 Picture —— 扫描线求周长,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Picture

Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 13565 Accepted: 7145

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

题意:

求给定多个矩形并的周长

思路:

扫描线

横向的长度是上一次查询时的总区间长度减掉当前查询时的长度

用ml,mr表示当前区间的左端点处和右端点处是否有竖向的边,用mm求和,表示区间内需要计算的竖向边的条数

竖向的长度是两个边的高度差乘根节点里的mm

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <bitset>
#define max_ 5010
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
#define mod 364875103
using namespace std;
struct node
{
int l,r;
int s;
int len;
int ml,mr,mm;
};
struct node tree[max_*8];
struct edge
{
int l,r;
int h;
int f;
bool operator < (const edge &a)const
{
return h<a.h;
}
};
struct edge e[max_*2];
int n;
int x[max_*2];
void built(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].s=tree[i].len=tree[i].mm=tree[i].ml=tree[i].mr=0;
if(l+1==r)
return;
int mid=(l+r)>>1;
built(i<<1,l,mid);
built(i<<1|1,mid,r);
}
void up(int i)
{
if(tree[i].s>=1)
{
tree[i].len=x[tree[i].r]-x[tree[i].l];
tree[i].mm=tree[i].ml=tree[i].mr=1;
}
else if(tree[i].l+1==tree[i].r)
{
tree[i].len=0;
tree[i].mm=tree[i].ml=tree[i].mr=0;
}
else
{
tree[i].len=tree[i<<1].len+tree[i<<1|1].len;
tree[i].ml=tree[i<<1].ml;
tree[i].mr=tree[i<<1|1].mr;
tree[i].mm=tree[i<<1].mm+tree[i<<1|1].mm-tree[i<<1].mr*tree[i<<1|1].ml;
}
}
void updata(int i,int l,int r,int v)
{
if(tree[i].l==l&&tree[i].r==r)
{
tree[i].s+=v;
up(i);
return;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
updata(i<<1,l,r,v);
else if(l>=mid)
updata(i<<1|1,l,r,v);
else
{
updata(i<<1,l,mid,v);
updata(i<<1|1,mid,r,v);
}
up(i);
}
int main(int argc, char const *argv[]) {
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int p=i*2-1;
int q=i*2;
e[p].l=e[q].l=x1;
e[p].r=e[q].r=x2;
e[p].h=y1;
e[p].f=1;
e[q].h=y2;
e[q].f=-1;
x[p]=x1;
x[q]=x2;
}
sort(x+1,x+1+n*2);
sort(e+1,e+1+n*2);
built(1,1,2*n);
int pre=0,ans=0;
e[0].h=e[1].h;
for(int i=1;i<=2*n;i++)
{
int l=lower_bound(x+1,x+1+2*n,e[i].l)-x;
int r=lower_bound(x+1,x+1+2*n,e[i].r)-x;
ans+=2*tree[1].mm*(e[i].h-e[i-1].h);
updata(1,l,r,e[i].f);
ans+=e[i].f*(tree[1].len-pre);
pre=tree[1].len;
}
printf("%dn",ans );
}
return 0;
}

 

最后

以上就是跳跃万宝路为你收集整理的POJ - 1177 Picture —— 扫描线求周长的全部内容,希望文章能够帮你解决POJ - 1177 Picture —— 扫描线求周长所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部