问题 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;
}

ArtWork+并查集二维相关推荐

  1. 【CCCC】L2-010 排座位 (25分),,并查集+二维矩阵判定关系

    problem L2-010 排座位 (25分) 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位.无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编 ...

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

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

  3. 【贪心】 POJ 1456 Supermarket 详解 (并查集/二叉堆优化)

    题目链接:POJ-1456 题目大意 有n件商品,每件商品的利润为,销售日期的截止时间为(即只能在时间前销售该物品).一天只能销售一件物品.问这n件商品的最大利润为多少 方案一 分析 有两种贪心策略, ...

  4. php关联二维数组,php-检查关联二维数组

    我有以下数组声明 Array ( [name] => 1 [callrate] => 1 [maxcalls] => 100000 [mintime] => 5 [maxtim ...

  5. 怎样通过易查分制作二维码查分系统

    在工作学习中,我们经常会遇到二维码查分系统这样的问题.卡耐基说过:对别人的意见要表示尊重.千万别说:"你错了."因此,面对二维码查分系统我们应该有努力探索的精神.不要害怕学习,知识没有重量,它是你随时 ...

  6. python zxing 识别条码_Python zxing 库解析(条形码二维码识别)

    各种扫码软件 最近要做个二维码识别的项目,查到二维码识别有好多开源的不开源的软件 Zbar 首先试了一下Zbar,python加载ZBar时各种报错.可能的原因是zbar的dll文件是32位的,而我系 ...

  7. python发票二维码条码识别_Python zxing 库解析(条形码二维码识别)

    各种扫码软件 最近要做个二维码识别的项目,查到二维码识别有好多开源的不开源的软件 http://www.oschina.net/project/tag/238/ Zbar 首先试了一下Zbar,pyt ...

  8. GIS从二维到三维有多远

    摘要 地理信息系统(GIS)起源于北美,是现代地理学与空间信息科学相结合的产物.经过数十年的发展,GIS已经从只有少数专业人士才懂的应用系统,成为日益走向大众的专业软件. 三维GIS是GIS的重要发展 ...

  9. 【转载】 突破技术瓶颈 GIS从二维到三维有多远

    在3sNews上看到了一篇文章,关于三维GIS的,写的不错,转载在这里. 突破技术瓶颈 GIS从二维到三维有多远? http://www.3snews.net/html/06/n-21706.html ...

最新文章

  1. js获取url中的参数值
  2. 快速修改HTML5,HTML5无刷新修改URL(示例代码)
  3. css3 position: fixed 居中问题,移动端,旁边留白的情况fixed不能居中的问题;
  4. 敏捷项目管理架构(APMF)
  5. uBLAS——Boost 线性代数基础程序库 (三)
  6. Java新职篇:编译程序(2)
  7. MySQL(my.ini)配置文件详解
  8. windows10 配置深度学习环境
  9. kendo ui开源工具:Kendo ui core
  10. 谷歌浏览器关闭阅读清单
  11. 基于OpenCV的土壤裂缝分割系统
  12. php邮箱发送,PHP的邮箱发送
  13. 视频教程-微信公众号二维码签到和抽奖软件-微信开发
  14. 【高并发】解密导致并发问题的第二个幕后黑手——原子性问题(文末有福利)
  15. 【软件定义汽车】【架构篇】最全整车电子电气E/E架构(含汽车公司)
  16. 虚拟机一直光标闪,进不去,解决方法之一。
  17. mysql 查询dual报错_MYSQL基础02(查询)
  18. AIO-3399J重新安装Ubuntu系统-刷新固件
  19. 姓张信息mysql_MySQL_数据查询
  20. 存储知识 -- 什么是LUN?LUN有什么用?对理解存储设备很有好处

热门文章

  1. 《第四堂棒球课》:MLB棒球创造营·棒球名人堂
  2. 【笔记】《Federated Learning With Blockchain for Autonomous Vehicles Analysis and Design Challenges》精读笔记
  3. 简单总结协程Coroutine及Yield常见用法
  4. Win10系统打不开图片
  5. 使用Navicat 设定 sql 触发器
  6. MYSQL的函数与Navicat的使用
  7. Excel 2010 VBA 入门 033 批量合并相同的单元格
  8. R语言logistic回归的细节解读
  9. 奥维互动地图加载地球引擎Google Earth Engine(GEE)1984-2022 地球卫星资源 亲测有效,路亚 钓鱼 露营 水下结构 水文 温湿度 地貌变化 大数据AI分析 神器
  10. 巨高兴,自己的“万能数据库查询分析器”中英文 3.01版本 已经在国内6大软件下载网站发布