Polycarp is a frequent user of the very popular messenger. He’s chatting with his friends all the time. He has friends, numbered from to .
Recall that a permutation of size is an array of size such that each integer from to occurs exactly once in this array.
So his recent chat list can be represented with a permutation of size . is the most recent friend Polycarp talked to, is the second most recent and so on.
Initially, Polycarp’s recent chat list looks like (in other words, it is an identity permutation).
After that he receives m messages, the j-th message comes from the friend . 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 :
if he gets messaged by friend 3, then p becomes ;
if he gets messaged by friend 4, then p doesn’t change ;
if he gets messaged by friend 2, then p becomes .
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 and — the number of Polycarp’s friends and the number of received messages, respectively.
The second line contains m integers — 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:
So, for example, the positions of the friend are respectively. Out of these is the minimum one and is the maximum one. Thus, the answer for the friend is a pair .
In the second example, Polycarp’s recent chat list looks like this:
E題好水,完全可以跟D題或者C題交換一下位置
要找每個編號在序列中出現的最小位置和最大位置。
可以建一個長度爲的數組,表示元素在數組中的位置,表示元素的當前排名。
初始狀態賦值爲1,。
每次將一個元素放在首位,就將減一,加一,同時更新並更新答案。求排名過程可用樹狀數組維護,時間複雜度爲。
#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;
}