ArtWork+并查集二维

本文深入探讨了ArtWork绘画算法的实现细节,该算法通过在网格上绘制黑色线条来改变白色区域的数量,以此衡量艺术品的美观程度。文章详细介绍了算法的工作原理,包括如何计算网格上每个新画笔触后的白色区域数量变化,以及如何使用路径搜索和并查集数据结构来高效处理大量绘画操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 A: ArtWork

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

题目描述

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


#include<bits/stdc++.h> 
using namespace std;

struct stock{
	int x1,x2,y1,y2;
}pos[10010];
const int N=1010;

int num[N*N],fa[N*N],ans[10010];
int dir[4][2]={0,1,-1,0,0,-1,1,0};
int m,n,p,t;
int hsh(int x,int y){
	int num=(x-1)*m+y;
	return num;
}

void init(){
	for(int i=1;i<=n*m;i++){
		fa[i]=i;
		num[i]=0;
	}
}

int find(int x){
	return x==fa[x]?x:fa[x]=find(fa[x]);
}

void merge(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx==fy)return;
	t--;
	fa[fx]=fy;
}
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 xx=x+dir[i][0];
		int yy=y+dir[i][1];
		if(check(xx,yy)&&!num[hsh(xx,yy)]){
			merge(hsh(xx,yy),hsh(x,y));
		}
	}
}
int main() {
    scanf("%d%d%d",&n,&m,&p);
    t=n*m;
    init();
    for(int i=1;i<=p;i++){
    	scanf("%d%d%d%d",&pos[i].x1,&pos[i].y1,&pos[i].x2,&pos[i].y2);
    	for(int x=pos[i].x1;x<=pos[i].x2;x++)
    	for(int y=pos[i].y1;y<=pos[i].y2;y++){
    		if(num[hsh(x,y)]==0)t--;
    		num[hsh(x,y)]++; 
		}
	}
	
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	if(!num[hsh(i,j)])
		work(i,j);
	
	for(int i=p;i>0;i--){
		ans[i]=t;
		for(int x=pos[i].x1;x<=pos[i].x2;x++)
    	for(int y=pos[i].y1;y<=pos[i].y2;y++){
    		num[hsh(x,y)]--; 
			if(num[hsh(x,y)]==0){
				t++;
				work(x,y);
			}
		}
	}
	for(int i=1;i<=p;i++){
		printf("%d\n",ans[i]);
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值