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;
}

### Codeforces 1732A Bestie 题目解析 对于给定的整数数组 \(a\) 和查询次数 \(q\),每次查询给出两个索引 \(l, r\),需要计算子数组 \([l,r]\) 的最大公约数(GCD)。如果 GCD 结果为 1,则返回 "YES";否则返回 "NO"[^4]。 #### 解决方案概述 为了高效解决这个问题,可以预先处理数据以便快速响应多个查询。具体方法如下: - **预处理阶段**:构建辅助结构来存储每一对可能区间的 GCD 值。 - **查询阶段**:利用已有的辅助结构,在常量时间内完成每个查询。 然而,考虑到内存限制以及效率问题,直接保存所有区间的结果并不现实。因此采用更优化的方法——稀疏表(Sparse Table),它允许 O(1) 时间内求任意连续子序列的最大值/最小值/GCD等问题,并且支持静态RMQ(Range Minimum Query)/RANGE_GCD等操作。 #### 实现细节 ##### 构建稀疏表 通过动态规划的方式填充二维表格 `st`,其中 `st[i][j]` 表示从位置 i 开始长度为 \(2^j\) 的子串的最大公约数值。初始化时只需考虑单元素情况即 j=0 的情形,之后逐步扩展至更大的范围直到覆盖整个输入序列。 ```cpp const int MAXN = 2e5 + 5; int st[MAXN][20]; // Sparse table for storing precomputed results. vector<int> nums; void build_sparse_table() { memset(st,-1,sizeof(st)); // Initialize the base case where interval length is one element only. for(int i = 0 ;i < nums.size(); ++i){ st[i][0]=nums[i]; } // Fill up sparse table using previously computed values. for (int j = 1;(1 << j)<=nums.size();++j){ for (int i = 0;i+(1<<j)-1<nums.size();++i){ if(i==0 || st[i][j-1]!=-1 && st[i+(1<<(j-1))][j-1]!=-1) st[i][j]=__gcd(st[i][j-1],st[i+(1<<(j-1))][j-1]); } } } ``` ##### 处理查询请求 当接收到具体的 l 和 r 参数后,可以通过查找对应的 log₂(r-l+1) 来定位合适的跳跃步长 k ,进而组合得到最终答案。 ```cpp string query(int L,int R){ int K=(int)(log2(R-L+1)); return __gcd(st[L][K],st[R-(1<<K)+1][K])==1?"YES":"NO"; } ``` 这种方法能在较短时间内完成大量查询任务的同时保持较低的空间开销,非常适合本题设定下的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值