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⋅105) — 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
inputCopy
5 4
3 5 1 4
outputCopy
1 3
2 5
1 4
1 5
1 5
inputCopy
4 3
1 2 4
outputCopy
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次操作
初始序列为:1,2,3,4,5.....n;
每次操作把一个数x移到第一位,其他数往后移。
问1---n个数,每个数在最左边的位置和在最右边的位置。
解法:我们可以在n个数前面填加m个空。
我们先初始化每个数的最左边的位置和最右边的位置都等于自己的下标,l[x]=r[x]=i。
进行第一个操作x,那么x的最左边位置一定为1,r[i]=max(x前面有多少个数,r[i])。
删去该位置的值,把x放在m个空的第m个位置上(继续往下就是m-1,m-2,m-3这些位置上)。
不断的进行m次操作。
最后重新遍历一遍更新r[i],因为有的数没有出现在操作上,而且相对位置发生了改变,所以需要更新一下。
比如:n=3,m=2
0,0,1,2,3
当x=2,先计算2前面有2个数,在比较r[i]=max(2,r[i]),然后删除该位置的数,把x放到m个空的第m个位置就是0,2,1,0,3;
当x=3,先计算3前面有3个数,在比较r[i]=max(3,r[i]),然后删除该位置的数,把x放到m个空的第m-1个位置,就是3,2,1,0,0;
然后重新遍历一遍,更新所有的r[i]。例如1不在操作里面,r[i]=1。但经过m次操作后,1最右边的位置为3了,所以需要更新一遍。
#include<bits/stdc++.h>
using namespace std;
const int N=6e5+1000;
int c[N],pos[N],x,l[N],r[N];
int a[N];
int n,m;
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int x)
{
while(i<N)
{
c[i]+=x;
i+=lowbit(i);
}
}
int sum(int i)
{
int res=0;
while(i)
{
res+=c[i];
i-=lowbit(i);
}
return res;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
l[i]=r[i]=i;
add(i+m,1);
pos[i]=i+m;
}
int last=m;
while(m--)
{
cin>>x;
l[x]=1;
int res=sum(pos[x]); //当前位置前面有多少个数
r[x]=max(r[x],res);
if(res==1) continue; //说明就在第一个
add(pos[x],-1); //删去这个位置
pos[x]=last; //放在前面补充的m个位置的最后一位
add(pos[x],1); //然后插入这个值
last--;
}
for(int i=1;i<=n;i++)
{
r[i]=max(r[i],sum(pos[i])); //更新答案
}
for(int i=1;i<=n;i++)
{
cout<<l[i]<<" "<<r[i]<<endl;
}
return 0;
}