题解:P17500 [ICPC 2026 Wuhan I] Rectangle Cutting

· · 题解

题目分析

给一个长方形,每次操作会横着或竖着切一刀,求每次操作后最大的长方形。

思路

通过画图得知,每一次切割都会使在这一行或列上的长方形分隔开,所以所有长方形,都是由行切割坐标排序后相邻两项差,列切割坐标排序后相邻两项差,而最大的长方形就是长和宽最大的那一项。

优化

由于本题操作次数达 5\times10^5 次,普通插入排序时间复杂度为 O(n^2),我们可以用平衡树实现的 set 进行优化。

代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    long long n,m;
    int q;
    cin>>n>>m>>q;
    multiset<long long>a,b;
    a.insert(0);a.insert(n);
    b.insert(0);b.insert(m);
    multiset<long long>x,y;
    x.insert(n);
    y.insert(m);
    while(q--){
        int op;
        long long k;
        cin>>op>>k;
        if(op==1){
            a.insert(k);
            auto it=a.find(k);
            auto pr=prev(it);
            auto nx=next(it);
            long long d=*nx-*pr;
            x.erase(x.find(d));
            x.insert(k-*pr);
            x.insert(*nx-k);
            cout<<(*x.rbegin())*(*y.rbegin())<<"\n";
        }else{
            b.insert(k);
            auto it=b.find(k);
            auto pr=prev(it);
            auto nx=next(it);
            long long d=*nx-*pr;
            y.erase(y.find(d));
            y.insert(k-*pr);
            y.insert(*nx-k);
            cout<<(*x.rbegin())*(*y.rbegin())<<"\n";
        }
    }
    return 0;
}