【Codeforces Round #366 Div. 2】 705C Thor

Description
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.


【题目大意】
题意:手机里有n个不同的应用,每个应用不时的会产生通知消息。现在有q次模拟:
1:应用x产生了一个新的通知
2:阅读x产生的所有通知
3:阅读前t个产生的通知(任何应用产生的都算)
问每次查询后未读的通知有多少?


【题目分析】
暴力的每次去模拟肯定会挂。发现复杂度主要来源于第三个操作,那么我们可以放一个指针,用来指向当前3操作最远执行过的距离,小于这个值就是0.大于这个值在进行统计。然后发现,这样会与操作2冲突,我们再用一个数组记录一下每一个应用读取到的最大的模拟数,只有大于这个值的模拟才是有效的。
代码就很难写,有许多细节需要注意。


【代码】

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int me[500001],list[500001],l[500001],ans=0,n,m,now=0,top=0;
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i)
    {
        int op,tmp;
        scanf("%d%d",&op,&tmp);
        if (op==1) {me[tmp]++,ans++;list[++top]=tmp;}
        if (op==2) {ans-=me[tmp],me[tmp]=0;l[tmp]=top;}
        if (op==3)
        {
            int be=now+1;
            for (int i=be;i<=tmp&&i<=top;++i,now++)
            {
                int x=list[i];
                if (me[x]&&l[x]<i) {me[x]--;ans--;}
            }
        }
        printf("%d\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值