题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2022

题目:
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
Hint
Source
NCPC 2016

题意: 首先给你一个n*m大小的图,有几个查询,每个查询,会输入两个在一条直线上的点(可以是同一个点),这两个点之间的点和自己会被染色,然后求没有被染色的连通的区域有多少个,前面被染色的点,后面还是在其它查询里还是会存在。

思路: 首先储存好所有的查询,并把所有查询中要染色的点 全部染色,然后从最后一个查询开始反向遍历,让查询中 被染色的点变为不染色,并把此时的连通区域存起来。使用并查集处理连通区域很方便。 因为这个图的坐标x,y值是相反的,很不适应,我就把所有格子从左上到右下从数字1开始标记,直到n*m。

代码:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>using namespace std;const int maxn =10000+5;
const int maxn1 =1000000+5;
int vis[maxn1];//标记数组
int fa[maxn1],son[maxn1];
int n,m,q,cnt,ans;int find_(int x)
{return x==fa[x]? x: x=find_(fa[x]);
}struct query//几次查询染色
{int x,y,x1,y1,sum;//染色的起点坐标和终点坐标、答案。} Q[maxn];void solve(int a,int b)
{int x,y;x=find_(a);y=find_(b);if(x==y)return;ans--;if(son[x]>son[y]){fa[y]=x;son[x]+=son[y];}else{fa[x]=y;son[y]+=son[x];}
}void union_(int i)
{ans++;son[i]=1;if(i+1<=n*m&&vis[i+1]==0){if(i%n!=0)solve(i,i+1);}if(i-1>=1&&vis[i-1]==0){if(i%n!=1)solve(i-1,i);}if(i+n<=n*m&&vis[i+n]==0){solve(i,i+n);}if(i-n>=1&&vis[i-n]==0){solve(i-n,i);}return;
}int main()
{// freopen("in.txt","r",stdin);while(scanf("%d%d%d",&n,&m,&q)!=EOF){memset(son,0,sizeof(son));cnt=0;ans=0;for(int i=1; i<=n*m; i++){vis[i]=0;fa[i]=i;}for(int i=1; i<=q; i++){scanf("%d%d%d%d",&Q[i].x,&Q[i].y,&Q[i].x1,&Q[i].y1);Q[i].sum=0;if(Q[i].x==Q[i].x1){for(int j=(Q[i].y-1)*n+Q[i].x; j<=(Q[i].y1-1)*n+Q[i].x1; j+=n)vis[j]++;//一个格子可以多次被染色,所以我标记一下,当vis==0时,证明没有被染色了。}else if(Q[i].y==Q[i].y1){for(int j=(Q[i].y-1)*n+Q[i].x; j<=(Q[i].y1-1)*n+Q[i].x1; j++)vis[j]++;}}for(int i=1; i<=n*m; i++){if(vis[i]==0)union_(i);}Q[q].sum=ans;for(int i=q; i>=2; i--){if(Q[i].x==Q[i].x1){for(int j=(Q[i].y-1)*n+Q[i].x; j<=(Q[i].y1-1)*n+Q[i].x1; j+=n)vis[j]--;for(int j=(Q[i].y-1)*n+Q[i].x; j<=(Q[i].y1-1)*n+Q[i].x1; j+=n){if(vis[j]==0)union_(j);}}else if(Q[i].y==Q[i].y1){for(int j=(Q[i].y-1)*n+Q[i].x; j<=(Q[i].y1-1)*n+Q[i].x1; j++)vis[j]--;for(int j=(Q[i].y-1)*n+Q[i].x; j<=(Q[i].y1-1)*n+Q[i].x1; j++){if(vis[j]==0)union_(j);}}cnt++;Q[q-cnt].sum=ans;}for(int i=q-cnt; i<=q; i++)printf("%d\n",Q[i].sum);}return 0;
}

CSU2020:Artwork(并查集)相关推荐

  1. gym:Problem A Artwork(并查集思维题)

    20162017-acmicpc-nordic-collegiate-programming-contest-ncpc-2016 Problem A Artwork 题目链接 http://codef ...

  2. ArtWork+并查集二维

    问题 A: ArtWork 时间限制: 4 Sec  内存限制: 128 MB 提交: 18  解决: 6 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 A template for ...

  3. A - Artwork ( 并查集 )

    A - Artwork (  并查集  ) 题意:在一个n*m的房间里,有k个摄像头,每个摄像头给出坐标(x,y)和范围半径r , 小偷在左下角, 宝藏在右上角,问小偷能否成功偷到宝藏. 思路:可以把 ...

  4. ArtWork(并查集+降维)

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

  5. 并查集c++代码_[Leetcode 每日精选](本周主题-并查集) 547. 朋友圈

    题目难度: 中等 原题链接 今天继续来做并查集的问题, 这道题仍然比较基础, 而且也是个比较接近现实的问题了. 大家在我的公众号"每日精选算法题"中的聊天框中回复 并查集 就能看到 ...

  6. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  7. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法

    tanjar算法离线求LCA的思想主要是利用并查集的思想. 求距离的话就是d[start[i]]+end[en[i]]-2*d[lca[i]]; 首先从根节点dfs,在深度遍历的回溯的过程中不断的更新 ...

  8. POJ - 2513 Colored Sticks 欧拉通路+并查集+静态树

    一开始想用map来搞,但是感觉好复杂,然后想了一下看大佬们用trie做的,感觉十分合理就敲了一发. 一开始re,数组要开到550000 只会静态的字典树,在每个根节点看是否出现过改颜色,如果没有就把该 ...

  9. 关于 并查集(union find) 算法基本原理 以及 其 在分布式图场景的应用

    二月的最后一篇水文-想写一些有意思的东西. 文章目录 环检测在图数据结构中的应用 深度/广度优先 检测环 并查集数据结构 (Union-Find) 基本概念 初始化 合并 union 查找祖先 优化1 ...

最新文章

  1. 多模态人物识别技术及其在视频场景中的应用 | CSDN技术公开课
  2. 使用Git将我的最后一个X提交一起压缩
  3. leetcode 91. 解码方法(dp)
  4. s7-1200跟mysql_让西门子S7-1200直接连接MySQL数据库!!!
  5. 低欲望社会有多可怕?仅94万!日本去年新生人口数创历史新低,空房子如瘟疫般蔓延...
  6. 分布式链路追踪技术对比
  7. 计算机不属于发明保护客体,如何判断两种类型的计算机程序发明能否成为专利保护客体?...
  8. apk(安卓手机应用软件)解包汉化过程简单陈述 [转贴]
  9. 如何将微商管理模式流程化
  10. ERP实施--常见问题
  11. Android开发——ListView局部刷新的实现
  12. TPC TPCC TPMC 计算机性能衡量指标
  13. 三. 英语语法 - 名词和名词性从句
  14. 在Microsoft Office、Visio、WPS中用LaTeX的方式编辑公式
  15. 解决或者设置网页变灰-CSS-filter属性-哀悼
  16. 利用Linux的crontab实现定时执行python任务
  17. 五个最好的压缩软件下载
  18. Flutter ListView (动态)列表组件、水平列表组件、图标组件详解
  19. Nanoprobes Alexa Fluor 488 FluoroNanogold 偶联物
  20. Conda创建虚拟环境以及包

热门文章

  1. linux脚本乘法运算符,shell算术运算
  2. 利用谷歌镜像网站编辑Latex的参考文献与doi链接
  3. 硬件电路设计之如何设计一个STM32最小系统?
  4. 【阿里聚安全·移动安全周刊】移动裸奔时代,手机已成为隐私的监视器
  5. python批量下载邮件附件
  6. HDU6578 2019HDU多校训练赛第一场 1001 (dp)
  7. 【蓝桥杯选拔赛真题09】Scratch小猫旅行 少儿编程scratch蓝桥杯选拔赛真题讲解
  8. Ubuntu使用WPS打开文档出现缺失字体情况解决方法
  9. Structured Program I – Print a Frame
  10. python报错:SyntaxError: Missing parentheses in call to ‘exec‘