The block size is N1/3.
Separate the inquiries from the changes.
Each time the change between two queries is violently transferred, if the change is in the interval of the previous query, it will affect the current state.
So slow.
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std;# Define N 10001int num[n],n,m,b[n],a[n],enq,enc,anss[n];struct ask{int l,r,p,t;} Q[n];bool operator < (const ask &a,const ask &b) {if (NUM[A.L]==NUM[B.L]) {if (NUM[A.R]==NUM[B.R]) return a.t& Lt;b.t;return num[a.r]<num[b.r]; }return NUM[A.L]<NUM[B.L];} struct Upt{int x, y, Z;} Ch[1001];int ans,t[1000001];void Insert (const int &x) {if (! T[X]) ++ans;++t[x];} void Delete (const int &x) {--t[x];if (! T[X])--ans;} int main () {char op[2];scanf ("%d%d", &n,&m), int sz= (int) Pow ((double) n,1.0/3.0), Blo=0;if (!SZ) sz=1;for (int i=1 ; i<=n;++i) {scanf ("%d", &a[i]); if (i%sz==1| | Sz==1) ++blo; Num[i]=blo; }memcpy (B,a, (n+1) *sizeof (int)), for (int i=1;i<=m;++i) {scanf ("%s", op); if (op[0]== ' Q ') {++enq; scanf ("%d%d", &Q[ENQ].L,&Q[ENQ].R); Q[enq].p=enq; Q[enq].t=enc; }else {++enc; scanf ("%d%d", &ch[enc].x,&ch[enc].y); Ch[enc].z=b[ch[enc].x];B[CH[ENC].X]=CH[ENC].Y; }}sort (Q+1,q+enq+1), for (int i=1;i<=q[1].t;++i) a[ch[i].x]=ch[i].y;for (int i=q[1].l;i<=q[1].r;++i) Insert (a[i ]); Anss[q[1].p]=ans; for (int i=2;i<=enq;++i) {if (q[i-1].t<q[i].t) for (int j=q[i-1].t+1;j<=q[i].t;++j) {if (ch[j].x> ; =q[i-1].l&&ch[j].x<=q[i-1].r) {Insert (CH[J].Y); Delete (a[ch[j].x]); } a[ch[j].x]=ch[j].y; } else for (int j=q[i-1].t;j>q[i].t;--j) {if (CH[J].X>=Q[I-1].L&&CH[J].X<=Q[I-1].R) {Ins ERT (CH[J].Z); Delete (a[ch[j].x]); } a[ch[j].x]=ch[j].z; } if (Q[I].L<Q[I-1].L) for (int j=q[i-1].l-1;j>=q[i].l;--j) Insert (A[j]); else for (int j=q[i-1].l;j<q[i].l;++j) Delete (A[j]); if (Q[I].R<Q[I-1].R) for (int j=q[i-1].r;j>q[i].r;--j) Delete (A[j]); else for (int j=q[i-1].r+1;j<=q[i].r;++j) Insert (A[j]); Anss[q[i].p]=ans; } for (int i=1;i<=enq;++i) printf ("%d\n", Anss[i]); return 0;}
With bzoj2120 number of colors