操作3:全局指针p记录已经扫描过的最大编号,如果 x > p,就处理区间 (p, x]。对区间里每条消息i:如果 last[bel[i]] < i,则 cnt[bel[i]]--、ans--,并更新 last[bel[i]] = i;循环结束后 p = x
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
const int N = 300005;
int app[N], cnt[N], last[N], bel[N];
signed main() {
int n, q, idx = 0, ans = 0, p = 0;
cin >> n >> q;
while (q--) {
int op, x;
cin >> op >> x;
if (op == 1) cnt[x]++, idx++, bel[idx] = x;
else if (op == 2) ans -= cnt[x], cnt[x] = 0, last[x] = idx;
else while (p < x) {
p++;
int k = bel[p];
if (p > last[k]) ans--, last[k] = p, cnt[k]--;
}
cout << ans << "\n";
}
return 0;
}