求调!带修莫队模板 3AC+3TLE+7WA
查看原帖
求调!带修莫队模板 3AC+3TLE+7WA
564732
TimSwn090306楼主2022/9/17 14:55

求救已经调了一个上午了qwq record

#include <bits/stdc++.h>
using namespace std;
const int maxn=133334;
const int maxk=1e6+1;
struct node{
	int l,r;
	int t,id;
}q[maxn];
struct change{
	int pos,from,to;
}c[maxn];
int n,m,now,tot,block,ans,l=1,r;
int a[maxn],cnt[maxk],res[maxn]; 
inline bool cmp(node x,node y){
	if (x.l/block!=y.l/block) return x.l/block<y.l/block;
	if (x.r/block!=y.r/block) return x.r/block<y.r/block;
	return x.t>y.t;
}
inline void add(int pos){
	if (++cnt[a[pos]]==1) ans++;
}
inline void del(int pos){
	if (--cnt[a[pos]]==0) ans--;
}
inline void upd(int time){
	if (c[time].pos>=l && c[time].pos<=r){
		del(c[time].pos);
		a[c[time].pos]=c[time].to;
		add(c[time].pos);
	}else a[c[time].pos]=c[time].to;
} 
inline void ret(int time){
	if (c[time].pos>=l && c[time].pos<=r){
		del(c[time].pos);
		a[c[time].pos]=c[time].from;
		add(c[time].pos);
	}else a[c[time].pos]=c[time].to;
}
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1,x,y;i<=m;i++){
		char tmp;
		cin>>tmp;
		scanf("%d%d",&x,&y);
		if (tmp=='Q') q[++tot]=(node){x,y,now,tot};
		else{
			c[++now]=(change){x,a[x],y};
			a[x]=y;
		}
	}
	block=sqrt(tot);
	sort(q+1,q+tot+1,cmp);
	for (int i=1;i<=tot;i++){
		while (now<q[i].t) upd(++now);
		while (now>q[i].t) ret(now--);
		while (l<q[i].l) del(l++);
		while (l>q[i].l) add(--l);
		while (r<q[i].r) add(++r);
		while (r>q[i].r) del(r--);
		res[q[i].id]=ans; 
	}
	for (int i=1;i<=tot;i++) printf("%d\n",res[i]);
	return 0;
}

2022/9/17 14:55
加载中...