CodeForces 705C Thor

开30W个vector将数字归类,每一类数字开一个指针P,记录已经阅读到哪一个了,还可以开一个优先队列维护这些指针P。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); }
    return x;
}

const int maxn=300000+10;
vector<int>g[maxn];
int n,q,sz,p[maxn],ans;

struct X
{
    int num,now,pos,idx;
    bool operator < (const X &a) const {
        return idx>a.idx;
    }
    X(int Num,int Now,int Pos,int Idx) {num=Num; now=Now; pos=Pos,idx=Idx;}
};
priority_queue<X>Q;

int main()
{
    scanf("%d%d",&n,&q);
    sz=0; ans=0; memset(p,-1,sizeof p);
    for(int i=1;i<=q;i++)
    {
        int t,d; scanf("%d%d",&t,&d);
        if(t==1)
        {
            ++sz;
            if(p[d]==g[d].size()-1)
            {
                Q.push(X(d,p[d],p[d]+1,sz));
            }g[d].push_back(sz); ans++;
        }
        else if(t==2)
        {
            ans=ans-(g[d].size()-1-p[d]);
            p[d]=g[d].size()-1;
        }
        else
        {
            while(!Q.empty()&&Q.top().idx<=d)
            {
                X h=Q.top(); Q.pop();
                if(h.now<p[h.num]) continue;
                ans=ans-(h.pos-p[h.num]);
                p[h.num]=h.pos;
                if(p[h.num]+1<g[h.num].size())
                    Q.push(X(h.num,p[h.num],p[h.num]+1,g[h.num][p[h.num]+1]));
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-04 22:20:40

CodeForces 705C Thor的相关文章

【模拟】Codeforces 704A &amp; 705C Thor

题目链接: http://codeforces.com/problemset/problem/704/A http://codeforces.com/problemset/problem/705/C 题目大意: 雷神有N个应用,接下来Q个操作,操作分三种 1.应用X增加一个未读消息. 2.把应用X的未读消息全都读了.(可能重复读了已读的) 3.把前X个1操作的未读消息全都读了.(可能重复读了已读的) 求每一个操作后当前的未读消息总数. 题目思路: [模拟] 一开始看错题目,以为操作二是把后X个消

【打CF,学算法——三星级】Codeforces 704A Thor (模拟)

[CF简介] 题目链接:CF 704A 题面: A. 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 ph

Codeforces 704A Thor 队列模拟

题目大意:托尔有一部手机可执行三种操作 1.x APP产生一个新消息 2.读取x App已产生的所有消息 3.读取前t个产生的消息 问每次操作后未读取的消息的数量 题目思路: 队列模拟,坑点在于竟然卡内存……详细看代码. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h&g

Codeforces #366 Div. 2 C. Thor (模拟

http://codeforces.com/contest/705/problem/C 题目 模拟题 : 设的方法采用一个 r 数组(第几个app已经阅读过的消息的数量),和app数组(第几个app发出的消息的总数),加上一个 q 队列. 思路: 查询==1的时候,入队(记录顺序), sum++ (sum 为全部的剩余 没阅读的数量) 查询==2的时候,针对一个app,sum -(这个app发出的消息的总数 - 这个app已经阅读过的消息的数量),然后用 app数组 更新 r 数组,表示这个ap

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

题意: 手机里有n个应用,有三类操作. 第一类,第x个应用产生一个提醒通知 第二类,阅读了第x个应用产生的所有提醒通知,可能重复阅读已经读过的. 第三类,阅读了从头开始产生的t个提醒通知,可能重复阅读已经读过的. 输出每个操作发生后未读消息的个数. 分析: 对产生的消息进行编号,编号唯一. 未读的消息构成一个上述产生消息的子集s. 每个消息属于特定的应用,每个应用产生的消息构成队列. 这样, 1操作同时维护应用x产生的队列和整个的消息集合. 2操作读取应用x的消息队列,然后删除集合s中的这些消息

codeforces 704A (队列模拟) Thor

题目:这里 题意:n个app,q个操作,当操作数type为1的时候表示y这个app推送了你一条消息,当操作数type为2的时候表示将y这个app已推送的所有消息都读完,当操作数为3的时候 表示将已经推送的前(按推送的时间顺序)y条消息再读一遍(不管这前y条消息中有没有读过的,都再读一遍),问每次操作的时候的未读的消息数是多少? 用队列来模拟就好,记得每次要输出的是所有app的没有读过的消息的总数就行,不要想的太细,把读过的标记一下就行,总是觉得这个会超时,但是一想这才是2的第三题,1 的第一题,

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th