POJ 1177 Picture 线段树+扫描线

题目链接:POJ 1177 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即可。
思路还是用两条边来替换一个矩形,且权值分别为1和-1,然后线段树维护区间被覆盖的次数 c n t cnt cnt和覆盖的总长度 l e n len len
一个直观的感受是符合周长的边是从线段树上的一些区间从无到有,即本来值为0的数组后来被覆盖了。直接进行这一统计其实有一丢丢难,我们可以转换一下,每次计算加入一条边后被覆盖区间增加的长度,最后横着扫一遍,竖着扫一遍再乘二即可。
注意一个小细节:有可能横坐标相等的两条边权值相反,且他们覆盖的区间有某一子区间本来是被覆盖了一次。那么这两条边其实对周长是没有贡献的,但是更新不当极其容易把该子区间算作边长。解决方法其实很简单:只要排序的时候对于横坐标相同的边按权值由大到小排序即可,纵向同理。
AC代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstring>
using namespace std;

const int maxn = 5010;
int sx[maxn*2], sy[maxn*2];
struct node{
	int cnt, len;
} tree[maxn*16];
struct lne{
	int x, d, u;
	int val;
	bool operator < (const lne &oth) const{
		if(x == oth.x) return val > oth.val;
		else return x < oth.x; 
	}
} l[maxn*2];
struct vec{
	int x1, x2, y1, y2;
} v[maxn];

void build(int ll, int r, int dex, int *m)
{
	tree[dex].len = tree[dex].cnt = 0;
	if(ll != r){
		int mid = (ll+r) >> 1;
		build(ll, mid, dex*2, m), build(mid+1, r, dex*2+1, m);
	}
}
void update(int ll, int r, int dex, int x, int y, int val, int *m)
{
	if(ll > y || r < x) return;
	else if(ll >= x && r <= y) tree[dex].cnt += val;
	else{
		int mid = (ll+r) >> 1;
		update(ll, mid, dex*2, x, y, val, m), update(mid+1, r, dex*2+1, x, y, val, m);
		tree[dex].len = tree[dex*2].len + tree[dex*2+1].len;
	}
	if(tree[dex].cnt > 0) tree[dex].len = m[r+1] - m[ll];
	else tree[dex].len = (ll==r?0:tree[dex*2].len+tree[dex*2+1].len);
}

int main()
{
	int res = 0, pre = 0;
	int n, x1, x2, y1, y2;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d%d%d%d", &v[i].x1, &v[i].y1, &v[i].x2, &v[i].y2),
		sx[i] = v[i].x1, sx[i+n] = v[i].x2, sy[i] = v[i].y1, sy[i+n] = v[i].y2;
	sort(sx+1, sx+1+2*n), sort(sy+1, sy+1+n*2);
	int xx = unique(sx+1, sx+1+2*n) - sx-1, yy = unique(sy+1, sy+1+2*n) - sy-1;
	for(int i = 1; i <= n; i++)
		v[i].x1 = lower_bound(sx+1, sx+1+xx, v[i].x1) - sx,
		v[i].x2 = lower_bound(sx+1, sx+1+xx, v[i].x2) - sx,
		v[i].y1 = lower_bound(sy+1, sy+1+yy, v[i].y1) - sy,
		v[i].y2 = lower_bound(sy+1, sy+1+yy, v[i].y2) - sy;
	for(int i = 1; i <= n; i++){
		l[i].x = v[i].x1, l[i].d = v[i].y1, l[i].u = v[i].y2;
		l[i+n] = l[i], l[i+n].x = v[i].x2;
		l[i].val = 1, l[i+n].val = -1;
	}
	sort(l+1, l+1+2*n);
	build(1, yy-1, 1, sy);
	for(int i = 1; i <= 2*n; i++){
		update(1, yy-1, 1, l[i].d, l[i].u-1, l[i].val, sy);
		res += max(0, tree[1].len-pre);
		pre = tree[1].len;
	}
	
	pre = 0;
	for(int i = 1; i <= n; i++){
		l[i].x = v[i].y1, l[i].d = v[i].x1, l[i].u = v[i].x2;
		l[i+n] = l[i], l[i+n].x = v[i].y2;
		l[i].val = 1, l[i+n].val = -1;
	}
	sort(l+1, l+1+n*2);
	build(1, xx-1, 1, sx);
	for(int i = 1; i <= 2*n; i++){
		update(1, xx-1, 1, l[i].d, l[i].u-1, l[i].val, sx);
		res += max(0, tree[1].len - pre);
		pre = tree[1].len;
	}
	printf("%d\n", res*2);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值