提交时间:2023-08-16 15:00:20

运行 ID: 98799

#include<bits/stdc++.h> using namespace std; const int N = 1e6+2; int n,m,ans,tim,cnt; int l,r,t; int a[N],g[N],pos[N],res[N]; int tmp[N],tot; struct opt { int p,x; }f[N]; struct sth { int l,r,t,id; }q[N]; bool cmp(sth a,sth b) { if(pos[a.l] == pos[b.l] && pos[a.r] == pos[b.r]) return a.t < b.t; if(pos[a.l] == pos[b.l]) return a.r < b.r; return a.l < b.l; } void add(int col) { if(!res[col]) ans++; res[col]++; } void del(int col) { if(res[col] == 1) ans--; res[col]--; } void mod(int p,int &col) { int pre = a[p]; if(l <= p && p <= r) { if(res[a[p]] == 1) ans--; res[a[p]]--; if(res[col] == 0) ans++; res[col]++; } a[p] = col; col = pre; } int main() { scanf("%d%d",&n,&m); for(int i = 1;i <= n;i++) { scanf("%d",&a[i]); tmp[++tot] = a[i]; } int k = pow(n,2.0/3.0); for(int i = 1;i <= n;i++) pos[i] = (i-1)/k+1; for(int i = 1;i <= m;i++) { int opt; scanf("%d",&opt); if(opt == 1) { int l,r,x; scanf("%d%d%d",&l,&r,&x); tmp[++tot] = x; for(int j = l;j <= r;j++) { tim++; f[tim].p = j; f[tim].x = x; } } else { cnt++; scanf("%d%d",&q[cnt].l,&q[cnt].r); q[cnt].t = tim; q[cnt].id = cnt; } } sort(tmp+1,tmp+tot+1); int len = unique(tmp+1,tmp+tot+1)-tmp-1; for(int i = 1;i <= n;i++) a[i] = lower_bound(tmp+1,tmp+len+1,a[i])-tmp; for(int i = 1;i <= tim;i++) f[i].x = lower_bound(tmp+1,tmp+len+1,f[i].x)-tmp; sort(q+1,q+cnt+1,cmp); l = 1; r = 0; t = 0; for(int i = 1;i <= cnt;i++) { while(l < q[i].l) del(a[l++]); while(l > q[i].l) add(a[--l]); while(r < q[i].r) add(a[++r]); while(r > q[i].r) del(a[r--]); while(t < q[i].t) { t++; mod(f[t].p,f[t].x); } while(t > q[i].t) { mod(f[t].p,f[t].x); t--; } g[q[i].id] = ans; } for(int i = 1;i <= cnt;i++) printf("%d\n",g[i]); return 0; }