Glass Carving CodeForces - 527C(线段树/multiset)

博客围绕矩形切割问题展开,给出一个矩形,有水平和垂直切割操作,需计算每次操作后最大矩形面积。介绍了两种解题思路,一是用线段树维护切割点信息,二是用multiset分别维护宽和高的每段长及切割点。

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.

Example 

Input


4 3 4 
H 2 
V 2 
V 3 
V 1

Output





2

Input 

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

Output 

28 
16 
12 

4

题目大意:给出一个矩形,有两种操作,一是对举行进行水平切割,二是进行垂直切割,问每次操作过后最大的矩形面积是多少

思路1:分析可知,完成一次操作后,最大的矩形面积就是长和宽的最长线段相乘。对于每个切割点,可以看成0和1,1代表进行切割(如下图),这样求最长的一段就是求最长的连续0再加1。由于每次都要进行修改和查询,可以用线段树来维护。每个结点需要维护的信息有:区间左最长连续0,右最长连续0,区间是否全0(合并时求父区间的左最长和右最长要用),区间最长连续0四个信息。查询则直接查st[1]即可。

思路2:用multiset,分别维护宽和高的每一段长,以及每个切割点。对于每一次操作,从multiset1中取出离该点最近的左右切割点。可以计算出切割后形成的两段长。同时除去multiset2中左右切割点之间这段长度,添加新形成的两段。计算则只需取出宽高的set中最长的相乘即可。这里得用multiset,因为维护长度的set中,切割后可能形成长度相同的线段。

struct Node{
    int lz,rz,az,mz;
}st[maxn*4][2];

void pushUp(int rt,int k)
{
    st[rt][k].lz = (st[rt<<1][k].az == 1) ? (st[rt<<1][k].lz+st[rt<<1|1][k].lz) : (st[rt<<1][k].lz);
    st[rt][k].rz = (st[rt<<1|1][k].az == 1) ? (st[rt<<1|1][k].rz+st[rt<<1][k].rz) : (st[rt<<1|1][k].rz);
    st[rt][k].az = (st[rt<<1][k].az & st[rt<<1|1][k].az);
    st[rt][k].mz = max(st[rt<<1][k].mz,max(st[rt<<1|1][k].mz,st[rt<<1][k].rz+st[rt<<1|1][k].lz));
    //dbg(st[rt][k].lz,st[rt][k].rz,st[rt][k].az,st[rt][k].mz);
}

void build(int rt,int l,int r,int k)
{
    if(l == r)
    {
        st[rt][k].lz = st[rt][k].rz = st[rt][k].az = 1;
        st[rt][k].mz = 1;
    }
    else
    {
        int m = (l + r) >> 1;
        build(rt<<1,l,m,k);
        build(rt<<1|1,m+1,r,k);
        pushUp(rt,k);
    }
}

void update(int rt,int l,int r,int q,int k)
{
    //dbg(k,"**");
    if(l == r)
    {
        st[rt][k].az = st[rt][k].lz = st[rt][k].rz = st[rt][k].mz = 0;
    }
    else
    {
        int m = (l+r) >> 1;
        if(q <= m) update(rt<<1,l,m,q,k);
        else update(rt<<1|1,m+1,r,q,k);
        pushUp(rt,k);
        //dbg(rt,st[rt][k].mz);
    }
}


void work()
{
    int w,h,n;
    while(cin>>w>>h>>n)
    {
        build(1,1,w-1,0);
        build(1,1,h-1,1);
        //dbg(st[1][1].mz);
        for(int i = 0; i < n; i++)
        {
            string op;
            int x;
            cin>>op>>x;
            //dbg(op);
            if(!op.compare("H"))
                update(1,1,h-1,x,1);
            else update(1,1,w-1,x,0);
            //dbg(st[1][1].mz,st[1][0].mz);
            cout<<(1+st[1][0].mz)*(1+st[1][1].mz)<<endl;
        }
    }
}

multiset<int> cutpoint[2],length[2];

void add(int k,int x)
{
    int p = *(--cutpoint[k].upper_bound(x));
    int q = *(cutpoint[k].upper_bound(x));
    length[k].erase(length[k].find(q-p));
    length[k].insert(q-x);
    length[k].insert(x-p);
    cutpoint[k].insert(x);
}

void work()
{
    int n,w,h;
    while(cin>>w>>h>>n)
    {
        cutpoint[0].insert(0);
        cutpoint[0].insert(w);
        cutpoint[1].insert(0);
        cutpoint[1].insert(h);
        length[0].insert(w);
        length[1].insert(h);
        for(int i = 0; i < n; i++)
        {
            string op;
            int x;
            cin>>op>>x;
            if(!op.compare("H"))
                add(1,x);
            else add(0,x);
            cout<<*length[0].rbegin()*(*length[1].rbegin())<<endl;
        }
    }
}

 

### 关于C++线段树实现中的错误分析 在线段树的实现过程中,可能会遇到多种类型的错误。以下是常见的几种原因及其可能引发的问题: #### 1. **未初始化变量** 如果在构建线段树之前没有对数组或其他存储结构进行清零处理,则可能导致残留数据干扰计算结果[^3]。 例如,在某些情况下,`sum` 或 `lazy` 数组如果没有被正确初始化为零,后续的操作会受到先前数据的影响。 ```cpp // 初始化操作 memset(sum, 0, sizeof(sum)); memset(lazy, 0, sizeof(lazy)); ``` #### 2. **懒惰标记(Lazy Propagation)问题** 当使用带延迟更新的线段树时,忘记下传懒惰标记或者错误地实现了 `push_down` 函数,都会导致查询结果不准确[^2]。 ```cpp void push_down(int p, int l, int r) { if (lazy[p]) { int mid = (l + r) >> 1; lazy[left(p)] += lazy[p]; sum[left(p)] += (mid - l + 1) * lazy[p]; lazy[right(p)] += lazy[p]; sum[right(p)] += (r - mid) * lazy[p]; lazy[p] = 0; // 清除当前节点的懒惰标记 } } ``` #### 3. **边界条件处理不当** 对于区间的划分和递归终止条件,如果不小心设置错误,可能会导致无限递归或错过目标区间[^4]。 例如,在以下代码片段中,如果 `if(tree[i].l > X || tree[i].r < X)` 条件写错,就无法正常退出递归。 ```cpp void find(int i) { if (tree[i].l > X || tree[i].r < X) return ; ans += tree[i].cnt; find(i * 2); find(i * 2 + 1); } ``` #### 4. **内存分配不足** 由于线段树通常需要两倍甚至四倍的空间来存储节点信息,因此如果数组大小定义过小,就会发生越界访问。 建议按照实际需求合理规划空间大小,比如将数组容量设为 `N << 2`。 ```cpp const int MAX_N = 1e6 + 5; int segTree[MAX_N << 2]; // 开辟足够的空间 ``` #### 5. **逻辑错误** 有时算法本身的逻辑存在缺陷,例如在解决特定题目时未能充分考虑特殊情况。以 CF527C Glass Carving 为例,若未维护好最大矩形面积的变化过程,则难以得出正确答案[^5]。 --- ### 总结 综上所述,C++ 中线段树实现可能出现的错误主要包括但不限于以上几点。开发者应仔细检查每一处细节,并通过调试工具验证程序行为是否符合预期。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值