CSUOJ 2022: Artwork 暴力 并查集

这篇博客介绍了一个艺术品绘制的问题,初始为白色网格,通过画横纵黑色线条改变颜色,每次画线后需要计算网格中白色区域的连通块数量。题目要求逆序处理操作以使用并查集避免高复杂度,最终实现复杂度不超过1e7,适合4秒的时间限制。
摘要由CSDN通过智能技术生成

Description

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 (x1, y1), ends at square (x2,y2)(x1=x2
or y1=y2)

and changes the color of all squares (x, y) to black where x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. 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.
Input

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 integersx1,y1,x2
and y2(1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m). Either x1 = x2 or y1=y2

(or both).
Output

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

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

Sample Output

1
3
3
4
3

题意:给出一个n列m行(md, 找bug找了半天 发现题目读错了)的矩阵 有Q次操作 操作会在x1 y1 到x2 y2的连线上设置一层障碍物,在每次操作后,输出当前联通块的个数。
解题思路:
既然要求输出联通块的个数,很容易联想到并查集,但是这里正着去遍历操作的话是没法用并查集写 因为有删除操作。如果强行正序遍历的话,每次操作就要遍历一次图 复杂度O(n*m*q) 。
考虑以逆序遍历操作。 这样就只有添加操作了。 最多1e4次操作 每次操作最多处理1000个点,每个点最多要与它周围的四个点进行合并操作。复杂度小

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值