bzoj1058 [ZJOI2007]报表统计(stl)

本文介绍了一种使用STL标准库进行数据处理的方法,包括利用优先队列实现动态数据更新及使用集合来查找最小间隔值。通过具体实例演示了如何在C++中运用这些数据结构解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

都可以用stl实现就好了。第一问用个能删除的堆,第二问用个set。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 500010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m,ans=inf;
vector<int>a[N];
set<int>st;
set<int>::iterator it;
char op[20];
inline void get_S(){
    char ch=gc();int len=0;
    while(ch!='I'&&ch!='M') ch=gc();
    while(ch=='_'||ch>='A'&&ch<='Z') op[++len]=ch,ch=gc();
}
struct Heap{
    priority_queue<int,vector<int>,greater<int> >q1,q2;
    inline void push(int x){q1.push(x);}
    inline void del(int x){q2.push(x);}
    inline int top(){while(!q2.empty()&&q1.top()==q2.top()) q1.pop(),q2.pop();return q1.top();}
}Q;
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    for(int i=1;i<=n;++i){
        int x=read();a[i].push_back(x);if(i==1){st.insert(x);continue;}
        Q.push(abs(x-a[i-1][0]));if(!ans) continue;
        it=st.lower_bound(x);if(it!=st.end()){
            if(*it==x){ans=0;continue;}ans=min(ans,*it-x);
        }if(it!=st.begin()) ans=min(ans,x-*(--it));st.insert(x);
    }while(m--){
        get_S();
        if(op[1]=='I'){
            int id=read(),x=read();Q.push(abs(x-a[id].back()));
            if(id!=n){
                Q.del(abs(a[id].back()-a[id+1][0]));
                Q.push(abs(x-a[id+1][0]));
            }a[id].push_back(x);if(!ans) continue;
            it=st.lower_bound(x);if(it!=st.end()){
                if(*it==x){ans=0;continue;}ans=min(ans,*it-x);
            }if(it!=st.begin()) ans=min(ans,x-*(--it));
            st.insert(x);continue;
        }if(op[5]=='G') printf("%d\n",Q.top());
        else printf("%d\n",ans);
    }return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值