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。。后面看了别人的才发现是输完后再一起输出。。想死的心都有了。
思路:从最后一张图开始往前推,再用并查集解决。详细看代码
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAXN 1010
  4. struct stock{
  5. int x1, x2, y1, y2;
  6. }pos[10010];
  7.  
  8. int num[MAXN*MAXN], fa[MAXN*MAXN],ans[10010];//num表示当前位置涂了几个黑点 ans[n]表示第n次画横线时白色连通块的个数
  9. int dir[4][2] = { 0, 1, -1, 0, 0, -1, 1, 0 };
  10. int m, n, p,t;
  11. int hash(int x, int y)//用哈希表表示
  12. {
  13. int num = (x - 1)*m + y;
  14. return num;
  15. }
  16. void init()//初始化
  17. {
  18. for (int i = 1; i <= n*m; i++)
  19. {
  20. fa[i] = i;
  21. num[i] = 0;
  22. }
  23. }
  24. int find(int x)
  25. {
  26. return x == fa[x] ? x : fa[x] = find(fa[x]);
  27. }
  28. void merge(int x, int y)//将两个相邻连通块连接在一起
  29. {
  30. int fx = find(x), fy = find(y);
  31. if (fx == fy)
  32. return;
  33. t--;//t表示连通块个数
  34. fa[fx] = fy;
  35. }
  36. bool check(int x, int y)
  37. {
  38. if (x >= 1 && y >= 1 && x <= n&&y <= m)
  39. return true;
  40. return false;
  41. }
  42. void work(int x,int y)//将刚出现的白块连到连通块中
  43. {
  44. for (int i = 0; i < 4; i++)
  45. {
  46. int xx = x + dir[i][0];
  47. int yy = y + dir[i][1];
  48. if (check(xx, yy) && !num[hash(xx,yy)])
  49. {
  50. merge(hash(xx,yy), hash(x,y));
  51. }
  52. }
  53. }
  54.  
  55. int main()
  56. {
  57. scanf("%d %d %d", &n, &m, &p);
  58. t = m*n;
  59. init();
  60. //画黑线
  61. for (int i = 1; i <= p;i++)
  62. {
  63. scanf("%d%d%d%d", &pos[i].x1, &pos[i].y1, &pos[i].x2, &pos[i].y2);
  64. for (int x = pos[i].x1; x <= pos[i].x2; x++)
  65. {
  66. for (int y = pos[i].y1; y <= pos[i].y2; y++)
  67. {
  68. if (num[hash(x, y)] == 0)
  69. t--;
  70. num[hash(x, y)]++;
  71. }
  72. }
  73. }
  74. //求出最后一个图的白色连通块的个数
  75. for (int i = 1; i <= n; i++)
  76. {
  77. for (int j = 1; j <= m; j++)
  78. {
  79. if (!num[hash(i,j)])
  80. {
  81. work(i, j);
  82. }
  83. }
  84. }
  85. //向前面的图推
  86. for (int i = p; i > 0; i--)
  87. {
  88. ans[i] = t;
  89. //一步一步撤去黑线
  90. for (int x = pos[i].x1; x <= pos[i].x2; x++)
  91. {
  92. for (int y = pos[i].y1; y <= pos[i].y2; y++)
  93. {
  94. num[hash(x, y)]--;
  95. if (num[hash(x, y)] == 0)
  96. {
  97. t++;//黑块撤完白块数目增加
  98. work(x, y);
  99. }
  100. }
  101. }
  102. }
  103. for (int i = 1; i <= p; i++)
  104. {
  105. printf("%d\n", ans[i]);
  106. }
  107. return 0;
  108. }

在比赛时从正面进行时是通过黑块入手的,发现时间没有超,但一直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. List(JDK1.7)(2)

    LinkedList List接口和Deque接口的一种双向链表实现.非同步的. 快速失败机制.ConcurrentModificationException 结点结构 插入结点 删除结点 add() ...

  2. 100baseT、100baseFX、1000base-SX、100/1000base-T

    100baseT.100baseFX.1000base-SX.100/1000base-T 100baseT.100baseFX都是100Mbps速率基带传输系统,唯一的不同是100baseT用的是双 ...

  3. 20155307 2016-2017-2 《Java程序设计》第5周学习总结

    20155307 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 这两章主要讲的是如何处理程序中的异常情况,对于错误,java会将其打包成对象,可以用&quo ...

  4. C++ Primer 5th 第17章 标准库特殊设施

    C++新标准库提供了很多新功能,它们更加强大和易用. tuple类型 tuple是一种类似pair的模板,pair可以用来保存一对逻辑上有关联的元素对.但与pair不同的是,pair只能存储两个成员, ...

  5. java7与java8中计算两个日期间隔多少年多少月多少天的实现方式

    最近工作中碰到个新需求,计算每个员工入职公司的时长,要求形式为多少年多少月多少天形式,某个值为0就跳过不显示,因为前段时间学习过java8新特性,对于这个需求,java8的新时间日期API可以直接解决 ...

  6. ip_local_deliver && ip_local_deliver_finish

    当ip包收上来,查路由,发现是发往本地的数据包时,会调用ip_local_deliver函数: ip_local_deliver中对ip分片进行重组,经过LOCAL_IN钩子点,然后调用ip_loca ...

  7. Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体

    一.Framebuffer 设备驱动总体架构 帧缓冲设备为标准的字符型设备,在Linux中主设备号29,定义在/include/linux/major.h中的FB_MAJOR,次设备号定义帧缓冲的个数 ...

  8. sqlite3 的insert记录项思路

    sqlite3 的insert记录项思路 1.组合一个insert的sql语句 2.判断是否需要立即执行,若不是立刻执行的语句,则插入到待处理的链表中,供后续事务处理时提交.必须有一个专门线程来对事务 ...

  9. 个性化你的Git Log的输出格式

    git已经变成了很多程序员日常工具之一. git log是查看git历史的好工具,不过默认的格式并不是特别的直观. 很多时候想要更简便的输出更多或者更少的信息,这里列出几个git log的format ...

  10. pom.xml一个简单配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...