我是靠谱客的博主 甜美吐司,最近开发中收集的这篇文章主要介绍POJ1199-Picture(线段树+扫描线),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

题意:

求平面上多个矩形相互覆盖后的周长是多少

思路:

  • 将各个矩形的上下边按纵坐标从小到大排序(相同按入边出边排序,不知道这个有没有影响)。
  • 一次对每一条边进行操作,若是入边,加入线段树中,出边则删除
  • 对于图形的周长,观察可以发现,每次碰到一条边时,加入到线段树/从中删除,前后覆盖区间的长度变化就是该图形的上下边长。而左右部分,可以发现,一段连续的覆盖区间,会贡献两个端点,就有了两端左右边长。所以只需统计有多少段连续区间*2*位移即可。

注意点:

 对于结点的更新,一开始只考虑了覆盖长度。而连续段没有考虑叶子结点的情况,结果re了。pushUp的时候,如果该结点是叶子结点且没有被全覆盖。则它的覆盖长度和连续段数,左右是否为0都是0。因为它没有孩子了,无法从孩子结点推出。否则越界

//#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<map>
#include<stack>
#include<vector>
#define rep(i,e) for(int i=0;i<(e);++i)
#define rep1(i,e) for(int i=1;i<=(e);++i)
#define repx(i,x,e) for(int i=(x);i<=(e);++i)
#define pii pair<int,int>
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
typedef long long ll;
using namespace std;
#ifdef LOCAL
template<typename T>
void dbg(T t){
cout<<t<<" "<<endl;
}
template<typename T, typename... Args>
void dbg(T t, Args... args){
cout<<t<<" ";dbg(args...);
}
#else
#define dbg(...)
#endif // local
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int mod = 1e9+7;
const int N = 1e4+10;
typedef long long ll;
#define K 137
const int maxn = 1e4+10;
const int maxm =4e5+10;
struct Node{
int cover_cnt;
int lc,rc;
int cover_length;
int lines;
}st[maxn*8];
struct Edge{
int x1,x2;
int y;
int isin;
bool operator < (const Edge b) const
{
if(this->y != b.y)
return this->y < b.y;
return this->isin < b.isin;
}
Edge(){}
Edge(int x1,int x2,int y,int isin):
x1(x1),x2(x2),y(y),isin(isin){}
}edge[maxn*2];
void build(int rt,int l,int r)
{
st[rt].cover_cnt = st[rt].cover_length = st[rt].lc = st[rt].rc = st[rt].lines = 0;
if(l == r)
return;
int m = (l+r) >> 1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
}
void pushUp(int rt,int l,int r)
{
//覆盖长度
if(st[rt].cover_cnt > 0)
st[rt].cover_length = r+1 - l;
else if(l == r)
st[rt].cover_length = 0;
else
st[rt].cover_length = st[rt<<1].cover_length + st[rt<<1|1].cover_length;
//连续段
if(st[rt].cover_cnt > 0)
st[rt].lines = 1;
else if(l ==
r)
//一开始没判断
st[rt].lines = 0;
else if(st[rt<<1].rc == st[rt<<1|1].lc && st[rt<<1].rc == 1)
st[rt].lines = st[rt<<1].lines + st[rt<<1|1].lines - 1;
else st[rt].lines = st[rt<<1].lines + st[rt<<1|1].lines;
if(st[rt].cover_cnt > 0)
st[rt].lc = st[rt].rc = 1;
else if(l == r)
//一开始没判断
st[rt].rc = st[rt].lc = 0;
else st[rt].lc = st[rt<<1].lc,st[rt].rc = st[rt<<1|1].rc;
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
if(ql <= l && r <= qr)
{
st[rt].cover_cnt += v;
pushUp(rt,l,r);
return ;
}
int m = (l+r) >> 1;
if(ql <= m)
update(rt<<1,l,m,ql,qr,v);
if(qr > m)
update(rt<<1|1,m+1,r,ql,qr,v);
pushUp(rt,l,r);
}
void work()
{
int n;
while(cin>>n)
{
if(n == 0)
{
cout<<0<<endl;
continue;
}
int x1,x2,y1,y2;
int ec = 0;
for(int i = 0; i < n; i++)
{
cin>>x1>>y1>>x2>>y2;
edge[ec++] = {x1+N,x2+N,y1+N,1};
edge[ec++] = {x1+N,x2+N,y2+N,-1};
}
sort(edge,edge+ec);
build(1,1,2*N);
update(1,1,2*N,edge[0].x1,edge[0].x2-1,edge[0].isin);
long long
res = st[1].cover_length;
for(int i = 1; i < ec; i++)
{
int lines = st[1].lines;
int c1 = st[1].cover_length;
update(1,1,2*N,edge[i].x1,edge[i].x2-1,edge[i].isin);
int c2 = st[1].cover_length;
//dbg(lines,c1,c2);
res = res + (ll)lines*(ll)(edge[i].y-edge[i-1].y)*2 + abs(c1-c2);
}
cout<<res;
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
work();
return 0;
}

 

最后

以上就是甜美吐司为你收集整理的POJ1199-Picture(线段树+扫描线)的全部内容,希望文章能够帮你解决POJ1199-Picture(线段树+扫描线)所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部