提交时间:2023-08-16 12:04:58
运行 ID: 98593
#include<bits/stdc++.h> using namespace std; #ifdef IAKIOI #define cin fin ifstream cin("in.txt"); #endif constexpr int N = 1e5 + 5; int n, m, a[N], ans[N], upd[N], v[N], sum[N * 2], tmp, l = 1, r = 0, t = 0, cnt, tot, ti, in[N]; map<int,int>ma; namespace bl { int op[N], l[N], r[N], x[N]; bool vis[N * 2]; void solve() { for (int i = 1; i <= m; ++i) { cin >> op[i] >> l[i] >> r[i]; if (op[i] == 1) { cin >> x[i]; if (!ma[x[i]]) { ma[x[i]] = ++cnt; } x[i] = ma[x[i]]; } } for (int c = 1; c <= m; ++c){ if (op[c] == 1) { for (int i = l[c]; i <= r[c]; ++i) { a[i] = x[c]; } } else { int ans = 0; for (int i = l[c]; i <= r[c]; ++i) { if (!vis[a[i]]) { vis[a[i]] = 1; ++ans; } } cout << ans << '\n'; for (int i = l[c]; i <= r[c]; ++i) { vis[a[i]] = 0; } } } } } struct Query { int l, r, x, t, id; }q[N]; inline void del(int x) { tmp -= (!--sum[a[x]]); } inline void add(int x) { tmp += (!sum[a[x]]++); } inline void update(int t) { if (l <= upd[t] && upd[t] <= r) { tmp -= (!--sum[a[upd[t]]]); tmp += (!sum[v[t]]++); } swap(a[upd[t]], v[t]); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int s; cin >> n >> m; s = 5000; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (!ma[a[i]]) { ma[a[i]] = ++cnt; } a[i] = ma[a[i]]; in[i] = (i + s - 1) / s; } if (n <= 5000 && m <= 5000) { bl::solve(); } else { for (int i = 1; i <= m; ++i) { int op; cin >> op; if (op == 1) { int l, r, x; cin >> l >> r >> x; if (!ma[x]) ma[x] = ++cnt; x = ma[x]; upd[++ti] = l, v[ti] = x; } else { ++tot; cin >> q[tot].l >> q[tot].r; q[tot].t = ti; q[tot].id = tot; } } sort(q + 1, q + 1 + tot, [](const Query &a, const Query &b) { if (in[a.l] != in[b.l]) return in[a.l] < in[b.l]; if (in[a.r] != in[b.r]) return in[a.r] < in[b.r]; return a.t < b.t; }); for (int i = 1; i <= m; ++i) { while (t < q[i].t) update(++t); while (t > q[i].t) update(t--); while (l > q[i].l) add(--l); while (r < q[i].r) add(++r); while (l < q[i].l) del(l++); while (r > q[i].r) del(r--); ans[q[i].id] = tmp; } for (int i = 1; i <= tot; ++i) { cout << ans[i] << '\n'; } } return 0; }