待修莫队90ptsWA#3求调
查看原帖
待修莫队90ptsWA#3求调
1717150
Aurelia_Veil楼主2025/7/19 16:47

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+111;
int n,m,le,cnt[N];
struct slt{
	int l,r,t,id,k;
}b[N];
struct ins{
    int x,pre,now;
}tt[N];
int a[N]; 
int last[N];
bool vis[N];
int c[N];
int sid[N];
bool cmp(slt a,slt b){
    if(sid[a.l]!=sid[b.l]){
        return sid[a.l]<sid[b.l];
    }
    if(sid[a.r]!=sid[b.r]){
        return sid[a.r]<sid[b.r];
    }
    return a.t<b.t;
}
void add(int x){
    if(vis[x]==0){
    	cnt[a[x]]++;
    }else{
    	cnt[a[x]]--;
    }
    vis[x]=!vis[x];
}
void inst(int x,int pre,int now){
    if(vis[x]){
        add(x);
        a[x]=now;
        add(x);
    }else{
        a[x]=now;
    }
}
map<int,int>mp;
int al[N];
void _lis(){
	sort(al+1,al+n+1);
	int coun=0;
	for(int i=1;i<=n;i++){
		if(i>1&&al[i]==al[i-1]){
			continue;
		}
		mp[al[i]]=++coun;
	}
	for(int i=1;i<=n;i++){
		a[i]=mp[a[i]];
	}
}
void _init(){
    for(int i=1;i<=n;i++){
        sid[i]=(i-1)/le+1;
        last[i]=a[i];
    }
}
signed main(){
	scanf("%d%d",&n,&m);
	le=pow(n,2.0/3);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		al[i]=a[i];
	}
	_lis();
    _init();
    int nowtt=0;
    int _nowtt=0;
	for(int i=1;i<=m;i++){
        char op;
        cin>>op;
        if(op=='Q'){
            _nowtt++;
    		scanf("%d%d%d",&b[_nowtt].l,&b[_nowtt].r,&b[_nowtt].k);
    		b[_nowtt].k=mp[b[_nowtt].k];
            b[_nowtt].t=nowtt;
    		b[_nowtt].id=_nowtt;
        }else{
            int x,y;
            scanf("%d%d",&x,&y);
            y=mp[y];
            nowtt++;
            tt[nowtt].x=x;
            tt[nowtt].pre=last[x];
            tt[nowtt].now=y;
            last[x]=y;
        }
	}
	sort(b+1,b+_nowtt+1,cmp);
	int topl=1,topr=0,topt=0;
	for(int i=1;i<=_nowtt;i++){
		int nowl=b[i].l,nowr=b[i].r;
        int nowt=b[i].t;
		while(topt<nowt){
			topt++;
			inst(tt[topt].x,tt[topt].pre,tt[topt].now);
		}
		while(topt>nowt){
			inst(tt[topt].x,tt[topt].now,tt[topt].pre);
			topt--;
		}
		while(topl<nowl){
			add(topl);
			topl++;
		}
		while(topl>nowl){
			topl--;
			add(topl);
		}
		while(topr<nowr){
			topr++;
			add(topr);
		}
		while(topr>nowr){
			add(topr);
			topr--;
		}
		c[b[i].id]=cnt[b[i].k];
	}
	for(int i=1;i<=_nowtt;i++){
        printf("%d\n",c[i]);
	}
    return 0;
}



2025/7/19 16:47
加载中...