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 = 2then 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).
题目大意:
给你三种操作:
1 x:表示用x号机器生产一条消息。
2 x:表示x号机器生产的消息全部读完(当然有可能重复这样一个操作)。
3 x:表示前x个生产的消息全部读完(当然也有可能重复这样一个操作)。
每一次操作输出还有多少消息没有读。
思路:
1、首先,我们设定数组:sum【i】=aa表示i号机器还有aa个信息没有读。那么对应操作1:sum【x】++;output++;对应操作2:output-=sum【x】;sum【x】=0;
那么第三个操作呢?暴力的话是就是每一次都从第一个消息扫到最后一个消息。最坏的情况会接近O(N^2)
2、那么这个题的问题就是在如何降低第三种操作的复杂度。我们这样来想,如果之前有了这样的操作请求:
3 3
那么如果现在再有一个这样的操作请求:
3 4
如果我们两次都:从1扫到了3,从1扫到了4这样来操作,显然有重叠的部分,我们可以维护一个值:pre,表示前pre个信息已经有过操作3的请求了,那么当前请求如果小于等于pre,那么其实就不用操作了,否则就从pre一直操作到当前请求操作到的位子即可。那么就是在说无论多少次操作3的请求,我们对所有的操作3的请求累加在一起,最多也就N次操作。那么明显这么做之后,不会超时。
3、那么还有另外一个问题,操作2和操作3会有重叠操作的部分,我们要去重,我们不妨对应数组标记一下,设定一个:vis【i】表示第i条信息是否被读过了。然后我们就能通过标记来进行正确无重叠的操作。
4、具体实现,剩下的就是代码了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
queue<int >s[300015];//队列并没有什么特殊的实现意义,只是单纯的用来储存需要处理的信息的
int p[300015][2];
int vis[300015];
int sum[300015];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int pre=0;
memset(vis,0,sizeof(vis));
memset(sum,0,sizeof(sum));
int output=0;
int contz=1;
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x==1)
{
sum[y]++;
output++;
s[y].push(contz);
}
if(x==2)
{
output-=sum[y];
sum[y]=0;
while(!s[y].empty())
{
//printf("vis:%d\n",s[y].front());
vis[s[y].front()]=1;
s[y].pop();
}
}
if(x==3)
{
if(y>pre)
{
if(pre==0)pre=1;
for(int j=pre;j<=y;j++)
{
if(vis[j]==1)continue;
else
{
vis[j]=1;
//printf("fuck:%d\n",j);
sum[p[j][1]]--;
output--;
s[p[j][1]].pop();
}
}
pre=y;
}
}
if(x==1)
{
p[contz][0]=x;
p[contz++][1]=y;
}
printf("%d\n",output);
}
}
}