[CF 705C]Thor
这题我考试的时候想出各种蜜汁数据结构。
但是正解是:暴力!
好了好了,暴力也是要优化的。首先给消息编号,然后用一个bitset记下来哪个删了哪个没删,这个都能想到。
不过接下来有个挺头疼的问题:操作3阅读的信息可能在操作2中会被再次阅读,如此一来效率就很低了……咋整?
注意大消息队列里的消息编号肯定是升序的,每个程序的消息队列的编号也是升序的,所以说大队列中同一程序的消息的编号和该程序自身队列中的消息编号的顺序是一致的。如此一来,在操作3中可以顺便把程序队列中的消息也删掉,效率大为提升。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | # include <cstdio> # include <queue> # include <utility> # include <cctype> # include <bitset> using namespace std; const int maxn= 300001 ; typedef pair< int , int > pii; bitset<maxn> vis; queue< int > S[maxn]; queue<pii> Q; // I/O优化 inline int readint(){ char c=getchar(); register int x= 0 ; while (!isdigit(c)) c=getchar(); while (isdigit(c)){ x=x* 10 +c- '0' ; c=getchar(); } return x; } int bf[ 10 ]; inline void writeint( int x){ register int p= 0 ; if (x== 0 ){ bf[p++]= 0 ; } else { while (x){ bf[p++]=x% 10 ; x/= 10 ; } } for (register int i=p- 1 ;i>= 0 ;i--) putchar( '0' +bf[i]); } int main(){ register int ans= 0 ,cnt= 0 ; register int temp,temp_2,q,u,v; int n; n=readint(); q=readint(); while (q--){ u=readint(); v=readint(); if (u== 1 ){ cnt++; S[v].push(cnt); Q.push(make_pair(v,cnt)); ans++; } else { if (u== 2 ){ while (!S[v].empty()){ temp=S[v].front(); S[v].pop(); if (!vis[temp]){ vis[temp]= true ; ans--; } } } else { while (!Q.empty() && (temp=Q.front().second)<=v){ temp=Q.front().second; temp_2=Q.front().first; Q.pop(); if (!vis[temp]){ vis[temp]= true ; S[temp_2].pop(); ans--; } } } } writeint(ans); putchar( '\n' ); } return 0 ; } |