Codeforces Round #366 (Div. 2) C. Thor

C. Thor
time limit per test
2 seconds
memory limit per test
256 megabytes

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:

**1.Application x generates a notification (this new notification is unread).
2.Thor reads all notifications generated so far by application x (he may re-read some notifications).
3.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.
Input

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).
Output

Print the number of unread notifications after each event.
Examples
Input

3 4
1 3
1 1
1 2
2 3

Output

1
2
3
2

Input

4 6
1 2
1 4
1 2
3 3
1 3
1 3

Output

1
2
3
0
1
2

【题意】 手机里有n个不同的应用,每个应用可能会产生通知。现在有q个查询,如下种类:
1:x号应用产生了一个新的通知
2:阅读所有x已产生的通知
3:阅读前t个产生的通知(任何种类的应用产生的都算)
问每次查询后还有未读的通知多少?

【解题思路】 若光用一个数组标记x应用所产生的通知,再遍历的话会超时。所以我们用一个队列s将所有产生通知的记录都压入队列,再用两个标记数组read[x]和sum[x]来表示x应用已读的通知和所有的通知。
操作1时,将x压入队列,同时ans(未读通知总数)和unread[x]都加一;
操作2时,将ans减去sum[x]-read[x],同时read[x]的值更新为sum[x]的值;
操作3时,设置一个已出队数out和标记数组vis[x],直接将队列中前(t-out)的记录弹出,vis[x]++,若弹出的通知已读,则不做改动;否则,ans减一,read[x]加一;

【AC代码】

#include<bits/stdc++.h>
#define fff 300005
using namespace std;
int sum[fff],read[fff],vis[fff];
int main()
{
    int n,q,a,b,out=0,ans=0;
    scanf("%d%d",&n,&q);
    queue<int>s;
    while(q--)
    {
        scanf("%d%d",&a,&b);
        if(a==1)
        {
            sum[b]++;
            ans++;
            s.push(b);
        }
        if(a==2)
        {
            ans-=sum[b]-read[b]; //ans减去的是**当前**b应用还未阅读的通知
            read[b]=sum[b];  //不要忘记更新已读通知的信息
        }
        if(a==3)
        {
            int t=b-out; //out是已弹出队列(就是已经阅读过了)的通知数量,则t是前b条通知中还在队列中(还未读的)的通知数
            while(!s.empty()&&t>0)
            {
                t--;
                int x=s.front();
                s.pop();
                out++;
                vis[x]++;
                if(vis[x]>read[x]) //通知未读的情况
                {
                    ans--;
                    read[x]++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值