Codeforces 704 A. Thor(队列,queue)

题目链接:http://codeforces.com/problemset/problem/704/A

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
Copy
3 4
1 3
1 1
1 2
2 3
output
1
2
3
2
input
Copy
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次询问,每次询问(x,y),x对应3种操作1、2、3,1:y应用新增一条消息,2:读掉y应用的所有消息,3:读前y条消息(是前y条,不是未读的前y条)

思路:一个队列模拟,写得有点复杂,记录的比较多,不过最终终于还是过了。用a[N]记录第i条信息由谁产生,用Appcnt[N]记录第i个app读过了多少条,用Kcnt[N]记录前k条消息中第i个app有几个(注意Kcnt[N]在第三种操作的时候才更新,用记录的a[N]来更新),然后判断一下在Kcnt[a[i]]和已经出队的消息Appcnt[a[i]]来判断是否需要出队,即ans--。1,2,操作比较简单,这里就不再赘述。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 3e5+5;

queue<int>que[N];
int a[N];       //记录第i条信息由谁产生
int Appcnt[N];  //记录第i个app读过了多少条
int Kcnt[N];    //记录前k条消息中第i个app有几个
int num=0;      //记录当前产生过几条信息
int maxt=0;     //记录最大的t

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int n,q,x,y;
    MEM(Kcnt,0);
    MEM(Appcnt,0);
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
        while(!que[i].empty())
            que[i].pop();
    int ans=0;
    while(q--)
    {
        scanf("%d%d",&x,&y);
        if(x==1)
        {
            num++;
            que[y].push(num);
            a[num]=y;
            ans++;
        }
        else if(x==2)
        {
            while(!que[y].empty())
            {
                int tmp=que[y].front();
                que[y].pop();
                Appcnt[y]++;
                ans--;
            }
        }
        else
        {
            if(y>maxt)
            {
                for(int i=maxt+1;i<=y&&i<=num;i++)
                {
                    int tmp=a[i];
                    Kcnt[tmp]++;
                    if(Kcnt[tmp]>Appcnt[tmp])
                    {
                        for(int j=Appcnt[tmp]+1;j<=Kcnt[tmp];j++)
                        {
                            que[tmp].pop();
                            ans--;
                        }
                        Appcnt[tmp]=Kcnt[tmp];
                    }
                }
                maxt=min(y,num);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


看了一下网上其他大牛的博客,重新再贴一个简单的,用两个队列和一个vis数组维护,que【i】就是i容器里所有消息的时间,Q队列就是对头就是该条消息的时间和对应app,用vis【i】判断第i时间点进入的消息是否已经出过对来更新ans

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 3e5+5;

struct node
{
    int num;  //记录容器
    int time;  //记录时间
};

queue<int>que[N];
int vis[N];
queue<node>Q;

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    MEM(vis,0);
    int n,q,x,y;
    scanf("%d%d",&n,&q);
    int ans=0;
    int cnt=0;
    while(q--)
    {
        scanf("%d%d",&x,&y);
        if(x==1)
        {
            cnt++;
            que[y].push(cnt);
            node a;
            a.num = y;
            a.time = cnt;
            Q.push(a);
            ans++;
        }
        else if(x==2)
        {
            while(!que[y].empty())
            {
                int tmp=que[y].front();
                vis[tmp]=1;
                que[y].pop();
                ans--;
            }
        }
        else
        {
            while(!Q.empty()&&Q.front().time<=y)
            {
                int tmp=Q.front().num;
                if(vis[Q.front().time]==0)
                {
                    vis[Q.front().time]=1;
                    que[tmp].pop();
                    ans--;
                }
               Q.pop();
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值