Educational Codeforces Round 95 (Rated for Div. 2)补题

题目:D. Trash Problem

分析:

用set维护元素(将线性表的增删操作由O(n)优化成O(log(n))),用multiset维护差分.
学习迭代器相关函数。(prev, next)
注意点:multiset的erase(x)表示删除所有为x的值,erase(it)删除迭代器指向的单个元素。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;
set<int> s;
multiset<int> ms;

void add(int x)
{
    auto it = s.lower_bound(x);
    if(it != s.begin() && it != s.end())
    {
        int dif = *it - *prev(it);
        ms.erase(ms.lower_bound(dif));
    }
    if (it != s.end())
        ms.insert(*it - x);
    if(it != s.begin())
        ms.insert(x - *prev(it));
    s.insert(x);
}

void del(int x)
{
    set<int>::iterator it = s.lower_bound(x);
    it = s.erase(it);
    if (it != s.begin()) ms.erase(ms.lower_bound(x - *prev(it)));
    if (it != s.end()) ms.erase(ms.lower_bound(*it - x));
    if (it != s.begin() && it != s.end())
        ms.insert(*it - *prev(it));
}

int calc()
{

    return s.size() <= 2 ? 0 : *s.rbegin() - *s.begin() - *ms.rbegin();
}

int main()
{
    close();
    int n, q; cin >> n >> q;
    for(int i = 1; i <= n; i++)
    {
        int x; cin >> x;
        add(x);
    }
    cout << calc() << endl;
    while (q--)
    {
        int ope, x; cin >> ope >> x;
        if(ope == 1)
        {
            add(x);
            cout << calc() << endl;
        }
        else
        {
            del(x);
            cout << calc() << endl;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值