Problem A
Artwork
Problem ID: artwork
Time limit: 4 seconds
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.


                                1 region     3 regions      3 regions     4 regions     3 regions
Figure A.1: Illustration of 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 integers x1, y1, x2 and y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m). Either x1 = x2 or y1 = y2 (or both).

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 integers x1, 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 1                                                                             Sample Output 1

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

这题输出方式让我想死。。。之前训练比赛时认为是输入一行就输出一行。。结果一直output limit exceeded。。后面看了别人的才发现是输完后再一起输出。。想死的心都有了。
思路:从最后一张图开始往前推,再用并查集解决。详细看代码
#include<stdio.h>
#include<string.h>
#define MAXN 1010
struct stock{
int x1, x2, y1, y2;
}pos[10010]; int num[MAXN*MAXN], fa[MAXN*MAXN],ans[10010];//num表示当前位置涂了几个黑点 ans[n]表示第n次画横线时白色连通块的个数
int dir[4][2] = { 0, 1, -1, 0, 0, -1, 1, 0 };
int m, n, p,t;
int hash(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--;//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[hash(xx,yy)])
{
merge(hash(xx,yy), hash(x,y));
}
}
} int main()
{
scanf("%d %d %d", &n, &m, &p);
t = m*n;
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[hash(x, y)] == 0)
t--;
num[hash(x, y)]++;
}
}
}
//求出最后一个图的白色连通块的个数
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (!num[hash(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[hash(x, y)]--;
if (num[hash(x, y)] == 0)
{
t++;//黑块撤完白块数目增加
work(x, y);
}
}
}
}
for (int i = 1; i <= p; i++)
{
printf("%d\n", ans[i]);
}
return 0;
}

在比赛时从正面进行时是通过黑块入手的,发现时间没有超,但一直output limit exceeded


NCPC 2016 October 8,2016 Artwork的更多相关文章

  1. October 14th 2016 Week 42nd Friday

    Who am I? Coming October 18, 2016! 我是谁?2016.10.18 拭目以待! Don't worry. You will be a wow. Don't worry. ...

  2. 2016.1.4~2016.1.7真题回顾!-- HTML5学堂

    2016.1.4~2016.1.7真题回顾!-- HTML5学堂 2015悄然而逝,崭新的2016随即而行!生活需要新鲜感,学习JavaScript的过程需要有成就感!成就感又是来自于每一天的不断练习 ...

  3. Windows Server 2008 R2+SQL Server 2014 R2升级到Windows Server 2016+SQL Server 2016

    环境: 操作系统:Windows Server 2008 R2 数据库:SQL Server 2014 因SQL Server 2016可以无域创建AlwaysOn集群,集群只剩下单节点也不会挂掉,故 ...

  4. Windows 2016 安装Sharepoint 2016 预装组件失败

    Windows 2016 安装Sharepoint 2016 预装组件失败 日志如下: -- :: - Request for install time of Web 服务器(IIS)角色 -- :: ...

  5. 成功安装 Visio 2016 和 Office 2016 的64位版本~~

    .XML是个很  的东西!!! 折腾了一下 Visio 2016_x64 和 Office 2016_x64,功夫不负! 首先,选对配置工具很重要. 之前总是失败是因为在官网下载的配置工具是给2019 ...

  6. October 21st 2016 Week 43rd Friday

    Life is too short for long-term grudges. 人生苦短,无暇怨恨. Don't limit yourself. You can go as far as your ...

  7. October 17th 2016 Week 43rd Monday

    You only live once, but if you do it right, once is enough. 人生只有一次,但如果活对了,一次也就够了. Whether you do it ...

  8. October 15th 2016 Week 42nd Saturday

    Word to World. There are only two kinds of people who are really fascinating, people who know absolu ...

  9. October 13th 2016 Week 42nd Thursday

    If the world seems cold to you, kindle fires to warm it. 若世界以寒相待,请点燃火堆以温暖相报. Kindle fires to warm th ...

随机推荐

  1. 《编写高质量代码:改善JavaScript程序的188个建议》学习小记(二)

    建议3:减少全局变量污染 1.把多个全局变量都追加在一个名称空间下,将显著降低与其他应用程序产生冲突的概率,应用程序也会变得更容易阅读. var My = {}; My.name = { " ...

  2. Redis实战(七)Redis开发与运维

    Redis用途 1.缓存 Redis提供了键值过期时间设置, 并且也提供了灵活控制最大内存和内存溢出后的淘汰策略. 可以这么说, 一个合理的缓存设计能够为一个网站的稳定保驾护航. 2.排行榜系统 Re ...

  3. 网页制作中最有用的免费Ajax和JavaScript代码库

    网上看到的一篇小文,挺有用的,收藏在这. 本文中,我整理了12个免费的Ajax和JavaScript代码库,可以帮助Web开发人员将应用程序提升到一个新水平. Ajax Instant Messeng ...

  4. R5—字符串处理/正则表达式

    R通常被用来进行数值计算比较多,字符串处理相对较少,而且关于字符串的函数也不多,用得多的就是substr.strsplit.paste.regexpr这几个了.实际上R关于字符串处理的功能是非常强大的 ...

  5. Codeforces Round #540 (Div. 3)题解

    题目链接: https://codeforces.com/contest/1118 A题: 题意: q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值. 思路: 我 ...

  6. 简明Python教程 ~ 随书笔记

    本文是阅读<简明Python教程>所做的随书笔记,主要是记录一些自己不熟悉的用法,或者所看到的比较有意思的内容,本书英文版A Byte of Python, 中文译版 简明Python教程 ...

  7. HDU 1556 Color the ball (树状数组 区间更新+单点查询)

    题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...

  8. Perl6 必应抓取(2):最终版

    use HTTP::UserAgent; use URI::Encode; Firefox/52.0>); my $bing_url = 'http://cn.bing.com/search?q ...

  9. Linux触摸屏驱动测试程序范例【转】

    转自:http://blog.sina.com.cn/s/blog_4b4b54da0102viyl.html 转载2015-05-09 16:28:27 标签:androiditlinux 触摸屏驱 ...

  10. linux下的usb抓包方法【转】

    转自:http://blog.chinaunix.net/uid-11848011-id-4508834.html 1.配置内核使能usb monitor: make menuconfig       ...