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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		int[] queryOrder = new int[Q];
		for(int i=0;i<Q;i++) queryOrder[i] = i;
		Array.Sort(queryOrder, (i, j) => R[i].CompareTo(R[j]));
		
		int[] last = new int[N + 1];
		for(int i=0;i<=N;i++) last[i] = -1;
		
		int[] ans = new int[Q];
		var BT = new FenwickTree(N + 10);
		int idx = 0;
		foreach(var q in queryOrder){
			
			int r = R[q];
			int l = L[q];
			while(idx <= r){
				int c = C[idx];
				if(last[c] == -1){
					last[c] = idx;
					BT.Add(idx + 1, 1);
				} else {
					var prev = last[c];
					BT.Add(prev + 1, -1);
					last[c] = idx;
					BT.Add(idx + 1, 1);
				}
				idx++;
			}
			
			int cnt = (int) (BT.Cumulate(r + 1) - BT.Cumulate(l));
			ans[q] = cnt;
		}
		
		Console.WriteLine(String.Join("\n",ans));
		
	}
	int N,Q;
	int[] C;
	int[] L,R;
	public Sol(){
		var d = ria();
		N = d[0]; Q = d[1];
		C = ria();
		L = new int[Q];
		R = new int[Q];
		for(int i=0;i<Q;i++){
			d = ria();
			L[i] = d[0] - 1;
			R[i] = d[1] - 1;
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}

class FenwickTree {
	// field
	long[] Data;
	public int N;		// base space size 
	int n;				// total size (pow of 2)
	
	// constructor
	public FenwickTree(int n_){
		N = n_;
		n = 1 << 2;
		while(n < N * 2) n <<= 1;
		Data = new long[n + 1];
	}

	// add at [0, i)
	public void Add(int i, long x){
		while(i <= n){
			Data[i] += x;
			i += (i & -i);
		}
	}
	
	// i => sum(a_k) for k in [0, i)
	public long Cumulate(int i){
		long s = 0;
		while(i > 0){
			s += Data[i];
			i -= (i & -i);
		}
		return s;
	}
}