C. Glass Carving
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm  ×  h mm
sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.

In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made
glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.

After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.

Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?

Input

The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000).

Next n lines contain the descriptions of the cuts. Each description has the form H y or V x.
In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1)
from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1)
millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.

Output

After each cut print on a single line the area of the maximum available glass fragment in mm2.

Sample test(s)
input
  1. 4 3 4
  2. H 2
  3. V 2
  4. V 3
  5. V 1
output
  1. 8
  2. 4
  3. 4
  4. 2
input
  1. 7 6 5
  2. H 4
  3. V 3
  4. V 5
  5. H 2
  6. V 1
output
  1. 28
  2. 16
  3. 12
  4. 6
  5. 4
Note

Picture for the first sample test:


Picture for the second sample test:

题意:一个w*h的玻璃,如今水平或竖直切n次(“H”表示水平切,“V”表示竖直切),每一次切后输出当前切成的块中的最大面积。

思路:用set记录分割的位置(要用两个set,分别来记录长和宽),multiset记录某一条边被切后 所得到的 小段的长度(也要两个,分别记录长和宽的)。

那么每次切后就从multiset中取出最大的长和宽,相乘即得面积。

STL set 写法

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #define FRE(i,a,b) for(i = a; i <= b; i++)
  13. #define FRL(i,a,b) for(i = a; i < b; i++)
  14. #define mem(t, v) memset ((t) , v, sizeof(t))
  15. #define sf(n) scanf("%d", &n)
  16. #define sff(a,b) scanf("%d %d", &a, &b)
  17. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  18. #define pf printf
  19. #define DBG pf("Hi\n")
  20. typedef __int64 ll;
  21. using namespace std;
  22.  
  23. int w,h,n;
  24. set<int>wxs;
  25. set<int>hxs;
  26. multiset<int>wds;
  27. multiset<int>hds;
  28.  
  29. int main()
  30. {
  31. int i,j;
  32. while (~sfff(w,h,n))
  33. {
  34. set<int>::iterator it,p;
  35. char s[3];
  36. int x;
  37. wxs.clear();
  38. hxs.clear();
  39. wds.clear();
  40. hds.clear();
  41. wxs.insert(0); wxs.insert(w);
  42. hxs.insert(0); hxs.insert(h);
  43. wds.insert(w); hds.insert(h);
  44. while (n--)
  45. {
  46. scanf("%s%d",s,&x);
  47. if (s[0]=='H')
  48. {
  49. it=hxs.lower_bound(x);
  50. p=it;
  51. p--;
  52. int dis = *it - *p;
  53. hds.erase(hds.find(dis));//这里不能写成hds.erase(dis),在multiset里面这样写会把全部值等于dis的点删掉,这显然不符合我们的题意
  54. hds.insert(*it-x);
  55. hds.insert(x-*p);
  56. hxs.insert(x);
  57. }
  58. else
  59. {
  60. it=wxs.lower_bound(x);
  61. p=it;
  62. p--;
  63. int dis = *it - *p;
  64. wds.erase(wds.find(dis));
  65. wds.insert(*it-x);
  66. wds.insert(x-*p);
  67. wxs.insert(x);
  68. }
  69. int xx= *wds.rbegin();
  70. int yy= *hds.rbegin();
  71. pf("%I64d\n",(ll)xx * (ll)yy); //最后要强制转化,不然会爆int
  72. }
  73. }
  74. return 0;
  75. }

并查集写法

