zoj 1752 Rectangular Rectitude

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
using namespace std;

struct rec {
	int x1, y1, x2, y2, c;
} recs[1001], t1[1000001], t2[1000001];
int num1, num2;

void add(int x1, int y1, int x2, int y2, int c) {
	t2[num2].x1 = x1;
	t2[num2].x2 = x2;
	t2[num2].y1 = y1;
	t2[num2].y2 = y2;
	t2[num2++].c = c;
}

void modify(int k) {
	int i;
	int x1 = recs[k].x1;
	int x2 = recs[k].x2;
	int y1 = recs[k].y1;
	int y2 = recs[k].y2;

	for (i = 0; i < num1; i++) {
		int cx1 = t1[i].x1;
		int cx2 = t1[i].x2;
		int cy1 = t1[i].y1;
		int cy2 = t1[i].y2;

		if (cx1 > x2 || cx2 < x1 || cy1 > y2 || cy2 < y1) {
			t2[num2++] = t1[i];
			continue;
		}

		if (cx1 < x1)add(cx1, cy1, x1, cy2, t1[i].c);
		if (cx2 > x2)add(x2, cy1, cx2, cy2, t1[i].c);

		t1[i].x1 = max(t1[i].x1, x1);
		t1[i].x2 = min(t1[i].x2, x2);

		if (t1[i].y1 < y1)add(t1[i].x1, t1[i].y1, t1[i].x2, y1, t1[i].c);
		if (t1[i].y2 > y2)add(t1[i].x1, y2, t1[i].x2, t1[i].y2, t1[i].c);
	}

	for (i = 0; i < num2; i++) {
		t1[i] = t2[i];
	}
	t1[i] = recs[k];
	num1 = num2 + 1;
	num2 = 0;
}

void solve(int n) {
	num1 = num2 = 0;
	for (int i = 0; i < n; i++) {
		modify(i);
	}
}

int main() {
	int n, c, i, x1, x2, y1, y2, ans,coun,c1;
	coun=0;
	while (scanf("%d%d", &n, &c) && n) {
		
		for (i = 0; i < n; i++) {
			scanf("%d%d%d%d%d", &c1, &x1, &y1, &x2, &y2);
			if (x1 > x2)swap(x1, x2);
			if (y1 > y2)swap(y1, y2);
			recs[i].x1 = x1;
			recs[i].x2 = x2;
			recs[i].y1 = y1;
			recs[i].y2 = y2;
			recs[i].c = c1;
		}
		solve(n);
		ans = 0;
		for (i = 0; i < num1; i++) {
			if (t1[i].c == c) {
				ans+=(t1[i].x2-t1[i].x1)*(t1[i].y2-t1[i].y1);
			}
		}
		
		printf("The area of color %d in dataset %d is: %d\n",c,++coun,ans);

	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值