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: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store") // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop")// to pop argument of function as soon as it returns
#define all(a) a.begin(),a.end()
#define ll long long int
#define ld long double
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
#define ps push_back
#define fs first
#define sc second
#define takeline cin.ignore();
#define iactive cout.flush();
#define N 500005
#define endl "\n"
#define mod 1000000007
#define PI 3.141592653589793
//((1.0l)*BIG MULTIPLY MAGIC?)
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//-----------------------------------------------
ll n,ar[N]; set<ll> st[4*N];
void build(ll node,ll nl,ll nr){
	if(nl==nr){
		st[node].insert(ar[nl]);
		return ;
	}
	build(2*node,nl,(nl+nr)/2);
	build(2*node+1,(nl+nr)/2+1,nr);
	st[node].insert(all(st[2*node]));
	st[node].insert(all(st[2*node+1]));
}
set<ll> query(ll node,ll nl,ll nr,ll ql,ll qr){
	set<ll> l,r,an;
	if(nl>qr||ql>nr){
		return an;
	}
	if(nl>=ql&&qr>=nr){
		return st[node];
	}

	l=query(2*node,nl,(nl+nr)/2,ql,qr);
	r=query(2*node+1,(nl+nr)/2+1,nr,ql,qr);
	an.insert(all(l)); an.insert(all(r));
	return an;
}
int main(){

	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	ll i,j,k,l,n,q;
	cin>>n>>q;
	for(i=1;i<=n;i++){
		cin>>ar[i];
	}
	build(1,1,n);
	while(q--){
		cin>>k>>l;
		set<ll> st=query(1,1,n,k,l);
		cout<<st.size()<<endl;
	}
	return 0;	
}