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

Copy

4 3 4
H 2
V 2
V 3
V 1
Output

Copy

8
4
4
2
Input

Copy

7 6 5
H 4
V 3
V 5
H 2
V 1
Output

Copy

28
16
12
6
4
Note

Picture for the first sample test:

题意是给定一个矩形,不停地纵向或横向切割,问每次切割后,最大的矩形面积是多少。
最大矩形面积=最长的长*最宽的宽
这题,长宽都是10^5,所以,用01序列表示每个点是否被切割,然后,
最长的长就是长的最长连续0的数量+1
最长的宽就是宽的最长连续0的数量+1
于是用线段树维护最长连续零
 
问题转换成:
目标信息:区间最长连续零的个数
点信息:0 或 1
由于目标信息不符合区间加法,所以要扩充目标信息。
 
转换后的线段树结构
区间信息:从左,右开始的最长连续零,本区间是否全零,本区间最长连续零。
点信息:0 或 1
然后还是那2个问题:
 
1.区间加法:
这里,一个区间的最长连续零,需要考虑3部分:
-(1):左子区间最长连续零
-(2):右子区间最长连续零
-(3):左右子区间拼起来,而在中间生成的连续零(可能长于两个子区间的最长连续零)
而中间拼起来的部分长度,其实是左区间从右开始的最长连续零+右区间从左开始的最长连续零。
所以每个节点需要多两个量,来存从左右开始的最长连续零。
然而,左开始的最长连续零分两种情况,
--(1):左区间不是全零,那么等于左区间的左最长连续零
--(2):左区间全零,那么等于左区间0的个数加上右区间的左最长连续零
于是,需要知道左区间是否全零,于是再多加一个变量。
最终,通过维护4个值,达到了维护区间最长连续零的效果。
 
2.点信息->区间信息 : 
如果是0,那么  最长连续零=左最长连续零=右最长连续零=1 ,全零=true。
如果是1,那么  最长连续零=左最长连续零=右最长连续零=0, 全零=false。
 
至于修改和查询,有了区间加法之后,机械地写一下就好了。
由于这里其实只有对整个区间的查询,所以查询函数是不用写的,直接找根的统计信息就行了。

递归

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
const int maxn = * 1e5 + ; struct SegTree {
ll ls, rs, max0;
bool is_all0;
}segTree[][maxn<<]; void pushup(int root, int flag) {
SegTree &cur = segTree[flag][root], &lc = segTree[flag][root<<], &rc = segTree[flag][root<<|];
cur.ls = lc.ls + (lc.is_all0 ? rc.ls : );
cur.rs = rc.rs + (rc.is_all0 ? lc.rs : );
cur.max0 = max(lc.rs + rc.ls, max(lc.max0, rc.max0));
cur.is_all0 = lc.is_all0 && rc.is_all0;
} void build(int L, int R, int root, int flag) {
if (L == R) {
segTree[flag][root].ls = segTree[flag][root].rs = segTree[flag][root].max0 = ;
segTree[flag][root].is_all0 = true;
return;
}
int mid = (L + R)>>;
build(L, mid, root<<, flag);
build(mid + , R, root<<|, flag);
pushup(root, flag);
} void update_node(int L, int R, int root, int pos, int flag) {
if (L == R) {
segTree[flag][root].ls = segTree[flag][root].rs = segTree[flag][root].max0 = ;
segTree[flag][root].is_all0 = false;
return;
}
int mid = (L + R)>>;
if (pos <= mid) {
update_node(L, mid, root<<, pos, flag);
}
else {
update_node(mid + , R, root<<|, pos, flag);
}
pushup(root, flag);
} ll query(int L, int R, int root, int qL, int qR, int flag) {
if (qL <= L && R <= qR) {
return segTree[flag][root].max0;
}
int mid = (L + R)>>;
ll temp = ;
if (qL <= mid) {
temp = max(temp, query(L, mid, root<<, qL, qR, flag));
}
if (qR > mid) {
temp = max(temp, query(mid + , R, root<<|, qL, qR, flag));
}
return temp;
} int main()
{
int W, H, q, x;
char c[];
while (scanf("%d %d %d", &W, &H, &q) == ) {
build(, W - , , );
build(, H - , , );
while (q--) {
scanf("%s %d", c, &x);
if (c[] == 'V') {
update_node(, W - , , x, );
}
else {
update_node(, H - , , x, );
}
printf("%I64d\n", (query(, W - , , , W - , ) + ) * (query(, H - , , , H - , ) + ));
}
}
}

非递归

