CodeForces 705C Thor
开30W个vector将数字归类,每一类数字开一个指针P,记录已经阅读到哪一个了,还可以开一个优先队列维护这些指针P。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-8; void File() { freopen("D:\\in.txt","r",stdin); freopen("D:\\out.txt","w",stdout); } inline int read() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x; } const int maxn=300000+10; vector<int>g[maxn]; int n,q,sz,p[maxn],ans; struct X { int num,now,pos,idx; bool operator < (const X &a) const { return idx>a.idx; } X(int Num,int Now,int Pos,int Idx) {num=Num; now=Now; pos=Pos,idx=Idx;} }; priority_queue<X>Q; int main() { scanf("%d%d",&n,&q); sz=0; ans=0; memset(p,-1,sizeof p); for(int i=1;i<=q;i++) { int t,d; scanf("%d%d",&t,&d); if(t==1) { ++sz; if(p[d]==g[d].size()-1) { Q.push(X(d,p[d],p[d]+1,sz)); }g[d].push_back(sz); ans++; } else if(t==2) { ans=ans-(g[d].size()-1-p[d]); p[d]=g[d].size()-1; } else { while(!Q.empty()&&Q.top().idx<=d) { X h=Q.top(); Q.pop(); if(h.now<p[h.num]) continue; ans=ans-(h.pos-p[h.num]); p[h.num]=h.pos; if(p[h.num]+1<g[h.num].size()) Q.push(X(h.num,p[h.num],p[h.num]+1,g[h.num][p[h.num]+1])); } } printf("%d\n",ans); } return 0; }