"Play CF, learn algorithm--three star" Codeforces 704A Thor (analog)

Source: Internet
Author: User

"About CF"


Title Link: CF 704A


Surface:

A. thortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There isn applications on the this phone. Thor is fascinated by the 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 is about to happen (in chronological order). They is 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 (no Tifications generated in First t events of the first type). It's guaranteed that there were @ Least t events of the first type before this even T. Note that he doesn ' t read First t unread notifications, he just reads the Very first t notifications generated on his phone and he could re-read some of them in This operation.

Thor and tell him the number of unread notifications after each event. Assume that initially there is no notifications in the phone.

Input

The first line of input contains the integers n andq (1?≤? N,? q≤?300?000)-the number of applications and the number of events to happen.

The next Q lines contain the events. Thei-th of these lines starts with an integertypei -type of theI-th event. If typei? =?1 ortypei? =?2 Then it's followed by an integerxI. Otherwise It is followed by an integerti (1?≤? Typei? ≤?3,?1?≤? Xi? ≤? N,? 1?≤? T i? ≤? q).

Output

Print the number of unread notifications after each event.

Examplesinput
3 41 31 11 22 3
Output
1232
Input
4 61 21 41 23 31 31 3
Output
123012
Note

In the first sample:

    1. Application 3 generates a notification (there is1 unread notification).
    2. Application 1 generates a notification (there is2 unread notifications).
    3. Application 2 generates a notification (there is3 unread notifications).
    4. Thor reads the notification generated by Application 3, there is2 unread notifications left.

In the second sample test:

    1. Application 2 generates a notification (there is1 unread notification).
    2. Application 4 generates a notification (there is2 unread notifications).
    3. Application 2 generates a notification (there is3 unread notifications).
    4. Thor reads first three notifications and since there is only three of them so far, there'll be no unread notification L Eft.
    5. Application 3 generates a notification (there is1 unread notification).
    6. Application 3 generates a notification (there is2 unread notifications).

Test instructions

The background is the mobile phone app produces unread messages, there are n apps, corresponding to three kinds of events, event one, the X app produces a new unread message. Event Two, Thor read all the unread information for the X app. In event three, Thor read the first T message, (these are the app messages that are generated sequentially, whether read or unread). After each event, the current number of unread messages is output.


Solving:

In the solution, the data structure takes a message list, records messages, an array of quantities, corresponds to the number of unread messages per app, a message vector array, the subscript for each message record generated by each app, a POS array, and a record of the last position that each app has currently processed for the final piece of information, A sum value records the total number of unread messages, and a P-value is recorded in chronological order by manipulating the last message position that is currently processed by the three.

Corresponding operation one, you can design a message list, each generating a new message, record the message generated by the app number, and a flag to indicate whether the message is read, and the number of arrays corresponding to the app to add one, the app's vector array records the message subscript, the total number of unread messages plus one.

For operation two, you can get the last position of an unread message processed by the app from the POS array in the app, and start scanning backwards to mark the message as read. At the same time, the total number of unread messages minus the number of unread messages in the app, and the number of unread messages in the app are zeroed, updating the last location information for processing unread messages.

Corresponding operation three, as long as from P (currently processed in the last position to start processing), the process will encounter unread and read messages, read the direct skip, unread need to mark Read, and the total sum value (number of unread messages) minus one, corresponding to the message generated by the app's unread array value also reduced by one.

The total complexity is O (n), because each message is produced at most one time, read it again.


Code:

#include <iostream> #include <vector> #include <cstdio> #include <cstring> #define LL long long# Define SZ 300010using namespace std;struct info{int Id;bool vis;} Store[sz];int amount[sz];int pos[sz];vector <int> v[sz];int Main () {    int sum=0,n,q,a,b,p=0,cnt=0;    scanf ("%d%d", &n,&q);    for (int i=0;i<q;i++)    {    scanf ("%d%d", &a,&b);    if (a==1)    {    v[b].push_back (CNT);    amount[b]++;    sum++;    store[cnt].vis=0;    store[cnt].id=b;    cnt++;    }    if (a==2)    {    sum-=amount[b];    amount[b]=0;    for (int j=pos[b];j<v[b].size (); j + +)    {    store[v[b][j]].vis=1;    }    Pos[b]=v[b].size ();    }    if (a==3)    {for    (int j=p;j<b;j++)    {    if (!store[j].vis)    {    Store[j].vis=1;    sum--;    amount[store[j].id]--;    }    }    P=max (p,b);    }    printf ("%d\n", sum);    } return 0;}


"Play CF, learn algorithm--three star" Codeforces 704A Thor (analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.