CodeForces - 705C Thor (数组模拟队列)
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
- Application x generates a notification (this new notification is unread).
- Thor reads all notifications generated so far by application x (he may re-read some notifications).
- Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Print the number of unread notifications after each event.
3 4
1 3
1 1
1 2
2 3
1
2
3
2
4 6
1 2
1 4
1 2
3 3
1 3
1 3
1
2
3
0
1
2
In the first sample:
- Application 3 generates a notification (there is 1 unread notification).
- Application 1 generates a notification (there are 2 unread notifications).
- Application 2 generates a notification (there are 3 unread notifications).
- Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
- Application 2 generates a notification (there is 1 unread notification).
- Application 4 generates a notification (there are 2 unread notifications).
- Application 2 generates a notification (there are 3 unread notifications).
- Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
- Application 3 generates a notification (there is 1 unread notification).
- Application 3 generates a notification (there are 2 unread notifications).
分析:
共有三种操作,而删除操作有两种,所以我们需要保证两种删除操作的进行不冲突
第二种删除操作是将前面n个数删除,那么我们就可以当作队列中前n个数出队,其中要判断前n个数中已经被第一种删除操作删除的
所以我们用数组来记录应用p的有效值的区域,比如zuo[p]=r代表r以前的是已经被删除了,面对已经被删除了的,就不进行操作了。
而进行第一种删除操作的时候,我们只需要对队列中的进行操作,且不会产生冲突。
代码如下:
#include <cstdio> #include <iostream> #include <cstring> using namespace std; typedef long long ll; const int MAXN=3e5+10; int zuo[MAXN]; int num[MAXN]; int Q[MAXN]; int main() { int n,q,l,r,a,b,now; while(scanf("%d%d",&n,&q)!=EOF) { now=0; memset(zuo,0,sizeof(zuo)); memset(num,0,sizeof(num)); l=0,r=0; while(q--) { scanf("%d%d",&a,&b); if(a==1) { Q[r++]=b; now++; num[b]++; } else if(a==2) { now-=num[b]; zuo[b]=r; num[b]=0; } else { if(l<b){ for(int i=l;i<b;i++) { if(i<zuo[Q[i]]); else { now--; num[Q[i]]--; } } l=b; } } printf("%d\n",now); } } return 0; }