萌新求调带修莫队
查看原帖
萌新求调带修莫队
274935
_Z_Y_X_楼主2021/11/15 20:15

rt 只过了后面3个点 前面全WA

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e6+5;
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;
}
char op;
int n,m,l,r,ans[maxn],a[maxn],belong[maxn],cnt[maxn];
int cntqwq,cntwww,block,blocks; //Q的出现次数和 R 的   block为块长 
struct node{
	int l,r,id,t;
}qwq[maxn];
struct change{
	int color,pos;
}www[maxn];
/*
bool cmp(node x,node y){
	if(belong[x.l]!=belong[y.l]) return x.l<y.l;
	if(belong[x.r]!=belong[y.r]) return x.r<y.r;
	return x.t<y.t; 
}
*/
int cmp(node a,node b) {
	return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.r] ^ belong[b.r]) ? belong[a.r] < belong[b.r] : a.t < b.t);
}
signed main(){
	n=read();
	m=read();
	//预处理 
	block=pow(n,0.6666);
	blocks=ceil((double)n/block);
	for(int i=1;i<=blocks;i++){
		for(int j=(i-1)*block+1;j<=i*block;j++){
			belong[j]=i;
		}
	}
	//输入 
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	for(int i=1;i<=m;i++){
		//op=getchar();
		char opt[5];
		scanf("%s",opt);
		if(opt[0]=='Q'){
			qwq[++cntqwq].l=read();			
			qwq[cntqwq].r=read();
			qwq[cntqwq].t=cntwww;
			qwq[cntqwq].id=cntqwq;
		}
		else if(opt[0]=='R'){
			www[++cntwww].pos=read();
			www[cntwww].color=read();
		}
	}
	sort(qwq+1,qwq+1+cntqwq,cmp);
	//核心操作 
	int l=1,r=0,t=0,now=0;
	for(int i=1;i<=cntqwq;i++){
		int al=qwq[i].l,ar=qwq[i].r,at=qwq[i].t;
		while(l<al) now-= !--cnt[a[l++]];
		while(l>al) now+= !cnt[a[--l]]++;
		while(r<ar) now+= !cnt[a[++r]]++;
		while(r>ar) now-= !--cnt[a[r--]];
		while(t<at){
			++t;
			if(al<=www[t].pos&&www[t].pos<=ar) now-= !--cnt[a[www[t].pos]]-! cnt[a[www[t].color]]++;
			swap(a[www[t].pos],www[t].color);
		}
		while(t>at){
			if(al<=www[t].pos&&www[t].pos<=ar) now-= !--cnt[a[www[t].pos]]-! cnt[a[www[t].color]]++;
			swap(a[www[t].pos],www[t].color);
			--t;
		}
		ans[qwq[i].id]=now;
	}
	//输出 
	for(int i=1;i<=cntqwq;i++){
		printf("%lld\n",ans[i]);
	}
	return 0;
}
2021/11/15 20:15
加载中...