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
4 3 4
H 2
V 2
V 3
V 1
output
8
4
4
2
input
7 6 5
H 4
V 3
V 5
H 2
V 1
output
28
16
12
6
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 写法

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef __int64 ll;
using namespace std; int w,h,n;
set<int>wxs;
set<int>hxs;
multiset<int>wds;
multiset<int>hds; int main()
{
int i,j;
while (~sfff(w,h,n))
{
set<int>::iterator it,p;
char s[3];
int x;
wxs.clear();
hxs.clear();
wds.clear();
hds.clear();
wxs.insert(0); wxs.insert(w);
hxs.insert(0); hxs.insert(h);
wds.insert(w); hds.insert(h);
while (n--)
{
scanf("%s%d",s,&x);
if (s[0]=='H')
{
it=hxs.lower_bound(x);
p=it;
p--;
int dis = *it - *p;
hds.erase(hds.find(dis));//这里不能写成hds.erase(dis),在multiset里面这样写会把全部值等于dis的点删掉,这显然不符合我们的题意
hds.insert(*it-x);
hds.insert(x-*p);
hxs.insert(x);
}
else
{
it=wxs.lower_bound(x);
p=it;
p--;
int dis = *it - *p;
wds.erase(wds.find(dis));
wds.insert(*it-x);
wds.insert(x-*p);
wxs.insert(x);
}
int xx= *wds.rbegin();
int yy= *hds.rbegin();
pf("%I64d\n",(ll)xx * (ll)yy); //最后要强制转化,不然会爆int
}
}
return 0;
}

并查集写法

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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 200010
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef __int64 ll;
using namespace std; //维护两个并查集,分别维护w和h
int w,h,n,max_w,max_h;
int father_w[maxn],father_h[maxn];
int num_w[maxn],num_h[maxn];
bool vis_w[maxn],vis_h[maxn];
char s[maxn][3];
int pos[maxn];
ll ans[maxn]; void init()
{
int i;
FRL(i,0,maxn)
{
father_w[i]=i;
father_h[i]=i;
num_w[i]=1;
num_h[i]=1;
}
num_w[0]=0;
num_h[0]=0;
mem(vis_w,false);
mem(vis_h,false);
max_w=1;//用来记录每次Union操作后边的最大值
max_h=1;
} int find_father_w(int x)
{
if (x!=father_w[x])
father_w[x]=find_father_w(father_w[x]);
return father_w[x];
} int find_father_h(int x)
{
if (x!=father_h[x])
father_h[x]=find_father_h(father_h[x]);
return father_h[x];
} void Union_w(int a,int b)
{
int fa=find_father_w(a);
int fb=find_father_w(b);
if (fa!=fb)
{
father_w[fb]=fa;
num_w[fa]+=num_w[fb];
}
max_w=max(max_w,num_w[fa]);
} void Union_h(int a,int b)
{
int fa=find_father_h(a);
int fb=find_father_h(b);
if (fa!=fb)
{
father_h[fb]=fa;
num_h[fa]+=num_h[fb];
}
max_h=max(max_h,num_h[fa]);
} int main()
{
int i,j;
while (~sfff(w,h,n))
{
init();
FRE(i,1,n)
{
scanf("%s%d",s[i],&pos[i]);
if (s[i][0]=='H') vis_h[pos[i]]=true;
else vis_w[pos[i]]=true;
}
FRL(i,1,w)//先把没有被切的位置连起来
{
if (!vis_w[i])
Union_w(i,i+1);
}
FRL(i,1,h)
{
if (!vis_h[i])
Union_h(i,i+1);
}
for (i=n;i>0;i--)//逆操作连接
{
// pf("max_w=%d\nmax_h=%d\n***\n",max_w,max_h);
ans[i]=(ll)max_w * (ll)max_h;//保存答案
if (s[i][0]=='H') Union_h(pos[i],pos[i]+1);
else Union_w(pos[i],pos[i]+1);
}
FRE(i,1,n)
pf("%I64d\n",ans[i]);
}
return 0;
}

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. 培训补坑(day1:最短路&two-sat)

    经过12天的滚粗,终于迎来了暑期培训的结尾啦QAQ 结业考才考了90分,真是对不起孙爷(孙爷请收下我的膝盖) orz小粉兔怒D rank 1 获得小粉兔一只QAQ 由于这次12天的培训题目又比较多,算 ...

  2. python笔记-冒泡排序【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python/ 前言 面试的时候经常有面试官喜欢问如何进行冒泡排序?这个问题相信能难倒一 ...

  3. centos6.x一键15项系统优化(转自努力哥)

    #!/bin/sh ################################################ #Author:nulige # qqinfo: # Date: -- #vers ...

  4. EntityFramework之多对多关系(四)

    上篇介绍了一对多关系,下面介绍下多对多关系代码编写. 1.新建model实体,User是用户类,Role是角色类,由于是多对多关系,必须得有一个中间类,所以产生了UserRole类 public cl ...

  5. Python的程序结构[2] -> 类/Class[1] -> 基类与继承

    基类与继承 / Base Class and Inheritance Class 面向对象的特性使得 Python 中不可避免地需要使用到类和类的继承,类的继承可以使得代码很好的被重用.下面以一些代码 ...

  6. luogu P3817 小A的糖果

    题目描述 小A有N个糖果盒,第i个盒中有a[i]颗糖果. 小A每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任意两个相邻的盒子中加起来都只有x颗或以下的糖果,至少得吃掉几颗糖. 输入输出格式 输入格 ...

  7. 【codevs1907】【方格取数3】二分图最大带权独立集

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=59001242 向大(hei)佬(e)势力学(di ...

  8. xml文件生成与下载

    写在前面: 最近要做一个新的功能,点击按钮,可以根据数据生成对应的xml文件并保存.下面记录一下在做的过程的一些疑惑与问题(我就是太笨了,一些很简单的知识都不知道,不过通过这次跟蛋蛋的交流,解决了我的 ...

  9. C语言的运算符、位操作

    + - * / (加 减 乘 除) > >= < <= (大于 大于等于 小于 小于等于) == != (测试等于 测试不等于) && || ! (逻辑与 逻辑 ...

  10. SQL的连接(外连接、内连接、交叉连接和自连接)

    在查询多个表时,我们经常会用到连接查询,连接是关系型数据库的主要特点,也是它区别于其他类型的数据库管理系统的一个标志. 一.什么是连接查询 连接查询:根据两个表或者多个表的列之间的关系,来从这些表中查 ...