#include <iostream>
#include <cstdio>
#include <cmath>
#define maxn 200001
using namespace std;
int L[maxn<<][];//从左开始连续零个数
int R[maxn<<][];//从右
int Max[maxn<<][];//区间最大连续零
bool Pure[maxn<<][];//是否全零
int M[];
void PushUp(int rt,int k){//更新rt节点的四个数据
Pure[rt][k]=Pure[rt<<][k]&&Pure[rt<<|][k];
Max[rt][k]=max(R[rt<<][k]+L[rt<<|][k],max(Max[rt<<][k],Max[rt<<|][k]));
L[rt][k]=Pure[rt<<][k]?L[rt<<][k]+L[rt<<|][k]:L[rt<<][k];
R[rt][k]=Pure[rt<<|][k]?R[rt<<|][k]+R[rt<<][k]:R[rt<<|][k];
}
void Build(int n,int k){//建树,赋初值
for(int i=;i<M[k];++i) L[M[k]+i][k]=R[M[k]+i][k]=Max[M[k]+i][k]=Pure[M[k]+i][k]=i<n;
for(int i=M[k]-;i>;--i) PushUp(i,k);
}
void Change(int X,int k){//切割,更新
int s=M[k]+X-;
Pure[s][k]=Max[s][k]=R[s][k]=L[s][k]=;
for(s>>=;s;s>>=) PushUp(s,k);
}
int main(void)
{
int w,h,n;
while(cin>>w>>h>>n){
//以下3行,找出非递归线段树的第一个数的位置。
M[]=M[]=;
while(M[]<h-) M[]<<=;
while(M[]<w-) M[]<<=;
//建树
Build(h-,);Build(w-,); for(int i=;i<n;++i){
//读取数据
char x;int v;
scanf(" %c%d",&x,&v);
//切割
x=='H'?Change(v,):Change(v,);
//输出
printf("%I64d\n",(long long)(Max[][]+)*(Max[][]+));
}
}
return ;
}

其他解法

https://blog.csdn.net/zearot/article/details/44759437

Codeforces 527C Glass Carving (最长连续0变形+线段树)的更多相关文章

  1. CodeForces 527C. Glass Carving (SBT,线段树,set,最长连续0)

    原题地址:http://codeforces.com/problemset/problem/527/C Examples input H V V V output input H V V H V ou ...

  2. Codeforces 527C Glass Carving(Set)

    意甲冠军  片w*h玻璃  其n斯普利特倍  各事业部为垂直或水平  每个分割窗格区域的最大输出 用两个set存储每次分割的位置   就能够比較方便的把每次分割产生和消失的长宽存下来  每次分割后剩下 ...

  3. Codeforces 527C Glass Carving

    vjudge 上题目链接:Glass Carving 题目大意: 一块 w * h 的玻璃,对其进行 n 次切割,每次切割都是垂直或者水平的,输出每次切割后最大单块玻璃的面积: 用两个 set 存储每 ...

  4. CF 527C Glass Carving

    数据结构维护二维平面 首先横着切与竖着切是完全没有关联的, 简单贪心,最大子矩阵的面积一定是最大长*最大宽 此处有三种做法 1.用set来维护,每次插入操作寻找这个点的前驱和后继,并维护一个计数数组, ...

  5. CF 150E Freezing with Style [长链剖分,线段树]

    \(sol:\) 给一种大常数 \(n \log^2 n\) 的做法 考虑二分,由于是中位数,我们就二分这个中位数,\(x>=mid\)则设为 \(1\),否则为 \(-1\) 所以我们只需要找 ...

  6. 最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile

    这题是杭电多校2019第六场的题目 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你平面上n个点,每个点都有权值(有负权),让你计算一 ...

  7. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  8. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  9. D. Babaei and Birthday Cake---cf629D(最长上升子序列和+线段树优化)

    http://codeforces.com/problemset/problem/629/D 题目大意: 我第一反应就是求最长上升子序列和  但是数值太大了  不能直接dp求  可以用线段树优化一下 ...

随机推荐

  1. hadoop job -kill 与 yarn application -kii(作业卡了或作业重复提交或MapReduce任务运行到running job卡住)

    问题详情  解决办法 [hadoop@master ~]$ hadoop job -kill job_1493782088693_0001 DEPRECATED: Use of this script ...

  2. tomcat使用manager管理app时需要身份验证问题

    我们可以通过图形用户界面来管理tomcat,启动tomcat,在地址栏中输入: Java代码 http://localhost:8080 就可以看见tomcat的欢迎页面,点击左边的tomcat ma ...

  3. 关于fragment生命周期的两张图片

    图1,fragment的生命周期,图2,fragment生命周期对应activity的生命周期

  4. 报错:空指针java.lang.NullPointerException 原因 Action层 private UserService userservice 上未加@Autowire注解

    java.lang.NullPointerException at com.itheima.test.Test2.fun1(Test2.java:18) at sun.reflect.NativeMe ...

  5. Halcon将裁剪后的图像还原为原始大小

    ************************************************************* * Halcon将裁剪后的图像还原为原始大小 * Author: LiGua ...

  6. R: 用 R 查看、管理文件(夹)

    文件管理主要函数: list.files( ): 查看当前目录下文件. file.show( ): 显示文件. file.access( ): 查看文件是否可读可写. file.create( ): ...

  7. php数据连接

    <?php header("Content-type: text/html;charset=utf-8");//设置编码格式为UTF-8 error_reporting(E_ ...

  8. excel导出工具

    介绍 excel导出工具 整个项目的代码结构如下 com \---utils +---demo # 案例相关 | | ExcelExportApplication.java # springboot启 ...

  9. Cookie seesion 赋值

    后台赋值: Session["uid"] = userid;//后台 后台取值: Label1.Text = Session["userid"].ToStrin ...

  10. C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值

    接收拦截+发送消息 对于处理所有消息.net 提供了wndproc进行重写 WndProc(ref Message m)protected override void WndProc(ref Mess ...