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

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

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

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;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值