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: Accepted)

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 500005
#define mid(l,r) l+(r-l)/2
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
int bit[N];
void update(int u,int k,int n){
	for(int i=u;i<=n;i+=(i&-i))bit[i]+=k;
}
int getsum(int u){
	int ans=0;
	for(int i=u;i;i-=(i&-i))ans+=bit[i];
	return ans;
}

void solve(){
	int n,q;
	cin>>n>>q;
	int c[n];
	rep(i,n)cin>>c[i];
	vector<pii>qry;
	int ans[q],l[q];
	memset(ans,0,sizeof(ans));
	rep(i,q){
		int l1,r;
		cin>>l1>>r;
		//if(l>1)qry.pb(pii(l-2,i));
		l[i]=l1-1;
		qry.pb(pii(r-1,i));
	}
	int last_visit[n+1];
	memset(last_visit,-1,sizeof(last_visit));
	sort(qry.begin(), qry.end());
	int j=0;
	for(int i=0;i<n;i++){
		if(last_visit[c[i]]!=-1)update(last_visit[c[i]]+1,-1,n);
		last_visit[c[i]]=i;
		update(i+1,1,n);
		while(j<qry.size()&&i==qry[j].fi){
			int id=qry[j].se;
			ans[id]=getsum(i+1)-getsum(l[id]);
			//cout<<ans[id]<<" "<<id<<" "<<getsum(i+1)<<" "<<getsum(l[id])<<"\n";
			j++;
		}
	}
	for(int i=0;i<q;i++)cout<<ans[i]<<"\n";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}