Problem:
Score : 1000 points

Problem Statement

There are N balls in a row.
Initially, the i-th ball from the left has the integer A_i written on it.

When Snuke cast a spell, the following happens:

Let the current number of balls be k. All the balls with k written on them disappear at the same time.

Snuke's objective is to vanish all the balls by casting the spell some number of times.
This may not be possible as it is. If that is the case, he would like to modify the integers on the minimum number of balls to make his objective achievable.

By the way, the integers on these balls sometimes change by themselves.
There will be M such changes. In the j-th change, the integer on the X_j-th ball from the left will change into Y_j.

After each change, find the minimum number of modifications of integers on the balls Snuke needs to make if he wishes to achieve his objective before the next change occurs. We will assume that he is quick enough in modifying integers. Here, note that he does not actually perform those necessary modifications and leaves them as they are.

Constraints

1 \leq N \leq 200000

1 \leq M \leq 200000

1 \leq A_i \leq N

1 \leq X_j \leq N

1 \leq Y_j \leq N

Subscore

In the test set worth 500 points, N \leq 200 and M \leq 200.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
X_1 Y_1
X_2 Y_2
:
X_M Y_M

Output

Print M lines.
The j-th line should contain the minimum necessary number of modifications of integers on the balls to make Snuke's objective achievable.

Sample Input 1

5 3
1 1 3 4 5
1 2
2 5
5 4

Sample Output 1

0
1
1

After the first change, the integers on the balls become 2, 1, 3, 4, 5, from left to right. Here, all the balls can be vanished by casting the spell five times. Thus, no modification is necessary.

After the second change, the integers on the balls become 2, 5, 3, 4, 5, from left to right. In this case, at least one modification must be made. One optimal solution is to modify the integer on the fifth ball from the left to 2, and cast the spell four times.

After the third change, the integers on the balls become 2, 5, 3, 4, 4, from left to right. Also in this case, at least one modification must be made. One optimal solution is to modify the integer on the third ball from the left to 2, and cast the spell three times.

Sample Input 2

4 4
4 4 4 4
4 1
3 1
1 1
2 1

Sample Output 2

0
1
2
3

Sample Input 3

10 10
8 7 2 9 10 6 6 5 5 4
8 1
6 3
6 2
7 10
9 7
9 9
2 4
8 1
1 8
7 7

Sample Output 3

1
0
1
2
2
3
3
3
3
2

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define ld long double
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define pq priority_queue
using namespace std;

const int inf=1<<29;

inline int read(){
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
inline void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
inline int minn(int x,int y){if(x<y)return x;else return y;}
inline int maxx(int x,int y){if(x>y)return x;else return y;}
inline int abss(int x){if(x<0)return -x;else return x;}

const int N=400005;
int a[N],tim[N],num[N],l[N],r[N],tot,pre[N],nxt[N];
set<int> s;
int main()
{
	int n=read(),m=read();
	for(int i=1;i<=n;++i) a[i]=read(),++tim[a[i]];
	for(int i=1;i<=n;++i)
		if(tim[i]>0){
			num[i]=++tot;
			l[num[i]]=i-tim[i]+1;
			r[num[i]]=i;
			s.insert(i);
		}
	ll ans=0;
	for(int i=1;i<=tot;++i){
		pre[i]=i-1;
		if(i<tot) nxt[i-1]=i;
		ans+=maxx(0,r[pre[i]]-l[i]+1);
	}
	for(int i=1;i<=m;++i){
		int x=read(),y=read();
		if(a[x]==y){printf("%lld\n",ans);continue;}
		int nu=num[a[x]];
		ans-=maxx(0,r[pre[nu]]-l[nu]+1);
		if(nxt[nu]!=0) ans-=maxx(0,r[nu]-l[nxt[nu]]+1);
		if(tim[a[x]]==1){
			l[nu]=r[nu]=0;
			nxt[pre[nu]]=nxt[nu];
			if(nxt[nu]!=0) pre[nxt[nu]]=pre[nu];
			if(nxt[nu]!=0) ans+=maxx(0,r[pre[nu]]-l[nxt[nu]]+1);
			num[a[x]]=0;
			s.erase(a[x]);
		}
		else{
			++l[nu];
			ans+=maxx(0,r[pre[nu]]-l[nu]+1);
			if(nxt[nu]!=0) ans+=maxx(0,r[nu]-l[nxt[nu]]+1);
		}
		--tim[a[x]];
		if(num[y]==0){
			num[y]=++tot;
			r[tot]=y;
			l[tot]=y;
			set<int>::iterator it;
			it=s.lower_bound(y);
			if(it!=s.end()){
				nu=num[*it];
				ans-=maxx(0,r[pre[nu]]-l[nu]+1);
				nxt[pre[nu]]=tot;
				pre[tot]=pre[nu];
				nxt[tot]=nu;
				pre[nu]=tot;
				ans+=maxx(0,r[pre[tot]]-l[tot]+1);
				ans+=maxx(0,r[tot]-l[nu]+1);
			}
			else{
				nu=num[*--it];
				nxt[nu]=tot;
				pre[tot]=nu;
				nxt[tot]=0;
				ans+=maxx(0,r[nu]-l[tot]+1);
			}
		}
		else{
			nu=num[y];
			ans-=maxx(0,r[pre[nu]]-l[nu]+1);
			if(nxt[nu]!=0) ans-=maxx(0,r[nu]-l[nxt[nu]]+1);
			--l[nu];
			ans+=maxx(0,r[pre[nu]]-l[nu]+1);
			if(nxt[nu]!=0) ans+=maxx(0,r[nu]-l[nxt[nu]]+1);
		}
		++tim[y];
		a[x]=y;
		printf("%lld\n",ans);
	}
	return 0;
}