AT_abc451_c 题解

· · 题解

luogu link

AT link

Solution

按照题意,用 multiset 维护树,直接模拟就能过了,删除就是先找到第一个比 h 大的数,然后把前面的全删了。

因为最多就加进来 q 个,时间复杂度是 O(q \log q),可以过。

Code

:::success[code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18;
const i128 Inf=1e35;
multiset<ll> ms;
ll q,id,h;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>q;
    while(q--)
    {
        cin>>id>>h;
        if(id==1) ms.insert(h); //加入一棵高度为 h 的树
        else
        {
            auto it=ms.upper_bound(h);//找到第一棵高度大于 h 的树
            ms.erase(ms.begin(),it);//把比它小的树全删了
        }
        cout<<ms.size()<<'\n';//剩下树的个数
    }
}

:::