POJ-1177 Picture (线段树 求矩形周长)

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 12999 Accepted: 6871

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


#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 5001;
struct Xpoint{
	int x, l, r, v;
	bool operator < (const Xpoint& e)const {
		return x < e.x;
	}
}line[maxn << 1];
struct tree{
	int segN, cover, len, l, r;
}c[maxn << 3];
int y[maxn << 2];
void build(int o, int l, int r){
	c[o].segN = c[o].cover = c[o].len = c[o].l = c[o].r = 0;
	if(l == r - 1) return;
	int mid = l + r >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid, r);
}
void maintain(int o, int l, int r){
	if(c[o].cover > 0){
		c[o].segN = 1;
		c[o].len = y[r] - y[l];
		c[o].l = c[o].r = 1;
		return;
	}
	if(l == r - 1){
		c[o].segN = c[o].cover = c[o].len = c[o].l = c[o].r = 0;
		return;
	}
	c[o].len = c[o << 1].len + c[o << 1 | 1].len;
	c[o].l = c[o << 1].l;
	c[o].r = c[o << 1 | 1].r;
	c[o].segN = c[o << 1].segN + c[o << 1 | 1].segN;
	if(c[o << 1].r == 1 && c[o << 1 | 1].l == 1){
		c[o].segN--;
	}
}
void add(int o, int l, int r, int L, int R, int v){
	if(l >= R || r <= L) return;
	if(l >= L && r <= R){
		c[o].cover += v;
		maintain(o, l, r);
		return;
	}
	int mid = l + r >> 1;
	if(mid >= L) add(o << 1, l, mid, L, R, v);
	if(mid < R) add(o << 1 | 1, mid, r, L, R, v);
	maintain(o, l, r);
}
int main(){
	int n, x1, x2, y1, y2;
	while(scanf("%d", &n) != EOF){
		int tot = 0;
		for(int i = 1; i <= n; ++i){
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			x1 += 10000;
			x2 += 10000;
			y1 += 10000;
			y2 += 10000;

			line[++tot].x = x1;
			line[tot].l = y1;
			line[tot].r = y2;
			line[tot].v = 1;
			y[tot] = y1;

			line[++tot].x = x2;
			line[tot].l = y1;
			line[tot].r = y2;
			line[tot].v = -1;
			y[tot] = y2;
		}
		build(1, 1, tot);
		sort(y + 1, y + 1 + tot);
		sort(line + 1, line + 1 + tot);
		long long ans = 0;
		int last = 0;
		for(int i = 1; i < tot; ++i){
			y1 = lower_bound(y + 1, y + 1 + tot, line[i].l) - y;
			y2 = lower_bound(y + 1, y + 1 + tot, line[i].r) - y;
			add(1, 1, tot, y1, y2, line[i].v);
			ans += c[1].segN * 2 * (line[i + 1].x - line[i].x);
			ans += abs(last - c[1].len);
			last = c[1].len;
		}
		ans += c[1].len;
		printf("%lld\n", ans);
	}
}

/*
题意:
5000个矩阵,求这些矩形组成的图形的周长。

思路:
类似于线段树求面积并,将y轴离散化后映射到线段树上,然后分段求,矩形的左边的表示进入一个
覆盖区域,后边表示走出一个覆盖区域。用cover维护一下当前是否被覆盖。由于在分段统计x轴方向的
边长时需要看现在有多少个不连续段,需要注意分类讨论一下。y轴方向前后计算时只计算多出的或者减少的,
防止重复统计。
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值