P1903-[國家集訓隊]數顏色/維護隊列【帶修莫隊】

正題

題目鏈接:https://www.luogu.com.cn/problem/P1903


題目大意

要求支持兩個操作

  1. Q  L  R:Q\ \ L\ \ R:詢問L,RL,R之間有多少個不同的數
  2. R  P  Col:R\ \ P\ \ Col:PP修改爲ColCol

解題思路

莫隊中多加一個時間維度tt表示前面有多少個修改操作,然後莫隊即可。


codecode

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=143333;
struct que_node{
	int id,t,l,r;
}que[N];
struct cha_node{
	int pos,val,pre;
}cha[N];
int n,m,c[N],ans[N],answer,cnt,tot,T;
int v[1000010];
bool operator<(que_node x,que_node y){
	if(x.l/T!=y.l/T)return x.l/T<y.l/T;
	if(x.r/T!=y.r/T)return x.r/T<y.r/T;
	return x.t<y.t;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&c[i]);
	for(int i=1;i<=m;i++){
		char op[3];int x,y;
		scanf("%s%d%d",op,&x,&y);
		if(op[0]=='Q'){
			if(x>y)swap(x,y);
			que[++cnt]=(que_node){cnt,tot,x,y};
		}
		else{
			cha[++tot]=(cha_node){x,y,c[x]};
			c[x]=y;
		}
	}
	T=ceil(exp((log(n)+log(tot))/3));
	for(int i=tot;i>=1;i--)
		c[cha[i].pos]=cha[i].pre;
	sort(que+1,que+1+cnt);
	int l=1,r=0,t=0;
	for(int i=1;i<=cnt;i++){
		while(que[i].l<l)
			answer+=!v[c[--l]]++;
		while(que[i].l>l)
			answer-=!--v[c[l++]];
		while(que[i].r>r)
			answer+=!v[c[++r]]++;
		while(que[i].r<r)
			answer-=!--v[c[r--]];
		while(que[i].t<t){
			int pos=cha[t].pos;
			if(l<=pos&&pos<=r)answer-=!--v[c[pos]];
			c[pos]=cha[t--].pre;
			if(l<=pos&&pos<=r)answer+=!v[c[pos]]++;
		}
		while(que[i].t>t){
			int pos=cha[++t].pos;
			if(l<=pos&&pos<=r)answer-=!--v[c[pos]];
			c[pos]=cha[t].val;
			if(l<=pos&&pos<=r)answer+=!v[c[pos]]++;
		}
		ans[que[i].id]=answer;
	}
	for(int i=1;i<=cnt;i++)
		printf("%d\n",ans[i]);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章