cf Educational Codeforces Round 80 E. Messenger Simulator

E. Messenger Simulator
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is a frequent user of the very popular messenger. He’s chatting with his friends all the time. He has n friends, numbered from 1 to n.
Recall that a permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array.

So his recent chat list can be represented with a permutation p of size n. p1 is the most recent friend Polycarp talked to, p2 is the second most recent and so on.

Initially, Polycarp’s recent chat list p looks like 1,2,…,n

(in other words, it is an identity permutation).

After that he receives m messages, the j-th message comes from the friend aj. And that causes friend aj to move to the first position in a permutation, shifting everyone between the first position and the current position of aj by 1. Note that if the friend aj is in the first position already then nothing happens.

For example, let the recent chat list be p=[4,1,5,3,2]:
if he gets messaged by friend 3, then p becomes [3,4,1,5,2];
if he gets messaged by friend 4, then p doesn’t change [4,1,5,3,2];
if he gets messaged by friend 2, then p becomes [2,4,1,5,3].

For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions.
Input

The first line contains two integers n and m (1≤n,m≤3⋅10^5) — the number of Polycarp’s friends and the number of received messages, respectively.

The second line contains m integers a1,a2,…,am (1≤ai≤n) — the descriptions of the received messages.
Output

Print n

pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message.
Examples
Input

5 4
3 5 1 4

Output

1 3
2 5
1 4
1 5
1 5

Input

4 3
1 2 4

Output

1 3
1 2
3 4
1 4

Note

In the first example, Polycarp’s recent chat list looks like this:

[1,2,3,4,5]
[3,1,2,4,5]
[5,3,1,2,4]
[1,5,3,2,4]
[4,1,5,3,2]

So, for example, the positions of the friend 2 are 2,3,4,4,5, respectively. Out of these 2 is the minimum one and 5 is the maximum one. Thus, the answer for the friend 2 is a pair (2,5).

In the second example, Polycarp’s recent chat list looks like this:

[1,2,3,4]
[1,2,3,4]
[2,1,3,4]
[4,2,1,3]

中文:

手机里有n个好友的消息列表,初始使每个好友的序列按顺序排列,每次好友来消息的时候,好友会从消息列表的当前位置跳到第一个位置,现在给出每个每个m个数,表示好友来消息的顺序,最后问你每个好友在消息列表中位置最大和最小是多少。

代码:

#include

using namespace std;
const int maxn = 6e5 + 1;
const int N = 3e5 + 1;

int c[maxn];
int a[N];
int mi[N],ma[N];
int n,m;

int lowbit(int x)
{
    return x&(-x);
}

void add(int pos,int val)
{
    while(pos<maxn)
    {
        c[pos]+=val;
        pos+=lowbit(pos);
    }
}
int query(int pos)
{
    int sum = 0;
    while(pos)
    {
        sum+=c[pos];
        pos-=lowbit(pos);
    }
    return sum;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            a[i]=i + m;
            ma[i]=mi[i]=i;
            add(i+m,1);
        }
        int x;
        int f = m;
        for(int i=1;i<=m;i++)
        {
            cin>>x;
            mi[x]=1;
            int res = query(a[x]);
            ma[x]=max(ma[x],res);
            add(a[x],-1);
            a[x]=f;
            add(a[x],1);
            f--;
        }
        for(int i=1;i<=n;i++)
        {
            ma[i]=max(ma[i],query(a[i]));
        }
        for(int i=1;i<=n;i++)
        {
            cout<<mi[i]<<" "<<ma[i]<<endl;
        }
    }
	return 0;
}

解答:

E题过了400多人,想必比较简单,果不其然-_-

题目给你n个人,m个数表示好友来消息的顺序。由于数据规模的限制,肯定要用一种数据结构维持数据的变化。
首先最小位置,如果消息列表中某个好友的位置没有发生过改变,那么最小位置就是当前位置,如果发生过改变,即来到第一个位置,那么最小位置就是1。
关键在于计算最大位置,这里利用树状数组或使用线段树维持一个前缀和,用来记录在列表中,每个好友前面有多少个人,就表示所在的位置了。假如当前将第i个好友移动到第一个位置,那么在前缀和中,需要先将大于等于i位置的前缀和全部减1,然后由于有m个好友发来消息会插入到消息列表的头部,所以在将所有大于1的位置的前缀和全部加1。
对应到树状数组的数据结构上,描述为对某个单点进行+1或-1的操作,每次查询时得到前缀和表示修改位置后的列表每个人所对应在列表中的新位置。
由于只有m个好友发消息,那么先在前面预留出m个空位置即可。

你可能感兴趣的:(数据结构)