您的位置:首页 > 其它

Codeforces 527C Glass Carving【思维+模拟】

2016-12-05 18:25 477 查看
A. 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
w mm  × 
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.

Examples

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 x,表示对高度为x(从下到上)的位子横向的切一刀。

②V x,表示对宽度为x(从左到右)的位子纵向的切一刀。

问每次操作结束之后,当前情况切割出来的最大部分的玻璃面积。

思路:

1、首先我们可以设定两个数组:

row【i】,col【i】,分别表示第i行(高度为i)和第i列(宽度为i)的位子是否有切割过,对应0表示没有,1表示有。

那么我们将样例2的切割完之后的情况写出来:

    0 1 2 3 4 5 6 7

row【i】:1 0 1 0 1 0 1 0

col【i】: 1 1 0 1 0 1 0 1

那么此时我们如果想要求最后这样的情况的最大部分面积,其实就是需要我们维护row【i】中相邻两个1的最远距离记做maxnrow,以及col【i】中相邻两个1的最远距离,记做maxncol,相乘即为答案。

2、那么我们考虑倒序将每一个操作删除掉,但是如果我们每次操作都O(n)的去维护两个数组中相邻两个1的最远距离,那么很显然O(n^2)是会超时的。

那么我们考虑进行优化:

①设定一个prerow【i】,表示高度为i的地方如果有切割操作的话.其前边与其距离最近的1的位子。

②设定一个nexrow【i】,表示高度为i的地方如果有切割操作的话.其后边与其距离最近的1的位子。

同理,对应也设定两个数组precol【】,nexcol【】;

那么我们每一次倒序删除一个操作的同时,(以H x为例),那么维护:maxnrow=max(maxnrow,nexrow【x】-prerow【x】);同时将prerow【nexrow【x】】更新为prerow【x】,同理,将nexrow【prerow【x】】更新为nexrow【x】;

都是常数级的操作,可以动态的维护所有数组以及我们需要的值,这样就可以在时间复杂度内搞定这个问题辣~。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
#define ll __int64
struct node
{
char op[4];
int pos;
}a[200070];
int prerow[200070];
int nexrow[200700];
int precol[200700];
int nexcol[200700];
int row[200070];
int col[200070];
ll ans[200070];
int main()
{
int l,h,q;
while(~scanf("%d%d%d",&l,&h,&q))
{
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
for(int i=0;i<q;i++)
{
scanf("%S%d",a[i].op,&a[i].pos);
if(a[i].op[0]=='H')
{
row[a[i].pos]=1;
}
else col[a[i].pos]=1;
}
prerow[0]=0;precol[0]=0;
row[0]=1;row[h]=1;
col[0]=1;col[l]=1;
ll maxnc=0;
ll maxnr=0;
int prec=0;
int prer=0;
for(int i=1;i<=200020;i++)
{
if(row[i]==1)
{
prerow[i]=prer;
nexrow[prer]=i;
maxnr=max(maxnr,(ll)(i-prer));
prer=i;
}
}
for(int i=1;i<=200020;i++)
{
if(col[i]==1)
{
precol[i]=prec;
nexcol[prec]=i;
maxnc=max(maxnc,(ll)(i-prec));
prec=i;
}
}
int cont=0;
ans[cont++]=maxnc*maxnr;
for(int i=q-1;i>=1;i--)
{
if(a[i].op[0]=='H')
{
row[a[i].pos]=0;
maxnr=max(maxnr,(ll)(nexrow[a[i].pos]-prerow[a[i].pos]));
prerow[nexrow[a[i].pos]]=prerow[a[i].pos];
nexrow[prerow[a[i].pos]]=nexrow[a[i].pos];
}
else
{
col[a[i].pos]=0;
maxnc=max(maxnc,(ll)(nexcol[a[i].pos]-precol[a[i].pos]));
precol[nexcol[a[i].pos]]=precol[a[i].pos];
nexcol[precol[a[i].pos]]=nexcol[a[i].pos];
}
ans[cont++]=maxnc*maxnr;
}
for(int i=cont-1;i>=0;i--)
{
printf("%I64d\n",ans[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 527C