提交时间:2023-08-16 13:05:28
运行 ID: 98723
#include<bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m, ti, len; int a[N], tmp[N], tong[N]; struct node { int op, l, r, x, tm, id; } t[N]; int l = 1, r, now; int sz = 4000, tot, CNT; int pre[N], nxt[N], pos[N], ans[N]; bool cmp(node x, node y) {return (x.l / sz < y.l / sz ? 1 : x.r < y.r ? 1 : x.tm < y.tm);} inline void del(int x) { tong[a[x]]--; if(!tong[a[x]]) CNT--; } inline void add(int x) { if(!tong[a[x]]) CNT++; tong[a[x]]++; } inline void addt(int time) { int x = pos[time]; if(x >= l && x <= r) { tong[a[x]]--; if(!tong[a[x]]) CNT--; if(!tong[nxt[time]]) CNT++; tong[nxt[time]]++; } a[x] = nxt[time]; } inline void delt(int time) { int x = pos[time]; if(x >= l && x <= r) { tong[a[x]]--; if(!tong[a[x]]) CNT--; if(!tong[pre[time]]) CNT++; tong[pre[time]]++; } a[x] = pre[time]; } int main() { // freopen("1.in", "r", stdin); // freopen("1.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++) cin >> a[i], tmp[i] = a[i], tong[++len] = a[i]; for(int i = 1; i <= m; i++) { cin >> t[i].op >> t[i].l >> t[i].r; if(t[i].op == 1) { ti++; pre[ti] = tmp[i]; cin >> t[i].x; pos[ti] = t[i].l; tmp[i] = t[i].x; tong[++len] = t[i].x; nxt[ti] = tmp[i]; } else t[i].tm = ti, t[i].id = ++tot; } sort(tong + 1, tong + len + 1); len = unique(tong + 1, tong + len + 1) - tong - 1; for(int i = 1; i <= n; i++) a[i] = lower_bound(tong + 1, tong + len + 1, a[i]) - tong; for(int i = 1; i <= m; i++) if(t[i].x) t[i].x = lower_bound(tong + 1, tong + len + 1, t[i].x) - tong; for(int i = 1; i <= len; i++) tong[i] = 0; if(n <= 5000 && m <= 5000) { for(int i = 1; i <= m; i++) { for(int j = 1; j <= len; j++) tong[j] = 0; if(t[i].op == 1) for(int j = t[i].l; j <= t[i].r; j++) a[j] = t[i].x; else { int res = 0; for(int j = t[i].l; j <= t[i].r; j++) if(!tong[a[j]]) res++, tong[a[j]]++; cout << res << '\n'; } } return 0; } sort(t + 1, t + m + 1, cmp); for(int i = 1; i <= m; i++) { if(t[i].op == 1) continue; while(now < t[i].tm) addt(++now); while(now > t[i].tm) delt(now--); while(l < t[i].l) del(l++); while(l > t[i].l) add(--l); while(r < t[i].r) add(++r); while(r > t[i].r) del(r--); ans[t[i].id] = CNT; } for(int i = 1; i <= tot; i++) cout << ans[i] << '\n'; return 0; }