N - Picture POJ - 1177

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int MAXN = 5010;
struct f
{
	double l, r, h;
	int flag;
} line[MAXN * 2];
struct node
{
	int l, r;
	int lc, rc, num;
	double len;
	int lazy;
} tree[MAXN * 8];
double temp[MAXN * 2];

bool com ( f u, f v )
{
	return u.h < v.h;
}
void build ( int l, int r, int rt )
{
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].len = 0;
	tree[rt].lazy = 0;
	tree[rt].lc = tree[rt].rc = tree[rt].num = 0;
	if ( l + 1 == r )
	{
		return;
	}
	int mid = ( l + r ) >> 1;
	build ( l, mid, rt << 1 );
	build ( mid, r, rt << 1 | 1 );
}
void push ( int rt )
{
	if ( tree[rt].lazy > 0 )
	{
		tree[rt].len = abs(temp[tree[rt].r] - temp[tree[rt].l]);
		tree[rt].lc = tree[rt].l; tree[rt].rc = tree[rt].r;
		tree[rt].num = 1;
	}
	else
	{
		if ( tree[rt].l + 1 == tree[rt].r )
		{
			tree[rt].len = 0;
			tree[rt].lc = 0; tree[rt].rc = 0;
			tree[rt].num = 0;
		}
		else
		{
			tree[rt].len = abs(tree[rt << 1].len + tree[rt << 1 | 1].len);
			tree[rt].lc = tree[rt << 1].lc;
			tree[rt].rc = tree[rt << 1| 1].rc;
			tree[rt].num = tree[rt << 1].num + tree[rt << 1 | 1].num;
			if(tree[rt << 1].rc == tree[rt << 1 | 1].lc && tree[rt << 1].rc != 0)
				tree[rt].num --;
		}
	}
}
void update ( int x, int y, int l, int r, int rt, int q )
{
	if ( l >= x && r <= y )
	{
		tree[rt].lazy += q;
		push ( rt );
		return;
	}
	int mid = ( l + r ) >> 1;
	if ( mid > x )
		update ( x, y, l, mid, rt << 1, q );
	if ( mid < y )
		update ( x, y, mid, r, rt << 1 | 1, q );
	push ( rt );
}
int main()
{
	int n;
	int x1, y1, x2, y2;
	scanf ( "%d", &n );
	int tot = 0;
	for ( int i = 1; i <= n; i ++ )
	{
		scanf ( "%d %d %d %d", &x1, &y1, &x2, &y2 );
		tot ++;
		line[tot].l = x1;
		line[tot].r = x2;
		line[tot].h = y1;
		line[tot].flag = 1;
		temp[tot] = x1;
		tot ++;
		line[tot].l = x1;
		line[tot].r = x2;
		line[tot].h = y2;
		line[tot].flag = -1;
		temp[tot] = x2;
	}
	sort ( temp + 1, temp + tot + 1 );
	sort ( line + 1, line + tot + 1, com );
	int k = 1;
	for ( int i = 2; i <= tot; i ++ )
	{
		if ( temp[i] != temp[i - 1] )
		{
			k ++ ;
			temp[k] = temp[i];
		}
	}
	//cout << k << endl;
	build ( 1, k, 1 );
	int ans = 0;
	//for(int i = 1; i <= k ; i ++)
	//      cout << temp[i] << " " ;
	//cout << endl;
	int lastlen = 0;
	for ( int i = 1; i < tot; i ++ )
	{
		int L = lower_bound ( temp + 1, temp + k + 1, line[i].l ) - temp;
		int R = lower_bound ( temp + 1, temp + k + 1, line[i].r ) - temp;
		//cout << L << " " << R << endl;
		update ( L, R, 1, k, 1, line[i].flag );
		//cout << "!" << tree[1].len << endl;
		//for(int j = 1; j <= 7 ; j ++)
		//      cout << tree[j].num << " " << tree[j].lc << " " << tree[j].rc << endl;
		ans += 1.00 * abs(tree[1].len - lastlen);
		//cout << "!" << ans << endl;
		ans += 2 * abs(line[i + 1].h - line[i].h) * tree[1].num;
		//cout << "!!" << ans << endl;
		lastlen = tree[1].len;
	}
	ans += lastlen;
	printf ( "%d", ans );

	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ant_e_zz/article/details/81431899
今日推荐