HDU 3265 Posters (线段树-扫描线)

Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4389    Accepted Submission(s): 975



Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters.

However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window.

Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes.
 

Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.

The input ends with a line of single zero.
 

Output
For each test case, output a single line with the total area of window covered by posters.
 

Sample Input
  
  
2 0 0 10 10 1 1 9 9 2 2 8 8 3 3 7 7 0
 

Sample Output
  
  
56
 

题意:

题中说要在墙上贴很多张海报,海报都是有残缺的,一般是中间挖了一个矩形的洞,问所有的海报一共覆盖了多少面积。

思路:

需要建立线段树,利用扫描线解题。

如下图:


根据整面墙的Y轴建立线段树,从左往右扫描所有边,红边则在该段(Y轴)上有效长度+1,蓝边则在该段上有效长度-1,当把所有边扫描完即可。


/*************************************************************************
	> File Name: hdu3265.c
	> Author: Bslin
	> Mail: Baoshenglin1994@gmail.com
	> Created Time: 2014年07月21日 星期一 16时35分04秒
 ************************************************************************/

#include <stdio.h>
#include <algorithm>
using namespace std;
#define M 50010

struct node {
	int L, R, len;
	int flag;
} tree[M << 2];

struct Line {
	int y1, y2, x;
	int flag;
	bool operator < (const Line& b) const {
		return x < b.x;
	}
} edge[M * 8];
int tot;

void add(int y1, int y2, int x, int flag) {
	edge[tot].y1 = y1;
	edge[tot].y2 = y2;
	edge[tot].x = x;
	edge[tot++].flag = flag;
}

void Push_UP(int p) {
	if(tree[p].flag) tree[p].len = tree[p].R - tree[p].L;
	else {
		if(tree[p].L + 1 >= tree[p].R) tree[p].len = 0;
		else tree[p].len = tree[p << 1].len + tree[p << 1 | 1].len;
	}
}

void build(int L, int R, int p) {
	tree[p].L = L;
	tree[p].R = R;
	if(L + 1 >= R) {
		return ;
	}
	int mid = (L + R) >> 1;
	build(L, mid, p << 1);
	build(mid, R, p << 1 | 1);
}

void update(int L, int R, int flag, int p) {
	if(L <= tree[p].L && R >= tree[p].R) {
		tree[p].flag += flag;
		Push_UP(p);
		return ;
	}
	int mid = (tree[p].L + tree[p].R) >> 1;
	if(mid >= R) update(L, R, flag, p << 1);
	else if(mid <= L) update(L, R, flag, p << 1 | 1);
	else {
		update(L, mid, flag, p << 1);
		update(mid , R, flag, p << 1 | 1);
	}
	Push_UP(p);
}

int main(int argc, char *argv[]) {
	freopen("in.txt", "r", stdin);
	int n, i;
	int x1, y1, x2, y2, x3, y3, x4, y4;
	long long ans;
	while(scanf("%d", &n) != EOF) {
		if(n == 0) {
			break;
		}
		tot = 0;
		for (i = 0; i < n; ++i) {
			scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
			if(y1 != y4) {
				add(y1, y4, x1, 1);
				add(y1, y4, x3, -1);
			}
			if(y2 != y4) {
				add(y4, y2, x1, 1);
				add(y4, y2, x4, -1);
			}
			if(y1 != y3) {
				add(y1, y3, x3, 1);
				add(y1, y3, x2, -1);
			}
			if(y2 != y3) {
				add(y3, y2, x4, 1);
				add(y3, y2, x2, -1);
			}
		}
		sort(edge, edge + tot);
		build(0, M, 1);
		ans = 0;
		for (i = 0; i < tot; ++i) {
			if(i) {
				ans += (long long)tree[1].len * (edge[i].x - edge[i - 1].x);
			}
			update(edge[i].y1, edge[i].y2, edge[i].flag, 1);
		}
		printf("%I64d\n", ans);  // hdu
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值