CodeForces - 527C Glass Carving (二分+set) 多维矩形切割问题

博客介绍了如何解决CodeForces上的一道多维矩形切割问题(527C Glass Carving)。通过转化为一维线段切割,利用二分查找和集合数据结构维护切点与线段长度,从而在每次切割后快速找到最大块的尺寸。这种方法将复杂度降低到log(N)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

        给出一个高为 H ,宽为 V 的矩形。要经过 N 次切割,问每次切割后最大的一块是多少?

思路:

       为简化思考,本题题意为二维切割,我们不妨先考虑将问题转化为一位线段的切割问题思考。

       我们可以通过维护 切点 及 从每个切点开始的线段长度 得知每次操作后的最大长度。

       即 利用 数组 维护从每个切点开始的线段长度

            利用 set 维护 切点

            利用 multiset 维护 所有线段的长度 以便求最大长度

       在增加切点时利用 set 的二分查找和 multiset 的相关操作可以将效率压在 log(N)。

       二维,多维同理

代码:

#include <bits/stdc++.h>

using namespace std;
const int MAXN=2e5+100;
set <int> x;
set <int> y;
set <int>::iterator it;
multiset <int> ansx;
multiset <int> ansy;
multiset <int>::iterator it1,it2;
int lenx[MAXN];
int leny[MAXN];
char ch;
int n,m,k,v,temp;
long long mh,mv;
void ini(){
    x.clear();ansx.clear();
    y.clear();ansy.clear();
    memset(lenx,0,sizeof(lenx));memset(leny,0,sizeof(leny));
    ansx.insert(n); ansy.insert(m);//...........................最初的线段长度,即线段的总长
    x.insert(0);y.insert(0);
    x.insert(n);y.insert(m);//..................................将线段头尾设置为切点
    lenx[0]=n;lenx[n]=0;//......................................将线段头尾设成切点后,切点对应的线段长度
    leny[0]=m;leny[m]=0;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m>>k){
        ini();
        while(k--){
            cin>>ch>>v;
            if(ch=='H'){
                it=y.lower_bound(v);--it;//.....................找到添加切点前的一个切点,并更新
                temp=*it;
                it1=ansy.find(leny[temp]);
                ansy.erase(it1);
                leny[v]=leny[temp]+temp-v;
                leny[temp]=v-temp;
                ansy.insert(leny[temp]);
                ansy.insert(leny[v]);
                y.insert(v);
            }else{
                it=x.lower_bound(v);--it;//.....................同理
                temp=*it;
                it1=ansx.find(lenx[temp]);
                ansx.erase(it1);
                lenx[v]=lenx[temp]+temp-v;
                lenx[temp]=v-temp;
                ansx.insert(lenx[temp]);
                ansx.insert(lenx[v]);
                x.insert(v);
            }
            it1=ansx.end();it2=ansy.end();//.....................找出最大的线段
            --it1;--it2;
            mh=*it1;mv=*it2;
            cout<<mh*mv<<endl;
        }
    }
}






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 0001 ≤ n ≤ 200 000).

Next n lines contain the descriptions of the cuts. Each description has the formH y or V x. In the first case Leonid makes the horizontal cut at the distance ymillimeters (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
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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值