zoukankan      html  css  js  c++  java
  • codeforces 705C 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


    题意:

    现在给n个应用,三种操作,第一种是一个应用产生一个消息,第二个是读完这种应产生的所有的消息,第三个是读完前t个消息;

    思路:

    用队列模拟一下;

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=3e5+10;
    const int maxn=2e3+14;
    const double eps=1e-12;
    
    int n,q,x,type,num[N];
    int a[N],vis[N];
    queue<int>qu[N];
    int main()
    {
        read(n);read(q);
        int sum=0,pre=0,cnt=0;
        For(i,1,q)
        {
            read(type);read(x);
            if(type==1)
            {
                num[x]++;
                sum++;
                a[++cnt]=x;
                qu[x].push(cnt);
            }
            else if(type==2) 
            {
                while(!qu[x].empty())
                {
                    int fr=qu[x].front();
                    qu[x].pop();
                    vis[fr]=1;
                }
                sum-=num[x];
                num[x]=0;
            }
            else 
            {
                for(int i=pre+1;i<=x;i++)
                {
                    if(vis[i])continue;
                    int temp=a[i];
                    qu[temp].pop();
                    num[temp]--;
                    sum--;
                    vis[i]=1;
                }
                pre=max(pre,x);
            }
            printf("%d
    ",sum);
        }
    
    
        return 0;
    }
    

      

  • 相关阅读:
    Sharding-Jdbc 自定义分库分表-复合分片算法自定义实现
    sklearn:Python语言开发的通用机器学习库
    php验证码--图片
    ListView中的Item点击事件和子控件的冲突或者item点击没有反应的解决的方法
    【转载】C# Graphics类具体解释
    Oracle之外键(Foreign Key)使用方法具体解释(二)- 级联删除(DELETE CASCADE)
    职业生涯-小公司和大公司的不同(持续更新)
    视音频数据处理入门:AAC音频码流解析
    让人非常easy误解的TCP拥塞控制算法
    Redis资料整理
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5751376.html
Copyright © 2011-2022 走看看