POJ1177----Picture

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 10727 Accepted: 5662

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

Source

IOI 1998

矩形周长并,  和面积并差不多, 每新插入一条边,周长的增量就是上一次总长和这一次总长的差值的绝对值, 我是先算横边,再算纵边的, 在纸上画一下图就知道了

/*************************************************************************
    > File Name: POJ1177.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月15日 星期四 15时13分56秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 20110;

struct node
{
	int l, r;
	int len;
	int add;
}tree[N << 2];

struct seg
{
	int l, r;
	int h;
	int flag;
}lines[N << 1], lines2[N << 1];

int cmp (seg a, seg b)
{
	if (a.h != b.h)
	{
		return a.h < b.h;
	}
	return a.flag > b.flag;
}

void build (int p, int l, int r)
{
	tree[p].l = l;
	tree[p].r = r;
	tree[p].len = 0;
	tree[p].add = 0;
	if (l == r)
	{
		return;
	}
	int mid = (l + r) >> 1;
	build (p << 1, l, mid);
	build (p << 1 | 1, mid + 1, r);
}

void pushup (int p)
{
	if (tree[p].add)
	{
		tree[p].len = tree[p].r - tree[p].l + 1;
	}
	else if (tree[p].l == tree[p].r)
	{
		tree[p].len = 0;
	}
	else
	{
		tree[p].len = tree[p << 1].len + tree[p << 1 | 1].len;
	}
}

void update (int p, int l, int r, int val)
{
	if (l == tree[p].l && r == tree[p].r)
	{
		tree[p].add += val;
		pushup (p);
		return;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (r <= mid)
	{
		update (p << 1, l, r, val);
	}
	else if (l > mid)
	{
		update (p << 1 | 1, l, r, val);
	}
	else
	{
		update (p << 1, l, mid, val);
		update (p << 1 | 1, mid + 1, r, val);
	}
	pushup (p);
}

int main()
{
	int n;
	int x1, y1, x2, y2;
	while (~scanf("%d", &n))
	{
		build (1, 1, N);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			x1 += 10001;
			x2 += 10001;
			lines[i].flag = 1;
			lines[i].l = x1;
			lines[i].r = x2;
			lines[i].h = y1;
			lines[i + n].flag = -1;
			lines[i + n].l = x1;
			lines[i + n].r = x2;
			lines[i + n].h = y2;
			y1 += 10001;
			y2 += 10001;
			lines2[i].flag = 1;
			lines2[i].l = y1;
			lines2[i].r = y2;
			lines2[i].h = x1;
			lines2[i + n].flag = -1;
			lines2[i + n].l = y1;
			lines2[i + n].r = y2;
			lines2[i + n].h = x2;
		}
		sort (lines + 1, lines + 2 * n + 1, cmp);
		sort (lines2 + 1, lines2 + 2 * n + 1, cmp);
		int  ans = 0;
		for (int i = 1; i <= 2 * n; ++i)
		{
			int last = tree[1].len;
			update (1, lines[i].l, lines[i].r - 1, lines[i].flag);
			ans += abs(last - tree[1].len);
		}
		build (1, 1, N);
		for (int i = 1; i <= 2 * n; ++i)
		{
			int last = tree[1].len;
			update (1, lines2[i].l, lines2[i].r - 1, lines2[i].flag);
			ans += abs(last - tree[1].len);
		}
		printf("%d\n", ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值