思路:并查集初始化。首先将玻璃所有切成1*1的小块,然后先保存下所有的操作,记录下它切了哪些位置(数组vis_w和vis_h),接着将没有被切的位置 i 连起来Union(i,i+1)。最后倒着把要切的位置连起来,这个过程中记录每次两条边的最大值,它们的乘积保存下来就是答案。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #define maxn 200010
  13. #define FRE(i,a,b) for(i = a; i <= b; i++)
  14. #define FRL(i,a,b) for(i = a; i < b; i++)
  15. #define mem(t, v) memset ((t) , v, sizeof(t))
  16. #define sf(n) scanf("%d", &n)
  17. #define sff(a,b) scanf("%d %d", &a, &b)
  18. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  19. #define pf printf
  20. #define DBG pf("Hi\n")
  21. typedef __int64 ll;
  22. using namespace std;
  23.  
  24. //维护两个并查集,分别维护w和h
  25. int w,h,n,max_w,max_h;
  26. int father_w[maxn],father_h[maxn];
  27. int num_w[maxn],num_h[maxn];
  28. bool vis_w[maxn],vis_h[maxn];
  29. char s[maxn][3];
  30. int pos[maxn];
  31. ll ans[maxn];
  32.  
  33. void init()
  34. {
  35. int i;
  36. FRL(i,0,maxn)
  37. {
  38. father_w[i]=i;
  39. father_h[i]=i;
  40. num_w[i]=1;
  41. num_h[i]=1;
  42. }
  43. num_w[0]=0;
  44. num_h[0]=0;
  45. mem(vis_w,false);
  46. mem(vis_h,false);
  47. max_w=1;//用来记录每次Union操作后边的最大值
  48. max_h=1;
  49. }
  50.  
  51. int find_father_w(int x)
  52. {
  53. if (x!=father_w[x])
  54. father_w[x]=find_father_w(father_w[x]);
  55. return father_w[x];
  56. }
  57.  
  58. int find_father_h(int x)
  59. {
  60. if (x!=father_h[x])
  61. father_h[x]=find_father_h(father_h[x]);
  62. return father_h[x];
  63. }
  64.  
  65. void Union_w(int a,int b)
  66. {
  67. int fa=find_father_w(a);
  68. int fb=find_father_w(b);
  69. if (fa!=fb)
  70. {
  71. father_w[fb]=fa;
  72. num_w[fa]+=num_w[fb];
  73. }
  74. max_w=max(max_w,num_w[fa]);
  75. }
  76.  
  77. void Union_h(int a,int b)
  78. {
  79. int fa=find_father_h(a);
  80. int fb=find_father_h(b);
  81. if (fa!=fb)
  82. {
  83. father_h[fb]=fa;
  84. num_h[fa]+=num_h[fb];
  85. }
  86. max_h=max(max_h,num_h[fa]);
  87. }
  88.  
  89. int main()
  90. {
  91. int i,j;
  92. while (~sfff(w,h,n))
  93. {
  94. init();
  95. FRE(i,1,n)
  96. {
  97. scanf("%s%d",s[i],&pos[i]);
  98. if (s[i][0]=='H') vis_h[pos[i]]=true;
  99. else vis_w[pos[i]]=true;
  100. }
  101. FRL(i,1,w)//先把没有被切的位置连起来
  102. {
  103. if (!vis_w[i])
  104. Union_w(i,i+1);
  105. }
  106. FRL(i,1,h)
  107. {
  108. if (!vis_h[i])
  109. Union_h(i,i+1);
  110. }
  111. for (i=n;i>0;i--)//逆操作连接
  112. {
  113. // pf("max_w=%d\nmax_h=%d\n***\n",max_w,max_h);
  114. ans[i]=(ll)max_w * (ll)max_h;//保存答案
  115. if (s[i][0]=='H') Union_h(pos[i],pos[i]+1);
  116. else Union_w(pos[i],pos[i]+1);
  117. }
  118. FRE(i,1,n)
  119. pf("%I64d\n",ans[i]);
  120. }
  121. return 0;
  122. }

C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 &amp;&amp; 并查集方法)的更多相关文章

  1. B. Error Correct System (CF Round #296 (Div. 2))

    B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

  3. CF Round #551 (Div. 2) D

    CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...

  4. CF Round #510 (Div. 2)

    前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...

  5. Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路

    Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xx ...

  6. 竞赛题解 - CF Round #524 Div.2

    CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...

  7. CF Round #600 (Div 2) 解题报告(A~E)

    CF Round #600 (Div 2) 解题报告(A~E) A:Single Push 采用差分的思想,让\(b-a=c\),然后观察\(c\)序列是不是一个满足要求的序列 #include< ...

  8. Codeforces Round #296 (Div. 1) A. Glass Carving Set的妙用

    A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #296 (Div. 2) C. Glass Carving [ set+multiset ]

    传送门 C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. centos 安装squid http代理

    yum -y install squid service squid start service iptables stop

  2. linux反汇编

    使用objdump参数可以: -a, --archive-headers    显示压缩头信息   -f, --file-headers       显示目录头总览   -p, --private-h ...

  3. JQuery实现多个菜单的显示隐藏

    (如有错敬请指点,以下是我工作中遇到并且解决的问题) 效果图: 点击各个菜单显示/隐藏,以及点击灰色部分隐藏. 比如点击了第一个菜单,然后点击第二个菜单,第一个菜单会隐藏,再显示第二个菜单,不会叠加. ...

  4. mogilefsd同步速度调优

    #查看主从mogadm settings list #一点点调试mogadm settings listmogadm settings set internal_queue_limit 500moga ...

  5. wsgi的学习(2):uWSGI的概念

    uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / u ...

  6. 我为什么喜欢Go语言

    从2000年至今,也写了11年代码了,期间用过VB.Delphi.C#.C++.Ruby.Python,一直在寻找一门符合自己心意和理念的 语言.我很在意写代码时的手感和执行的效率,所以在Go出现之前 ...

  7. (1)php开篇常识

    一.php PHP由zend公司开发维护,目前最高版本PHP7.1,官网地址http://php.net/ PHP从5.5开始不支持XP,没有PHP6这个版本 PHP是嵌入到HTML的脚本代码,格式: ...

  8. Codeforces 246E - Blood Cousins Return (树上启发式合并)

    246E - Blood Cousins Return 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor,每个节点有名字,名字不唯一.多次查询,给出 u k ...

  9. jsp笔记2(编译指令与动作指令)

    一.jsp的编译指令是通知jsp引擎的消息,不会生成输出. jsp的3个编译指令: page:针对当前页面的指令   include:包含另一个页面的指令   taglib:用于定义和访问自定义标签 ...

  10. 【java初学者】理解,从面向过程 到 面向对象,面向接口,面向切面

    http://blog.csdn.net/ssh159/article/details/52516986