题目描述

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
题意是给你一个n*m的矩阵,q次询问,每次将连续的一些竖直或水平的格子染黑,问每一步操作之后白色联通块的个数

从最后一种局面往前走,先求出所有操作之后白色联通块的数量,然后逐条删去黑线,对新出现的白格子,要么和原有的某个联通块相连,要么属于单独的联通块
用并查集维护联通块
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+;
const int dx[]={,-,,};
const int dy[]={,,,-};
int n,m,q,cnt;
struct line{
int x1,x2,y1,y2;}li[N*];
int f[N*N],num[N][N],ans[N*];
int Hash(int x,int y)
{
return (x-)*m+y;
}
int fund(int x)
{
if (f[x]==x) return f[x];
return f[x]=fund(f[x]);
}
void join(int x,int y)
{
int fx=fund(x),fy=fund(y);
if (fx!=fy)
{
cnt--;
f[fx]=fy;
}
}
void dfs(int x,int y)
{
int id=Hash(x,y);
for (int i=;i<;i++)
{
int fx=x+dx[i],fy=y+dy[i];
if (fx<||fx>n||fy<||fy>m) continue;
if (num[fx][fy]==)
{
join(id,Hash(fx,fy));
}
}
}
void print(line l)
{
for (int i=l.x1;i<=l.x2;i++)
for (int j=l.y1;j<=l.y2;j++)
{
if (num[i][j]==) cnt--;
num[i][j]++;
}
}
void reprint(line l)
{
for (int i=l.x1;i<=l.x2;i++)
for (int j=l.y1;j<=l.y2;j++)
{
num[i][j]--;
if (num[i][j]==)
{
cnt++;
dfs(i,j);
}
}
} int main()
{
scanf("%d%d%d",&n,&m,&q);
cnt=n*m;
for (int i=;i<=cnt;i++) f[i]=i;
for (int i=;i<=q;i++)
{
scanf("%d%d%d%d",&li[i].x1,&li[i].y1,&li[i].x2,&li[i].y2);
print(li[i]);
} for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (num[i][j]==) dfs(i,j); for (int i=q;i>=;i--)
{
ans[i]=cnt;
reprint(li[i]);
}
for (int i=;i<=q;i++) printf("%d\n",ans[i]);
return ;
}
 

NCPC2016-A-ArtWork的更多相关文章

  1. Artwork

    A template for an artwork is a white grid of n × m squares. The artwork will be created by painting  ...

  2. NCPC 2016 October 8,2016 Artwork

    Problem A Artwork Problem ID: artwork Time limit: 4 seconds A template for an artwork is a white gri ...

  3. Gym 102346A Artwork dfs

    Artwork Gym - 102346A 题意:给n*m的地图,入口是(0,0),出口是(n,m),其中有k个监视器,坐标是(xi,yi),监视半径是r,问一个人能不能不被监视到,从起点到终点. 如 ...

  4. Artwork (Gym - 102346A)【DFS、连通块】

    Artwork (Gym - 102346A) 题目链接 算法 DFS,连通块 时间复杂度:O(k*n + k * k) 1.这道题就是让你判断从(0,0)到(m,n),避开中途所有的传感器(传感器的 ...

  5. UVa LA 4636 Cubist Artwork 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. UVALive - 4636 Cubist Artwork(贪心)

    题目链接 题意 给出正视图和侧视图,判断最少用几个立方体 分析 若存在高度相同的立方块,则以数目多的那面为准. #include <iostream> #include <cstdi ...

  7. Gym - 101550A Artwork (并查集在线做法)

    题目链接 题意:给你一个n*m的网格图,初始时格点全白,每次可以将一段连续的格点涂黑.求出每次操作之后白色连通块的数量. 看了看网上的题解,基本全是离线的做法.其实这道题是有在线的做法的,利用了对偶图 ...

  8. UVa 1445 - Cubist Artwork

    统计正面看高度为i的竖条个数为cnt1[i], 统计侧面看高度为i的竖条个数为cnt2[i]: ans = sum( i * max( cnt1[i], cnt2[i] ) ); ( 1 <= ...

  9. Artwork 18年中南多校第一场A

    一.题意 对于一个矩阵,若干道命令,每道命令将会把某一段格子涂黑,请问每次涂黑之后矩阵中未被涂黑的块的数量? 二.思路 保存每道命令,并且忠实的执行他,到最后一步开始搜索联通块的数量,并将其保存. 之 ...

  10. Artwork Gym - 101550A 离线并查集

    题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...

随机推荐

  1. UVA - 11021 Tribles 概率dp

    题目链接: http://vjudge.net/problem/UVA-11021 Tribles Time Limit: 3000MS 题意 有k只麻球,每只活一天就会死亡,临死之前可能会出生一些新 ...

  2. windows redis 后台运行

    1. 进入 DOS窗口2. 在进入Redis的安装目录3. 输入:redis-server --service-install redis.windows.conf --loglevel verbos ...

  3. beta冲刺7/7

    队名:Boy Next Door 燃尽图 代码写入 https://github.com/mangoqiqi/paybook/tree/master/Desktop/Web%E8%B4%A6%E5%8 ...

  4. 浅学CSS

    CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 属性(property)是您希望设置的样式属性(sty ...

  5. vue 如何使用scss (转载)

    创建一个基于 webpack 模板的新项目 全局安装 vue-cli $ npm install --global vue-cli 创建一个基于 webpack 模板的新项目 $ vue init w ...

  6. Redis (二)_ jedis的使用

    Jedis 是 Redis 官方首选的 Java 客户端开发包 虚拟机设置 查看虚拟机的ip ifconfig 将虚拟机的6379端口打开 #运行下面的命令 如果是新建的一个新的 文件,你需要先安装 ...

  7. Java:JUnit4使用详解

    对于Junit的总是一知半解不太懂,现在认真梳理一下: 此次针对的是Junit4版本,注解也是在Junit4版本才有的,之前的版本并无注解功能.而注解开发基本上被认为是一种优秀的设计,所以我们写单元测 ...

  8. 自平衡二叉(查找树/搜索树/排序树) binary search tree

    在计算机科学中,AVL树是最先发明的自平衡二叉查找树.AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis,他们在 1962 年的论文 "An alg ...

  9. MT【133】磨刀不误砍柴工

    (2018,4月学考数学填空最后一题) \(f(x)=2x^2-(x-a)|x-a|-2 \ge 0\)对任意\(x\in R\)恒成立,求\(a\)的范围______ 提示:\(f(0)=a|a|- ...

  10. 【转】ubuntu16.04安装ncurses-devel

    在ubuntu16.04中编译内核时,使用make menuconfig发生错误,说没有安装ncurses-devel. 使用apt install ncurses-devel命令安装该库,没有,然后 ...