ArtWork(并查集--二维降一维)

3030: ArtWork

时间限制: 4 Sec  内存限制: 128 MB
提交: 49  解决: 22
[提交] [状态] [讨论版] [命题人:外部导入]

题目描述

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

代码魔改自这里、

那个博主成功的写出来了我的想法,于是我魔改了他的代码

想法很简单,首先这题肯定是并查集没错了,但是二维并查集并不会写,那么就要想办法降维,这就是那个Hash的由来

降维之后呢,因为还是四个方向的,所以方向数组还是要有的,还有就是逆向思维从最后一个图向前扫描

因为想学世界语所以写了个varii

#include<bits/stdc++.h>
#define Maxn 1010
struct Stock{
	int x1, x2, y1, y2;
}varii[10010];
int vis[Maxn*Maxn], Fa[Maxn*Maxn],ans[10010];
int dir[4][2] = { 0, 1, -1, 0, 0, -1, 1, 0 };
int m, n, p,t;
int Hash(int x, int y){
   return (x - 1)*m + y;
}
void init(){
	for (int i = 1; i <= n*m; i++)
		Fa[i] = i,vis[i] = 0;
}
int Find(int x){
	return x == Fa[x] ? x : Fa[x] = Find(Fa[x]);
}
void Merge(int x, int y){
	x = Find(x), y = Find(y);
	if (x == y)return;
	Fa[x] = y;t--;
}
bool Check(int x, int y){
	if (x >= 1 && y >= 1 && x <= n&&y <= m)
		return true;
	return false;
}
void Work(int x,int y){
	for (int i = 0; i < 4; i++){
		int tx = x + dir[i][0],ty = y + dir[i][1];
		if (Check(tx, ty) && !vis[Hash(tx,ty)])
			Merge(Hash(tx,ty), Hash(x,y));
	}
}
void Sovle(){
    for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (!vis[Hash(i,j)])
				Work(i, j);
    for (int i = p; i ; i--){
		ans[i] = t;
		for (int x = varii[i].x1; x <= varii[i].x2; x++)
			for (int y = varii[i].y1; y <= varii[i].y2; y++){
				vis[Hash(x, y)]--;
				if (vis[Hash(x, y)] == 0)
					t++,Work(x, y);
			}
	}
}
int main()
{
	scanf("%d %d %d", &n, &m, &p);t = m*n;init();
	for (int i = 1; i <= p;i++){
		scanf("%d%d%d%d", &varii[i].x1, &varii[i].y1, &varii[i].x2, &varii[i].y2);
        for (int x = varii[i].x1; x <= varii[i].x2; x++)
            for (int y = varii[i].y1; y <= varii[i].y2; y++){
				if (vis[Hash(x, y)] == 0)t--;
				vis[Hash(x, y)]++;
			}
	}
	Sovle();
	for (int i = 1; i <= p; i++)
		printf("%d\n", ans[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/82974761