萌新求救带修莫队
查看原帖
萌新求救带修莫队
463687
年年楼主2022/1/14 18:19

T了78910

#include<bits/stdc++.h>
using namespace std;
int n,m,tot1,tot2,res;
int l=1,r=0,cs=0;
int a[200005],pos[200005],ans[200005],book[1000005];
typedef struct{
	int l,r,k,cs;
}Q;
typedef struct{
	int x,y;
}T;
T t[200005];
Q q[200005];
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
inline void scan()
{
	n=read();
	m=read();
	for(int i=1;i<=n;++i)a[i]=read();
	char ch;
	for(int i=1;i<=m;++i){
		cin>>ch;
		if(ch=='Q'){
			q[++tot1].l=read();
			q[tot1].r=read();
			q[tot1].k=tot1;
			q[tot1].cs=tot2;
		}
		else if(ch=='R'){
			t[++tot2].x=read();
			t[tot2].y=read();
		}
	}
	
}
inline bool cmp(Q x,Q y)
{
	if(pos[x.l]!=pos[y.l])return x.l<y.l;
	if(pos[x.r]!=pos[y.r])return x.r<y.r;
	return x.cs<y.cs;
}
inline void deal()
{
	int size=sqrt(tot1);
	for(int i=1;i<=tot1;++i)pos[i]=i/size;
	sort(q+1,q+tot1+1,cmp);
}
inline void add(int x)
{
	++book[a[x]];
	if(book[a[x]]==1)++res;
}
inline void sub(int x)
{
	--book[a[x]];
	if(book[a[x]]==0)--res;
}
inline void change(int x)
{
	int y;
	y=a[t[x].x];
	if(t[x].x>=l&&t[x].x<=r){
		--book[a[t[x].x]];
		if(book[a[t[x].x]]==0)--res;
		++book[t[x].y];
		if(book[t[x].y]==1)++res;
	}
	a[t[x].x]=t[x].y;
	t[x].y=y;
}
inline void solve()
{	
	for(int i=1;i<=tot1;++i){
		while(q[i].l<l)add(--l);
		while(q[i].r>r)add(++r);
		while(q[i].l>l)sub(l++);
		while(q[i].r<r)sub(r--);
		while(q[i].cs<cs)change(cs--);
		while(q[i].cs>cs)change(++cs);
		ans[q[i].k]=res;
	}
}
inline void prin()
{
	for(int i=1;i<=tot1;++i)cout<<ans[i]<<endl;
}
int main()
{
	scan();
	deal();
	solve();
	prin();
	return 0;
}
2022/1/14 18:19
加载中...