正题
题目链接:https://www.luogu.com.cn/problem/P1903
题目大意
要求支持两个操作
- QLR:Q\ \ L\ \ R:Q L R:询问L,RL,RL,R之间有多少个不同的数
- RPCol:R\ \ P\ \ Col:R P Col:将PPP修改为ColColCol
解题思路
莫队中多加一个时间维度ttt表示前面有多少个修改操作,然后莫队即可。
codecodecode
#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;
}