Problem:
Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.

Sample Input 1

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

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

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

Sample Output 2

1
2
2
1
2
2
6
3
3
3

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<stdio.h>
int R=1,C=1,H[2000010],N[2000010],L[1000010];
//評価関数（いまはMIN）
int hyouka(int a,int b){
  if(C<b)return 1;
  if(C<a||b==0)return 0;
  return (N[H[a]]<N[H[b]]||N[H[a]]==N[H[b]]&&L[H[a]]>L[H[b]])?1:0;
}
//挿入関数
void hin(int a){
  int i=C++;
  for(N[H[0]=R]=a;hyouka(0,i/2);i/=2)H[i]=H[i/2];
  H[i]=R++;
}
//取り出す関数
int hout(){
  int rt=H[1],i,j=2,k=H[--C];
  for(i=1;hyouka(i,C);i=j)H[i]=H[j=i*2+1-hyouka(i*2,i*2+1)];
  H[j/2]=k;
  return rt;
}
int n,B[500010]={0};
//BITのadd関数
//i番めの数値にnを足す
void badd(int i,int a){for(;i<=n;i+=i&-i)B[i]+=a;}
//BITのsum関数
//1からi番めの数の総和を返す
int  bsum(int i){
  int s=0;
  for(;i;i-=i&-i)s+=B[i];
  return s;
}
int main(){
  int m,i,p[500010],a[500010],r,ans[500050],j;
  scanf("%d %d",&n,&m);
  for(i=0;i<n;i++)scanf("%d",&a[i]);
  for(i=0;i<=n;i++)p[i]=-1;
  for(i=0;i<m;i++){
    scanf("%d %d",&L[R],&r);
    hin(r);
  }
  for(i=r=0;i<n;i++){
    j=hout();//printf("%d %d\n",L[j],N[j]);
    while(N[j]>r){//printf("p,%d\n",p[a[r]]);
      if(p[a[r]]+1)badd(p[a[r]],-1);//printf("%d\n",r);
      p[a[r]]=r+1;//printf("a\n");
      badd(r+1,1);//printf("r+1,%d\n",r+1);
      //printf("sum %d\n",bsum(r+1));
      r++;
    }
    //printf("N,L,j,%d %d %d\n",bsum(N[j]),bsum(L[j]-1),j);
    ans[j]=bsum(N[j])-bsum(L[j]-1);//printf("ans %d %d %d\n",N[j],L[j],ans[j]);
  }
  for(i=0;i<m;i++)printf("%d\n",ans[i+1]);
  return 0;
}