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>
#define pb push_back
#define mp make_pair
#define forn(i,a,b) for(int i =a;i<b;i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

//for debugging 
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14 
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}
// debugging ends here

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;


vector<int> a,b;
const int blen = 707;
int cnt;
const int mxn = 5e5+10;
int freq[mxn];

struct query{
	int l,r,idx;
};

bool cmp(query a, query b){
	return (a.l/blen < b.l/blen)||((a.l/blen == b.l/blen) &&(a.r<b.r));
}

void add(int i){
	freq[a[i]]++;
	if(freq[a[i]]==1)
		cnt++;
}

void remove(int i){
	freq[a[i]]--;
	if(freq[a[i]]==0)
		cnt--;
}


void solve(){
	int n;cin >> n;
	a= vector<int> (n);
	int q;
	cin>> q;
	for(int i =0;i<n;i++){
		cin >> a[i];
	}
	
	vector<query> qs(q);

	for(int i =0;i<q;i++){
		cin >> qs[i].l >> qs[i].r;
		qs[i].l--,qs[i].r--;
		qs[i].idx = i;
	}
	int ans[q];
	sort(qs.begin(),qs.end(),cmp);
	int ml = 0,mr = -1;
	for(int i =0;i<q;i++){
		int l = qs[i].l;
		int r=  qs[i].r;
		//adding the elements for answering the queries
		while(ml>l)add(--ml);
		while(mr<r)add(++mr);

		//removing the elements
		while(ml<l)remove(ml++);
		while(mr>r)remove(mr--);

		ans[qs[i].idx] = cnt;
	}	
	for(int i =0;i<q;i++){
		cout << ans[i] << endl;
	}

}

int main(){
	fast;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif

	// int t;cin >> t;while(t--)
		solve();
}