http://codeforces.com/problemset/problem/940/F
You are given an array a. You have to answer the following queries:
You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, …, c109}
You are given two integers p to x. Change ap to x.
The Mex of a multiset of numbers is the smallest non-negative integer not in the set.
Note that in this problem all elements of a are positive, which means that c0 = 0 and 0 is never the answer for the query of the second type.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100 000) — the length of the array and the number of queries respectively.
The second line of input contains n integers — a1, a2, …, an (1 ≤ ai ≤ 109).
Each of the next q lines describes a single query.
The first type of query is described by three integers ti = 1, li, ri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.
The second type of query is described by three integers ti = 2, pi, xi, where 1 ≤ pi ≤ n is the index of the element, which must be changed and 1 ≤ xi ≤ 109 is the new value.
Output
For each query of the first type output a single integer — the Mex of {c0, c1, …, c109}.
Example
input
10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8
output
2
3
2
#include<bits/stdc++.h>
using namespace std;
int block_len;
const int maxn=1e5+5;
struct node{
int l,r,bl,br,id,t;
node(){}
node(int l,int r,int id,int t):l(l),r(r),id(id),t(t){
bl=l/block_len;
br=r/block_len;
}
bool operator<(const node&x)const{
if(bl==x.bl){
if(br==x.br) return t<x.t;
else return br<x.br;
}
else return bl<x.bl;
}
}query[maxn];
struct node2{
int pos,Old,New;
}c[maxn];
int m,n,curT,Q,T,tot,l=1,r,mex;
int a[maxn],b[maxn<<1],now[maxn],cnt[maxn<<1],cnt2[maxn],ans[maxn];
inline void add(int val)
{
cnt2[cnt[val]]--;
cnt[val]++;
cnt2[cnt[val]]++;
}
inline void del(int val)
{
cnt2[cnt[val]]--;
cnt[val]--;
cnt2[cnt[val]]++;
}
inline void change(int pos,int x)
{
if(pos>=l&&pos<=r){
del(now[pos]);
add(x);
}
now[pos]=x;
}
int main()
{
scanf("%d%d",&n,&m);
block_len=pow(n,0.666667);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
now[i]=a[i];
b[tot++]=a[i];
}
for(int i=1;i<=m;i++){
int op;
scanf("%d",&op);
if(op==1){
int l,r;
scanf("%d%d",&l,&r);
query[++Q]=node(l,r,Q,T);
}
else{
int p,x;
scanf("%d%d",&p,&x);
b[tot++]=x;
c[++T]={p,now[p],x};
now[p]=x;
}
}
sort(query+1,query+Q+1);
sort(b,b+tot);
tot=unique(b,b+tot)-b;
for(int i=1;i<=n;i++){
now[i]=lower_bound(b,b+tot,a[i])-b+1;
}
for(int i=1;i<=T;i++){
c[i].Old=lower_bound(b,b+tot,c[i].Old)-b+1;
c[i].New=lower_bound(b,b+tot,c[i].New)-b+1;
}
for(int i=1;i<=Q;i++){
node &qr=query[i];
while(r<qr.r) r++,add(now[r]);
while(l>qr.l) l--,add(now[l]);
while(r>qr.r) del(now[r]),r--;
while(l<qr.l) del(now[l]),l++;
while(curT<qr.t) curT++,change(c[curT].pos,c[curT].New);
while(curT>qr.t) change(c[curT].pos,c[curT].Old),curT--;
mex=1;
while(cnt2[mex]>0) mex++;
ans[qr.id]=mex;
}
for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
//system("pause");
return 0;
}