Link:E. Messenger Simulator

E. Messenger Simulator

Description:
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≤3e5) — 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]

Problem solving:
这道题的意思是一开始有一个1-n的序列按顺序排列。然后会有m次操作,每次都会给出一个值,每次操作这个值都会跳到最前面,问你经过m次操作后,每个数能达到的最小和最大位置是哪里。
暴力的话肯定会超时。所以我们可以这样想,比如说第一个样例

1 2 3 4 5
会有四次操作,我们往前面放四个0
0 0 0 0 1 2 3 4 5
每次操作一个数,就提到最前面,然后它本来的位置变成0,这个数此时的位置就是它前面的非0的数字的个数加一,操作过程中还要同时更新最大值
这里有一个很巧的就是最小值不是1就是它本来的位置,因为如果对一个数操作,那么它的最小位置一定是1,如果没有对它操作,那它的最后的位置一定是大于等于初始位置,所以最小值很简单。
样例中的操作
0 0 0 3 1 2 0 4 5
0 0 5 3 1 2 0 4 0
0 1 5 3 0 2 0 4 0
4 1 5 3 0 2 0 0 0  
最后再统计一次每个数的位置更新最大值。

统计位置即看这个数前面有几个非零数字即可

统计每个数前有几个非零数字,这里我们可以用树状数组实现,因为这里是动态的。单点修改和区间查询。用树状数组会很方便。
注意这里树状数组需要开到6e5.因为n前面我们加上了m个位置,

Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
#define lowbit(x)    (x & (-x))
int       n, m, mx[maxn], mn[maxn], pos[maxn], mid[maxn];
void add(int x, int k)
{
    for (; x < maxn; x += lowbit(x))
        mid[x] += k;
}
int see(int x)
{
    int ans = 0;
    for (; x; x -= lowbit(x))
        ans += mid[x];
    return ans;
}
int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        mx[i] = mn[i] = i, pos[i] = i + m, add(i + m, 1);
    for (int i = 0; i < m; i++)
    {
        int mi; cin >> mi;
        mx[mi] = max(mx[mi], see(pos[mi])); mn[mi] = 1;
        add(pos[mi], -1);
        pos[mi] = m - i;
        add(pos[mi], 1);
    }
    for (int i = 1; i <= n; i++)
        mx[i] = max(mx[i], see(pos[i]));
    for (int i = 1; i <= n; i++)
        cout << mn[i] << " " << mx[i] << endl;
    return 0;
}