E - Mex and Update

题意:
给定一个序列A,q次操作
每次操作给定 下标i和x
使得a[i]=x
求每次操作后序列的mex
思路:
不维护序列中已经存在的数字,反而维护不存在的数字
因为n为3e5范围
所以mex最多为n
用set维护不在序列中的数字,map进行操作

void solve(){
    int n,q;cin>>n>>q;
    vector<int>a(n+1);
    map<int,int>mp;
    set<int>st;
    rep(i,1,n){
        cin>>a[i];
        mp[a[i]]++;
    }
    // int mex=-1;
    rep(i,0,n){
        if(!mp.count(i))st.insert(i);
    }
    //0 1 1 2 2 2 2 5
    while(q--){
        int i,x;cin>>i>>x;
        mp[a[i]]--;
        if(mp[a[i]]==0){
            st.insert(a[i]);
        }
        a[i]=x;
        mp[x]++;
        st.erase(x);

        cout<<(*st.begin())<<endl;
    }
}
posted @ 2025-06-25 18:32  Marinaco  阅读(12)  评论(0)    收藏  举报
//雪花飘落效果