upc 3030 ArtWork(并查集)

                                                        3030: ArtWork

                                                                    时间限制: 4 Sec  内存限制: 128 MB
 

题目描述

A template for an artwork is a white grid of n × m squares. The artwork will be created by painting q horizontal and vertical black strokes. A stroke starts from square (x 1 , y 1 ), ends at square (x 2 , y 2 ) (x 1 = x 2 or y 1 = y 2 ) and changes the color of all squares (x, y) to black where
x 1 ≤ x ≤ x 2 and y 1 ≤ y ≤ y 2 .

The beauty of an artwork is the number of regions in the grid. Each region consists of one or more white squares that are connected to each other using a path of white squares in the grid, walking horizontally or vertically but not diagonally. The initial beauty of the artwork is 1. Your task is to calculate the beauty after each new stroke. Figure A.1 illustrates how the beauty of the artwork varies in Sample Input 1.

 

输入

The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 1000, 1 ≤ q ≤ 104 ).
Then follow q lines that describe the strokes. Each line consists of four integers x 1 , y 1 , x 2 and y 2 (1 ≤ x 1 ≤ x 2 ≤ n, 1 ≤ y 1 ≤ y 2 ≤ m). Either x 1 = x 2 or y 1 = y 2 (or both).

 

输出

For each of the q strokes, output a line containing the beauty of the artwork after the stroke.

 

样例输入

4 6 5
2 2 2 6
1 3 4 3
2 5 3 5
4 6 4 6
1 6 4 6

 

样例输出

1
3
3
4
3

 

 

 

 

 

题目大意:

n行m列的矩阵(行列与平常的恰好相反),  q次询问, 每次给出起点和终点的坐标, 将其之间的方格涂黑。 每次询问输出白色联通块的个数。

 

 

 

 

 

分析:

按照题意将方格涂黑再求联通块并不好求, 所以我们可以倒着来, 先求出最终矩阵的白色联通块, 然后将黑块涂白, 用并查集来维护白色联通块的个数。

 

 

 

 

 

AC代码:

//Author       :	NIYOUDUOGAO
//Last modified:	2018-10-09 20:26
//Email        :	pjm000@163.com
//Filename     :	t1.cc
#include <bits/stdc++.h>
#define mset(a, x) memset(a, x, sizeof(a))
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;
int n, m, q, cnt;
int pre[N * N], ans[N * 10], mp[N][N];
struct node {
	int sx, sy, ex, ey;
}a[N * 10];
void init() {
	for (int i = 0; i < N * N; i++) {
		pre[i] = i;
	}
}
int Find(int x) {
	int r = x;
	while(pre[r] != r) {
		r = pre[r];
	}
	int i = x, j;
	while(i != r) {
		j = pre[i];
		pre[i] = r;
		i = j;
	}
	return r;
}
void mix(int y, int x) {
	int fx = Find(x), fy = Find(y);
	if(fx != fy) {
		pre[fx] = fy;
	}
}
void work(int x, int y) {//   行列
	if (x - 1 >= 1 && mp[y][x - 1] == 0) {
		if (Find((x - 2) * m + y) != Find((x  - 1) * m + y)) {
			cnt--;
			mix(Find((x - 2) * m + y), Find((x - 1) * m + y));
		}
	}
	if (y - 1 >= 1 && mp[y - 1][x] == 0) {
		if (Find((x - 1) * m + y - 1) != Find((x - 1) * m + y)) {
			cnt--;
			mix(Find((x - 1) * m + y - 1), Find((x - 1) * m + y));
		}
	}
	if (x + 1 <= n && mp[y][x + 1] == 0) {
		if (Find((x - 1) * m + y) != Find(x * m + y)) {
			cnt--;
			mix(Find(x * m + y), Find((x - 1) * m + y));
		}
	}
	if (y + 1 <= m && mp[y + 1][x] == 0) {
		if (Find((x - 1) * m + y) != Find((x - 1) * m + y + 1)) {
			cnt--;
			mix(Find((x - 1) * m + y + 1), Find((x - 1) * m + y));
		}
	}
}
int main() {
	std::ios::sync_with_stdio(false);
	init();
	mset(mp, 0);
	cin >> n >> m >> q;
	// 右边是行   左边是列
	for (int i = 0; i < q; i++) {
		cin >> a[i].sx >> a[i].sy >> a[i].ex >> a[i].ey;//  记录下起点终点
		if (a[i].sx == a[i].ex) {  // 行相等
			for (int j = min(a[i].sy, a[i].ey); j <= max(a[i].sy, a[i].ey); j++) {
				mp[j][a[i].sx]++;
			}
		} else {
			for (int j = min(a[i].sx, a[i].ex); j <= max(a[i].sx, a[i].ex); j++) {
				mp[a[i].sy][j]++;
			}
		}
	}
	//   求一下最终图形的联通块个数
	for (int j = 1; j <= n; j++) {//   行
		for (int i = 1; i <= m; i++) {//  列
			if (mp[i][j] == 0) {
				if (i - 1 >= 1 && mp[i - 1][j] == 0) {
					mix(i - 1 + (j - 1) * m, i + (j - 1) * m);
				}
				if (j - 1 >= 1 && mp[i][j - 1] == 0) {
					mix(i + (j - 1) * m, i + (j - 2) * m);
				}
			}
		}
	}
	cnt = 0;
	for (int j = 1; j <= n; j++) {
		for (int i = 1; i <= m; i++) {
			if (mp[i][j] == 0 && Find((j - 1) * m + i) == (j - 1) * m + i) {
				cnt++;
			}
		}
	}
	ans[q] = cnt;
	int tmp = q;
	while (q--) {
		if (q == 0) break;
		if (a[q].sx == a[q].ex) {//   行相等
			for (int j = min(a[q].sy, a[q].ey); j <= max(a[q].sy, a[q].ey); j++) {
				mp[j][a[q].sx]--;
			}
			for (int j = min(a[q].sy, a[q].ey); j <= max(a[q].sy, a[q].ey); j++) {
				if (mp[j][a[q].sx] == 0) {
					cnt++;
				}
			}
			for (int j = min(a[q].sy, a[q].ey); j <= max(a[q].sy, a[q].ey); j++) {
				if (mp[j][a[q].sx] == 0) {
					work(a[q].sx, j);//  传值行列
				}
			}
		} else {//   列相等
			for (int j = min(a[q].sx, a[q].ex); j <= max(a[q].sx, a[q].ex); j++) {
				mp[a[q].sy][j]--;
			}

			for (int j = min(a[q].sx, a[q].ex); j <= max(a[q].sx, a[q].ex); j++) {
				if (mp[a[q].sy][j] == 0) {
					cnt++;
				}
			}
			for (int j = min(a[q].sx, a[q].ex); j <= max(a[q].sx, a[q].ex); j++) {
				if (mp[a[q].sy][j] == 0) {
					work(j, a[q].sy);//  传值行列
				}
			}
		}
		ans[q] = cnt;
	}
	for (int i = 1; i <= tmp; i++) {
		cout << ans[i] << "\n";
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值