Codeforces 527C Glass Carving (最长连续0变形+线段树)

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 = 2 * 1e5 + 10;

struct SegTree {
    ll ls, rs, max0;
    bool is_all0;
}segTree[2][maxn<<2];

void pushup(int root, int flag) {
    SegTree &cur = segTree[flag][root], &lc = segTree[flag][root<<1], &rc = segTree[flag][root<<1|1];
    cur.ls = lc.ls + (lc.is_all0 ? rc.ls : 0);
    cur.rs = rc.rs + (rc.is_all0 ? lc.rs : 0);
    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 = 1;
        segTree[flag][root].is_all0 = true;
        return;
    }
    int mid = (L + R)>>1;
    build(L, mid, root<<1, flag);
    build(mid + 1, R, root<<1|1, 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 = 0;
        segTree[flag][root].is_all0 = false;
        return;
    }
    int mid = (L + R)>>1;
    if (pos <= mid) {
        update_node(L, mid, root<<1, pos, flag);
    }
    else {
        update_node(mid + 1, R, root<<1|1, 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)>>1;
    ll temp = 0;
    if (qL <= mid) {
        temp = max(temp, query(L, mid, root<<1, qL, qR, flag));
    }
    if (qR > mid) {
        temp = max(temp, query(mid + 1, R, root<<1|1, qL, qR, flag));
    }
    return temp;
}

int main()
{
    int W, H, q, x;
    char c[5];
    while (scanf("%d %d %d", &W, &H, &q) == 3) {
        build(1, W - 1, 1, 0);
        build(1, H - 1, 1, 1);
        while (q--) {
            scanf("%s %d", c, &x);
            if (c[0] == ‘V‘) {
                update_node(1, W - 1, 1, x, 0);
            }
            else {
                update_node(1, H - 1, 1, x, 1);
            }
            printf("%I64d\n", (query(1, W - 1, 1, 1, W - 1, 0) + 1) * (query(1, H - 1, 1, 1, H - 1, 1) + 1));
        }
    }
}

非递归

#include <iostream>
#include <cstdio>
#include <cmath>
#define maxn 200001
using namespace std;
int L[maxn<<2][2];//从左开始连续零个数
int R[maxn<<2][2];//从右
int Max[maxn<<2][2];//区间最大连续零
bool Pure[maxn<<2][2];//是否全零
int M[2];
void PushUp(int rt,int k){//更新rt节点的四个数据
    Pure[rt][k]=Pure[rt<<1][k]&&Pure[rt<<1|1][k];
    Max[rt][k]=max(R[rt<<1][k]+L[rt<<1|1][k],max(Max[rt<<1][k],Max[rt<<1|1][k]));
    L[rt][k]=Pure[rt<<1][k]?L[rt<<1][k]+L[rt<<1|1][k]:L[rt<<1][k];
    R[rt][k]=Pure[rt<<1|1][k]?R[rt<<1|1][k]+R[rt<<1][k]:R[rt<<1|1][k];
}
void Build(int n,int k){//建树,赋初值
    for(int i=0;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]-1;i>0;--i) PushUp(i,k);
}
void Change(int X,int k){//切割,更新
    int s=M[k]+X-1;
    Pure[s][k]=Max[s][k]=R[s][k]=L[s][k]=0;
    for(s>>=1;s;s>>=1) PushUp(s,k);
}
int main(void)
{
    int w,h,n;
    while(cin>>w>>h>>n){
        //以下3行,找出非递归线段树的第一个数的位置。
        M[0]=M[1]=1;
        while(M[0]<h-1) M[0]<<=1;
        while(M[1]<w-1) M[1]<<=1;
        //建树
        Build(h-1,0);Build(w-1,1);

        for(int i=0;i<n;++i){
            //读取数据
            char x;int v;
            scanf(" %c%d",&x,&v);
            //切割
            x==‘H‘?Change(v,0):Change(v,1);
            //输出
            printf("%I64d\n",(long long)(Max[1][0]+1)*(Max[1][1]+1));
        }
    }
return 0;
}
 

其他解法

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

原文地址:https://www.cnblogs.com/shuaihui520/p/9835973.html

时间: 2024-08-28 05:05:44

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

Codeforces 527C Glass Carving(Set)

题意  一块w*h的玻璃  对其进行n次切割  每次切割都是垂直或者水平的  输出每次切割后最大单块玻璃的面积 用两个set存储每次切割的位置   就可以比较方便的把每次切割产生和消失的长宽存下来  每次切割后剩下的最大长宽的积就是答案了 #include <bits/stdc++.h> using namespace std; const int N = 200005; typedef long long LL; set<int>::iterator i, j; set<i

Codeforces 527C Glass Carving

vjudge 上题目链接:Glass Carving 题目大意: 一块 w * h 的玻璃,对其进行 n 次切割,每次切割都是垂直或者水平的,输出每次切割后最大单块玻璃的面积: 用两个 set 存储每次切割的位置,就可以比较方便的把每次切割产生和消失的长宽存下来(用个 hash 映射数组记录下对应值的长宽的数量即可,O(1) 时间维护),每次切割后剩下的最大长宽的积就是答案了: 1 #include<cstdio> 2 #include<cstring> 3 #include<

CF 527C Glass Carving

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

SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树

题目链接:点击打开链接 维护区间左起连续的最大和,右起连续的和.. #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <vector> #include <map> using namespace std; #define N 50050 #define Lson

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

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

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

BZOJ 3904 最长上升子序列 lkids 线段树

题目大意:给定一个序列,求以较小数开始的锯齿子序列,使相邻两项之间差值不小于k 令f[i][0]表示第i个数为序列中的较大值的最长子序列 f[i][1]表示第i个数为序列中的较小值的最长子序列 暴力转移是O(n^2)的 我们发现决策点的值都是连续的一段区间 因此用线段树维护一下就行了 (真简略) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #def

最大连续和(线段树+分治)

最大连续和 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 61(13 users) Total Accepted: 15(9 users) Rating: Special Judge: No Description 给出一个长度为n的整数序列D,你的任务是对m个询问做出回答.对于询问(a,b),需要找到 两个下标x和y,使得a<=x<=y<=b,并且Dx+Dx+1+....+Dy尽量大.如果有多组满足条件的x和y,x 应尽

Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所击败了(l<=x<=r),被击败的人立马退出游戏让你最后输出每个人是被谁击败的,最后那个胜利者没被 人击败就输出0 思路:他的每次修改的是一个区间的被击败的人,他而且只会记录第一次那个被击败的人,用线段树堕落标记的话他会记录最后一次的,所以我们倒着来修改, 然后因为那个区间里面还包含了自己,在线段