一开始理解错了第三个的类型的意思,
手机上有n个应用,q个事件发生。
类型1:应用发生了一个通知
类型2:使用者读完了到目前为止应用x的发出的所有通知
类型3:使用者按照时间先后,读取t个通知
输出每次事件发生后未读通知的数量
#include<bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd
#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)
const int maxn = 3e6+10;
const int inf = 1 << 28;
map<int,set<int> > mp;
bool vis[maxn];
vector<pair<int,int> > v;
int main(){
int n,q;
scanf("%d%d",&n,&q);
int tot = 0;
int last = 0;
int tim = 0;
for(int i=1;i<=q;i++){
int type,x;
scanf("%d%d",&type,&x);
if(type==1){
tot++;tim++;
mp[x].insert(tim);
v.pb(make_pair(tim,x));
}
else if(type==2){
set<int>::iterator it=mp[x].begin();
while(it!=mp[x].end()){
vis[*it]=1;
tot--;
it++;
}
mp[x].clear();
}
else {
if(x>last) {
for(int i=last;i<x;i++)if(!vis[v[i].first]){
vis[v[i].first]=true;
tot--;
set<int>::iterator it = mp[v[i].second].begin();
mp[v[i].second].erase(*it);
}
last = x;
}
}
printf("%d\n",tot);
}
return 0;
}
使用队列模拟,更加清晰
#include<bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd
#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)
const int maxn = 3e5+10;
const int inf = 1 << 28;
queue<int> app[maxn];
queue<pair<int,int> > all;
bool mark[maxn];
int main(){
int n,q;
scanf("%d%d",&n,&q);
int tot=0,cur=0;
for(int i=1;i<=q;i++){
int type,x;
scanf("%d%d",&type,&x);
if(type==1){
tot++;cur++;
all.push(make_pair(tot,x));
app[x].push(tot);
}
else if(type==2){
while(!app[x].empty()){
int t = app[x].front();app[x].pop();
mark[t]=1;
cur--;
}
}
else {
while(!all.empty()&&all.front().first<=x){
int t = all.front().first;
int y = all.front().second;
all.pop();
if(mark[t]==false){
mark[t]=true;
cur--;app[y].pop();
}
}
}
printf("%d\n",cur);
}
return 0;
}