【模板】带修莫队 / [国家集训队] 数颜色 / 维护队列

题目跳转

代码

#include<bits/stdc++.h>
using namespace std;
typedef int ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
inline void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxn=133335;
ll n,m,bl,qcnt,ccnt,cnt[maxn<<1],a[maxn],b[maxn],res,ans[maxn],mx;
vector<ll> lsh;
struct query{//分别表示查询的左、右端点,第几次查询,前面有几次修改 
	ll l,r,id,pre;
}q[maxn];
struct change{//分别表示在哪里修改,修改前是啥,修改后是啥 
	ll pos,pre,suf;
}c[maxn];
inline bool cmp(const query &u,const query &v){
	if(b[u.l]!=b[v.l]){
		return b[u.l]<b[v.l];
	}
	if(b[u.r]!=b[v.r]){
		if(b[u.l]&1){
            return b[u.r]>b[v.r];
        }else{
            return b[u.r]<b[v.r];
        }
	}
	if(u.pre!=v.pre){
        if(b[u.r]&1){
            return u.pre>v.pre;
        }else{
            return u.pre<v.pre;
        }
    }
    return u.id<v.id;
}
inline void add(ll p){
	cnt[a[p]]++;
	if(cnt[a[p]]==1){
		res++;
	}
}
inline void del(ll p){
	cnt[a[p]]--;
	if(cnt[a[p]]==0){
		res--;
	}
}
int main(){
	n=rd();
	m=rd();
	bl=pow(n,2.0/3)+1;
	for(ll i=1;i<=n;i++){
		a[i]=rd();
        lsh.emplace_back(a[i]);
		b[i]=(i-1)/bl+1;
	}
	for(ll i=1;i<=m;i++){
		char ch=gc();
		while(ch!='Q'&&ch!='R'){
			ch=gc();
		}
		if(ch=='Q'){
			q[++qcnt].l=rd();
			q[qcnt].r=rd();
			q[qcnt].id=qcnt;
			q[qcnt].pre=ccnt;
		}else{
			c[++ccnt].pos=rd();
			c[ccnt].suf=rd();
			lsh.push_back(c[ccnt].suf);
		}
	}
    sort(lsh.begin(),lsh.end());
    lsh.erase(unique(lsh.begin(),lsh.end()),lsh.end());
    for(ll i=1;i<=n;i++){
        a[i]=lower_bound(lsh.begin(),lsh.end(),a[i])-lsh.begin()+1;
    }
    for(ll i=1;i<=ccnt;i++){
        ll pos=c[i].pos;
        c[i].pre=a[pos];
        c[i].suf=lower_bound(lsh.begin(),lsh.end(),c[i].suf)-lsh.begin()+1;
        a[pos]=c[i].suf;
    }
	for(ll i=ccnt;i>=1;i--){
		a[c[i].pos]=c[i].pre;
	}
	sort(q+1,q+qcnt+1,cmp);
	ll L=1,R=0,ti=0;
	for(ll i=1;i<=qcnt;i++){
		ll l=q[i].l,r=q[i].r,pre=q[i].pre;
		while(L>l){
			add(--L);
		}
		while(R<r){
			add(++R);
		}
		while(L<l){
			del(L++);
		}
		while(R>r){
			del(R--);
		}
		while(pre<ti){//多改了要还原回去
			ll pos=c[ti].pos;
			if(pos>=l&&pos<=r){
				cnt[a[pos]]--;
				if(cnt[a[pos]]==0){
					res--;
				}
			}
			a[pos]=c[ti].pre;
			ti--;
			if(pos>=l&&pos<=r){
				cnt[a[pos]]++;
				if(cnt[a[pos]]==1){
					res++;
				}
			}
		}
		while(pre>ti){//少了要修改
			ti++;
			ll pos=c[ti].pos;
			if(pos>=l&&pos<=r){
				cnt[a[pos]]--;
				if(cnt[a[pos]]==0){
					res--;
				}
			}
			a[pos]=c[ti].suf;
			if(pos>=l&&pos<=r){
				cnt[a[pos]]++;
				if(cnt[a[pos]]==1){
					res++;
				}
			}
		}
		ans[q[i].id]=res;
	}
	for(ll i=1;i<=qcnt;i++){
		write(ans[i]);
		pc('\n');
	}
    fls();
    return 0;
}
posted @ 2026-11-27 08:34  beiciqi  阅读(9)  评论(0)    收藏  举报