可爱带修莫队板子RE求条
查看原帖
可爱带修莫队板子RE求条
1160620
MutU楼主2024/10/23 17:27

提交上去神秘 RE

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 140000;
int n,m,len,a[N],ans[N];
int cnt[N],res;
struct query{
	int l,r,t,id;
}q[N];
int cntq;
struct Updata{
	int x,y;
}u[N];
int cntu;
int get(int x){
	return x/len;
}
bool cmp(query a,query b){
	if(get(a.l)!=get(b.l)) return get(a.l)<get(b.l);
	if(get(a.r)!=get(b.r)) return get(a.r)<get(b.r);
	return a.t<b.t;
}
inline void add(int x){
	cnt[a[x]]++;
	if(cnt[a[x]]==1) res++;
}
inline void del(int x){
	cnt[a[x]]--;
	if(cnt[a[x]]==0) res--;
}
inline void updata(int x,int y){
	if(u[x].x>=q[y].l && u[x].x<=q[y].r){
		del(a[u[x].x]);
		add(u[x].y);
	}
	swap(u[x].y,a[u[x].x]);
	return;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m;
	len=pow(n,0.666);
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++){
		char op;
		cin>>op;
		if(op=='Q'){
			cin>>q[++cntq].l;
			cin>>q[cntq].r;
			q[cntq].t=cntu,q[cntq].id=cntq;
		} 
		else cin>>u[++cntu].x,cin>>u[cntu].y;
	}
	sort(q+1,q+1+cntq,cmp);
	int l=1,r=0,now=0;
	for(int i=1;i<=cntq;i++){
		while(l<q[i].l) del(l++);
		while(l>q[i].l) add(--l);
		while(r>q[i].r) del(r--);
		while(r<q[i].r) add(++r);
		while(now<q[i].t) updata(++now,i);
		while(now>q[i].t) updata(now--,i);
		ans[q[i].id]=res;
	}
	for(int i=1;i<=cntq;i++) cout<<ans[i]<<'\n';
	return 0;
}
2024/10/23 17:27
加载中...