#include<bits/stdc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;constintINF=2e18;constintmaxn=1e6+5;constintM=1e9+7;structQuery{intl,r,t,l_block,r_block,qid;booloperator<(constQuery&rhs)const{if(l_block==rhs.l_block){if(r_block==rhs.r_block){returnt<rhs.t;}else{returnr_block<rhs.r_block;}}returnl_block<rhs.l_block;}};intn,q;intans[maxn];vector<Query>query;structDS{staticconstintN=1e6+5;intans=0;vector<int>cnt;vector<int>a;vector<pii>updates;stack<pii>stk;DS(vector<int>b){cnt.resize(N);a=b;}voidadd_event(intidx,intval){updates.pb({idx,val});}voidadd(intx){x=a[x];if(cnt[x]==0){ans++;}cnt[x]++;}voiddel(intx){x=a[x];cnt[x]--;assert(cnt[x]>=0);if(cnt[x]==0){ans--;}}voidmodify_add(intl,intr,intt){auto[idx,val]=updates[t];if(l<=idx&&idx<=r){stk.push({idx,a[idx]});del(idx);a[idx]=val;add(idx);}else{stk.push({idx,a[idx]});a[idx]=val;}}voidmodify_del(intl,intr,intt){assert(stk.size());auto[idx,val]=stk.top();stk.pop();if(l<=idx&&idx<=r){del(idx);a[idx]=val;add(idx);}else{a[idx]=val;}}};signedmain(){ios::sync_with_stdio(0);cin.tie(0);cin>>n>>q;intk=pow(n,(double)2/(double)3);vector<int>a(n);for(inti=0;i<n;i++){cin>>a[i];}DSds(a);intuid=-1,qid=-1;for(inti=0;i<q;i++){charc;cin>>c;if(c=='R'){intidx,val;cin>>idx>>val;idx--;uid++;ds.add_event(idx,val);}elseif(c=='Q'){intl,r;cin>>l>>r;l--,r--;qid++;query.pb({l,r,uid,l/k,r/k,qid});}}sort(ALL(query));intl=0,r=-1,t=-1;for(auto[ql,qr,qt,l_block,r_block,qid]:query){while(ql<l)ds.add(--l);while(r<qr)ds.add(++r);while(l<ql)ds.del(l++);while(qr<r)ds.del(r--);while(t<qt)ds.modify_add(ql,qr,++t);while(t>qt)ds.modify_del(ql,qr,t--);ans[qid]=ds.ans;}for(inti=0;i<=qid;i++){cout<<ans[i]<<'\n';}}
#include<bits/stdc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;constintINF=2e18;constintmaxn=3e5+5;constintM=1e9+7;structEdge{intu,v;};structDSU{DSU(intn):n(n){sz=vector<int>(n,1);par=vector<int>(n);cnt=n;for(inti=0;i<n;i++){par[i]=i;}}voidmerge(Edgee){intx=find(e.u),y=find(e.v);if(x==y){stk.push({x,x});return;}if(sz[x]<sz[y])swap(x,y);sz[x]+=sz[y];par[y]=x;cnt--;stk.push({x,y});}voidundo(){assert(stk.size());auto[x,y]=stk.top();stk.pop();if(x==y)return;sz[x]-=sz[y];par[y]=y;cnt++;}intcc(){returncnt;}private:intn,cnt;vector<int>sz;vector<int>par;stack<pii>stk;intfind(intx){if(par[x]==x)returnx;elsereturnfind(par[x]);}};structQuery{intl,r,l_block,r_block,qid;booloperator<(constQuery&rhs)const{if(l_block==rhs.l_block){returnr<rhs.r;}returnl_block<rhs.l_block;}};intn,m,q,k;vector<Edge>edges;vector<Query>query;intL[maxn],R[maxn],ans[maxn];voidinit(){cin>>n>>m;for(inti=0;i<m;i++){intu,v;cin>>u>>v;u--,v--;edges.pb({u,v});}k=sqrt(m);cin>>q;intl,r;for(inti=0;i<q;i++){cin>>l>>r;l--,r--;query.pb({l,r,l/k,r/k,i});}sort(ALL(query));inttot=m/k;for(inti=0;i<tot;i++){L[i]=i*k;R[i]=(i+1)*k-1;}if(R[tot-1]<m-1){tot++;L[tot-1]=R[tot-2]+1;R[tot-1]=m-1;}}voidsolve(){DSUdsu(n);intl=-1,r=m,last_block=-1,r_cnt=0;for(auto[ql,qr,l_block,r_block,qid]:query){if(last_block<l_block){while(r_cnt>0){dsu.undo();r_cnt--;}l=R[l_block]+1;r=R[l_block];last_block=l_block;}if(l_block==r_block){for(inti=ql;i<=qr;i++){dsu.merge(edges[i]);}ans[qid]=dsu.cc();for(inti=ql;i<=qr;i++){dsu.undo();}}else{while(r<qr){dsu.merge(edges[++r]);r_cnt++;}intl_cnt=0;while(l>ql){dsu.merge(edges[--l]);l_cnt++;}ans[qid]=dsu.cc();while(l_cnt>0){dsu.undo();l_cnt--;}l=R[l_block]+1;}}for(inti=0;i<q;i++){cout<<ans[i]<<'\n';}}signedmain(){init();solve();}
#include<bits/stdc++.h>usingnamespacestd;constintMAXN=40005;constintMAXM=100005;constintLN=19;intN,M,K,cur,A[MAXN],LVL[MAXN],DP[LN][MAXN];intBL[MAXN<<1],ID[MAXN<<1],VAL[MAXN],ANS[MAXM];intd[MAXN],l[MAXN],r[MAXN];boolVIS[MAXN];vector<int>adjList[MAXN];structquery{intid,l,r,lc;booloperator<(constquery&rhs){return(BL[l]==BL[rhs.l])?(r<rhs.r):(BL[l]<BL[rhs.l]);}}Q[MAXM];// Set up Stuffvoiddfs(intu,intpar){l[u]=++cur;ID[cur]=u;for(inti=1;i<LN;i++)DP[i][u]=DP[i-1][DP[i-1][u]];for(inti=0;i<adjList[u].size();i++){intv=adjList[u][i];if(v==par)continue;LVL[v]=LVL[u]+1;DP[0][v]=u;dfs(v,u);}r[u]=++cur;ID[cur]=u;}// Function returns lca of (u) and (v)inlineintlca(intu,intv){if(LVL[u]>LVL[v])swap(u,v);for(inti=LN-1;i>=0;i--)if(LVL[v]-(1<<i)>=LVL[u])v=DP[i][v];if(u==v)returnu;for(inti=LN-1;i>=0;i--){if(DP[i][u]!=DP[i][v]){u=DP[i][u];v=DP[i][v];}}returnDP[0][u];}inlinevoidcheck(intx,int&res){// If (x) occurs twice, then don't consider it's value if((VIS[x])and(--VAL[A[x]]==0))res--;elseif((!VIS[x])and(VAL[A[x]]++==0))res++;VIS[x]^=1;}voidcompute(){// Perform standard Mo's AlgorithmintcurL=Q[0].l,curR=Q[0].l-1,res=0;for(inti=0;i<M;i++){while(curL<Q[i].l)check(ID[curL++],res);while(curL>Q[i].l)check(ID[--curL],res);while(curR<Q[i].r)check(ID[++curR],res);while(curR>Q[i].r)check(ID[curR--],res);intu=ID[curL],v=ID[curR];// Case 2if(Q[i].lc!=uandQ[i].lc!=v)check(Q[i].lc,res);ANS[Q[i].id]=res;if(Q[i].lc!=uandQ[i].lc!=v)check(Q[i].lc,res);}for(inti=0;i<M;i++)printf("%d\n",ANS[i]);}intmain(){intu,v,x;while(scanf("%d %d",&N,&M)!=EOF){// Cleanupcur=0;memset(VIS,0,sizeof(VIS));memset(VAL,0,sizeof(VAL));for(inti=1;i<=N;i++)adjList[i].clear();// Inputting Valuesfor(inti=1;i<=N;i++)scanf("%d",&A[i]);memcpy(d+1,A+1,sizeof(int)*N);// Compressing Coordinatessort(d+1,d+N+1);K=unique(d+1,d+N+1)-d-1;for(inti=1;i<=N;i++)A[i]=lower_bound(d+1,d+K+1,A[i])-d;// Inputting Treefor(inti=1;i<N;i++){scanf("%d %d",&u,&v);adjList[u].push_back(v);adjList[v].push_back(u);}// PreprocessDP[0][1]=1;dfs(1,-1);intsize=sqrt(cur);for(inti=1;i<=cur;i++)BL[i]=(i-1)/size+1;for(inti=0;i<M;i++){scanf("%d %d",&u,&v);Q[i].lc=lca(u,v);if(l[u]>l[v])swap(u,v);if(Q[i].lc==u)Q[i].l=l[u],Q[i].r=l[v];elseQ[i].l=r[u],Q[i].r=l[v];Q[i].id=i;}sort(Q,Q+M);compute();}}