跳到正文

算法·理论

小信拿金币

gmb729约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年12月30日
收录
2025年12月12日
更新
2026年10月17日
篇幅
约 4 分钟

版本

  1. v1当前

思路:!

代码

cpp#include<bits/stdc++.h>
using namespace std;
int n,q,op,x,ans,e=1000;
int p[100005];
set<int> s;//用到两个容器一个set 一个multiset
multiset<int> res;
int main(){
    int n,q,i,op,x;
    cin>>n>>q;//输入
    for(int i=1;i<=n;i++)cin>>p[i];
    sort(p+1,p+1+n);//排序
    for(i=1;i<=n;i++){
        if(i!=n)res.insert(p[i]-p[i+1]);//求差
        s.insert(p[i]);
    }
    if(s.size()<=2)cout<<0<<endl;//判断开始时是否不合法
    else cout<<*res.begin()-*s.begin()+*s.rbegin()<<endl;
    while(q--){
        cin>>op>>x;
        if(op!=1){
            auto it=s.lower_bound(x);//找数
            auto nex=it;
            ++nex;//地址自增
            if(it==s.begin()){
                int u=*nex;
                if(nex!=s.end()) res.erase(res.find(x-u));
                s.erase(it);//删除
            }
            else{
                auto pre=it;
                pre--;
                res.erase(res.find(*pre-x));//.....
                if(nex!=s.end()){
                    res.erase(res.find(x-*nex));//.....
                    res.insert(*pre-*nex);//.....
                }
                s.erase(it);//.....
            }
        }
        else{
            s.insert(x);//.....
            auto it=s.lower_bound(x);//.....
            auto nex=it;
            nex++;
            if(it!=s.begin()){//.....
                auto pre=it;//.....
                pre--;
                int u=*pre,o=*nex;//.....
                res.insert(u-x);//.....
                if(nex!=s.end()){//.....
                    res.insert(x-o);//.....
                    res.erase(res.find(u-o));//.....
                }
            }
            else{
                int o=*nex;
                if(nex!=s.end()) res.insert(x-*nex);//.....
            }
        }
        if(s.size()<=2) cout<<0<<endl;//.....
        else cout<<*res.begin()-*s.begin()+*s.rbegin()<<endl;//.....
    }
    return 0;
}
原文