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

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

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

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
Note

In the first sample:

  1. Application 3 generates a notification (there is 1 unread notification).
  2. Application 1 generates a notification (there are 2 unread notifications).
  3. Application 2 generates a notification (there are 3 unread notifications).
  4. Thor reads the notification generated by application 3, there are 2 unread notifications left.

In the second sample test:

  1. Application 2 generates a notification (there is 1 unread notification).
  2. Application 4 generates a notification (there are 2 unread notifications).
  3. Application 2 generates a notification (there are 3 unread notifications).
  4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
  5. Application 3 generates a notification (there is 1 unread notification).
  6. Application 3 generates a notification (there are 2 unread notifications).

 

题意:手机里有n个不同的应用,每个应用不时的会产生通知消息。现在有q次模拟:
        1:应用x产生了一个新的通知
   2:阅读x产生的所有通知
   3:阅读前t个产生的通知(任何应用产生的都算)
    问每次查询后未读的通知有多少?
 
思路:用一个数组存放所有的通知,然后遍历数组进行查询 复杂度 n^2,会超时。
那么就用needread数组记录各个应用还有多少条消息未读,readed数组记录各个应用已经被读的消息数目。
对于1对needread数组加一。
对于2要把总数减去needread数组的值,但是因为有些消息可能之前已经被读过了,所以要减的应该是needread数组减readed数组的值。
对于3我们需要按消息产生的顺序来处理,因此我们设置一个队列,每次1产生新消息时候将消息入队。每次要处理前t个消息,那么我们从队列中一个个出列然后处理消息就可以了。需要注意的是,不是每次都要处理队列中的前t个消息,因为我们前面处理完一些消息后前(t-pop_cnt)个消息已经出队了,处理过了。这就是设置一个pop_cnt变量记录已出队消息数量的原因。当处理到某个应用的消息数大于了这个应用已经处理的消息数后,接下来的消息就是未读的消息了,此时处理未读消息,更新记录。
 
 1 #include<cstdio>  
 2 #include<queue>  
 3 using namespace std;  
 4 const int maxn = 300005;  
 5 int needread[maxn];  
 6 int readed[maxn];  
 7 int cnt[maxn];  
 8 int main()  
 9 {  
10     int n,q,type,num,pop_cnt = 0;  
11     queue<int> que;  
12     scanf("%d %d", &n, &q);  
13     int ans = 0;  
14     while (q--)  
15     {  
16         scanf("%d %d", &type, &num);  
17         if (type == 1)  
18         {  
19             ans++;  
20             needread[num]++;  
21             que.push(num);
22         }  
23         else if (type == 2)  
24         {  
25             ans -= needread[num] - readed[num];  
26             readed[num] = needread[num];  
27         }  
28         else  
29         {  
30             int t = num - pop_cnt;
31             while (!que.empty() && t > 0)  
32             {  
33                 t--;  
34                 int cur = que.front();  
35                 que.pop();  
36                 pop_cnt++;  
37                 cnt[cur]++;  
38                 if (cnt[cur] > readed[cur]) 
39                 {  
40                     ans --;  
41                     readed[cur] ++;  
42                 }  
43             }  
44         }  
45         printf("%d\n",ans);  
46     }  
47     return 0;  
48 }

 

转载于:https://www.cnblogs.com/Mino521/p/5751397.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值