跳到正文

题解

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

cgy20140502约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年9月28日
收录
2026年9月28日
更新
2026年11月16日
篇幅
约 3 分钟
题目
P17500

版本

  1. v1当前

set 基础题。

思路

众所周知,set 是一个用于存储一组唯一且有序的元素的容器,基于 set 的特性,我们可以解决这道题。

首先,我们想到 set 的自动排序,那么基于这个特性,我们可以把我们更改的位置装进 set 里。

我们还得建一个 multiset 用于存储段的长度,因为段的长度是可以重复的。

然后对于每一个位置,我们都可以找到两根线,一根在 kk 位置左边,一根在 kk 位置右边,对于这两根线,找到他们那个段的位置,把这个段断成两截,就可以了。

说句闲话:迭代器真的很烦啊。

代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,q;
set <int> xq,yq;
multiset <int> xl,yl;
signed main(){
    cin >> n >> m >> q;
    int op,k;
    xq.insert(0),xq.insert(n);//自带的 0 和 n 两个边界线
    yq.insert(0),yq.insert(m);//自带的 0 和 m 两个边界线
    xl.insert(n),yl.insert(m);//自带的 n 和 m 两个段
    while (q--){
        cin >> op >> k;
        if (op == 1){
            auto it = xq.lower_bound(k);//找到要改的那根线的左边的那根线
            int r = *it;//左边那根线
            int l = *prev(it);//右边那根线
            auto aaa = xl.find(r-l);
            xl.erase(aaa);//擦除那个段
            xl.insert(r-k),xl.insert(k-l);//断成两截
            xq.insert(k);//插入新的一根线
        }else{//y 轴同理
            auto it = yq.lower_bound(k);
            int r = *it;
            int l = *prev(it);
            auto aaa = yl.find(r-l);
            yl.erase(aaa);
            yl.insert(r-k),yl.insert(k-l);
            yq.insert(k);
        }
        cout << (*xl.rbegin())*(*yl.rbegin()) << "\n";//rbegin 是找到最大的那一边
    }
    return 0;
}
原文