E - 1D Bucket Tool

题意:

初始颜色数组ai=i,两种操作
操作1:将x位置即其左右与他颜色相同的格子都涂为c
操作2:输出颜色c有多少个格子

思路:

珂朵莉树,每次维护修改x的区间后左右两个区间是否颜色相同,如果相同则变为同一个

struct node{
    int l,r;
    mutable int val;
    bool operator<(const node&a)const{
        return l<a.l;
    }
};
set<node>odt;

int col[maxn];
map<int,int>mp;

void solve(){
    int n,q;cin>>n>>q;
    rep(i,1,n)odt.insert((node){i,i,i});
    int cnt=0;

    rep(i,1,n)mp[i]=1;

    while(q--){
        int opt;cin>>opt;
        if(opt==1){
            int x,c;cin>>x>>c;
            auto it=odt.lower_bound((node){x,-1,0});
            if(x!=it->l)it--;
            int L=it->l,R=it->r,col=it->val;
            mp[col]-=(R-L+1);
            mp[c]+=(R-L+1);
            auto nxt=next(it);
            if(it!=odt.begin()){
                auto last=prev(it);
                if(last->val==c){
                    L=last->l;odt.erase(last);
                }
            }
            if(nxt!=odt.end()&&nxt->val==c){
                R=nxt->r;odt.erase(nxt);
            }
            odt.erase(it);
            odt.insert((node){L,R,c});
        }else{
            int c;cin>>c;
            cout<<mp[c]<<endl;
        }
    }
}
posted @ 2025-06-18 22:20  Marinaco  阅读(14)  评论(0)    收藏  举报
//雪花飘落效果