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)

//F - Range Set Query
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;
#define LL long long
#define INF 0x7fffffff
#define Cl(X,Y) memset((X),(Y),sizeof(X))
const int MAXn=5e5+5;

int Total,Test,Al,Ar,Col[MAXn],Cnt[MAXn],Ans[MAXn];
int Head=1,Tail,At,Block,Belong[MAXn],Cntq,Cntc,New;
double Size;

struct Ask {
	int L,R,Time,ID;
	bool friend operator < (Ask A,Ask B) {
		return (Belong[A.L] ^ Belong[B.L]) ? Belong[A.L] < Belong[B.L] : ((Belong[A.R] ^ Belong[B.R]) ? Belong[A.R] < Belong[B.R] : A.Time < B.Time );	
	}
}Q[MAXn];

struct Change {
	int Color,Poi;
}C[MAXn];

inline int Read() {
	int Fac=1,Temp=0;
	char Ch=getchar();
	while(Ch<'0' || Ch>'9') {
		if(Ch=='-') Fac=-1;
		Ch=getchar();
	}
	while(Ch>='0' && Ch<='9') {
		Temp=(Temp<<1)+(Temp<<3)+Ch-'0';
		Ch=getchar();
	}
	return Temp*Fac;
}

int main() {
	Total=Read();Test=Read();
	for(int i=1;i<=Total;i++) Col[i]=Read();
	Size=pow(Total,2.0/3.0);Block=(ceil)((double)Total/Size);
	for(int i=1;i<=Block;i++) for(int j=Size*(i-1)+1;j<=Size*i;j++) Belong[j]=i;
	for(int i=1;i<=Test;i++) {
		// scanf("%c",&Opt);
		// if(Opt=='Q') {
			Q[++Cntq].L=Read();Q[Cntq].R=Read();
			Q[Cntq].Time=Cntc;Q[Cntq].ID=Cntq;
		// }
		// if(Opt=='R') { C[++Cntc].Poi=Read();C[Cntc].Color=Read(); }
	}
	sort(Q+1,Q+Cntq+1);
	for(int i=1;i<=Cntq;i++) {
		Al=Q[i].L;Ar=Q[i].R;At=Q[i].Time;
		while(Head<Al) New-=!--Cnt[Col[Head++]];
		while(Head>Al) New+=!Cnt[Col[--Head]]++;
		while(Tail<Ar) New+=!Cnt[Col[++Tail]]++;
		while(Tail>Ar) New-=!--Cnt[Col[Tail--]];  
		// while(Thick<At) {
		// 	++Thick;
		// 	if(C[Thick].Poi<=Ar && C[Thick].Poi>=Al) New-=!--Cnt[Col[C[Thick].Poi]]-!Cnt[C[Thick].Color]++;
		// 	swap(C[Thick].Color,Col[C[Thick].Poi]);
		// }
		// while(Thick>At) {
		// 	if(C[Thick].Poi<=Ar && C[Thick].Poi>=Al) New-=!--Cnt[Col[C[Thick].Poi]]-!Cnt[C[Thick].Color]++;
		// 	swap(C[Thick].Color,Col[C[Thick].Poi]);
		// 	--Thick;
		// } 
		Ans[Q[i].ID]=New;                           
	}
	for(int i=1;i<=Cntq;i++) printf("%d\n",Ans[i]);
	return 0;
}