# Messenger Simulator

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$. $p_1$ is the most recent friend Polycarp talked to, $p_2$ 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 $a_j$. 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 $a_1,a_2,…,a_m(1≤a_i≤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]$
E题好水，完全可以跟D题或者C题交换一下位置
要找每个编号在序列中出现的最小位置和最大位置。
可以建一个长度为$n+m$的数组$c$$loc[x]$表示元素$x$在数组中的位置，$\sum\limits_{i=1}^{loc[x]}c[i]$表示元素$x$的当前排名。
初始状态$c[m+1]\sim c[m+n]$赋值为1，loc[i]=\left\{ \begin{aligned} &i-m \quad\forall i \in\mathbb [m+1,m+n]\\ &0 \quad\forall i \in\mathbb [1,m] \end{aligned} \right.
每次将一个元素$x_i$放在首位，就将$c[loc[x_i]]$减一，$c[m-i+1]$加一,同时更新$loc[x]=m-i+1$并更新答案。求排名过程可用树状数组维护，时间复杂度为$O(mlog(n+m))$
#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}

const int N = 3e5 + 10;
int c[N << 1], n, m, loc[N], ans[N][2], h;

int get_sum(int k) {
int ans = 0;
while (k > 0) {
ans += c[k];
k -= lowbit(k);
}
return ans;
}

void add(int t, int v) {
while (t <= n + m) {
c[t] += v;
t += lowbit(t);
}
}

int main() {
n = qr(), m = qr();
h = m + 1;
repi(i, 1, n)add(m + i, 1), loc[i] = m + i, ans[i][0] = i;
repi(i, 1, m) {
int x = qr();
ans[x][0] = 1, ans[x][1] = max(ans[x][1], get_sum(loc[x]));
add(loc[x], -1), add(--h, 1), loc[x] = h;
}
repi(i, 1, n)printf("%d %d\n", ans[i][0], max(ans[i][1], get_sum(loc[i])));
return 0;
}


01-15 326

01-30 30

01-15 190

09-18 7404

01-19 113

01-19 1343

01-17 123

01-15 84

01-16 287

01-17 87

05-15 7901

02-12 82

01-15 809

01-15 66

01-16 61

01-16 151

01-28 68

01-25 34

01-16 40

01-18 135

#### Educational Codeforces Round 80 (Rated for Div. 2) E - Messenger Simulator（前缀和，树状数组）

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客