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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main
{
	static int bit[] ;
	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
                solve();
            }
        }, "1", 1 << 26).start();
	}
	static void solve () {
		FastReader fr =new FastReader();	PrintWriter op =new PrintWriter(System.out);
 
 		int n =fr.nextInt() ,q =fr.nextInt() ,Q[][] =new int[q][3] ,c[] =new int[n+1] ,mx[] =new int[n+1] ,ans[] =new int[q] ,i ,j ;
 		for (i =0 ; i<n ; ++i)	c[i+1] =fr.nextInt() ;
 		for (i =0 ; i<q ; ++i) {	Q[i][1] =fr.nextInt() ;	Q[i][0] =fr.nextInt() ;	Q[i][2] =i ;	}
 		sort (Q , 0 , q-1) ;	bit =new int[n+1] ;	

 		i =0 ;	Arrays.fill(mx , -1) ;
 		for (j =0 ; j<q ; ++j) {
 			while (i+1 <= Q[j][0]) {
 				++i ;	if (mx[c[i]] != -1)	u (mx[c[i]] , n , -1) ;
 				mx[c[i]] =i ;	u (mx[c[i]] , n , 1) ;
 			}
 			ans[Q[j][2]] =qu (i) - qu (Q[j][1]-1) ;
 		}
 		for (i =0 ; i<q ; ++i)	op.println(ans[i]) ;
		op.flush();	op.close();
	}
	public static void sort(int[][] arr , int l , int u) {
		int m ;
 
		if(l < u){
			m =(l + u)/2 ;
 
			sort(arr , l , m);	sort(arr , m + 1 , u);
 
			merge(arr , l , m , u);
		}
	}
	public static void merge(int[][]arr , int l , int m , int u) {
		int[][] low = new int[m - l + 1][3];
 
		int[][] upr = new int[u - m][3];
 
		int i ,j =0 ,k =0 ;
 
		for(i =l;i<=m;i++){
			low[i - l][0] =arr[i][0];
			low[i - l][1] =arr[i][1];
			low[i - l][2] =arr[i][2];
		}
 
		for(i =m + 1;i<=u;i++){
			upr[i - m - 1][0] =arr[i][0];
			upr[i - m - 1][1] =arr[i][1];
			upr[i - m - 1][2] =arr[i][2];
		}
 
		i =l;
 
		while((j < low.length) && (k < upr.length))
		{
			if(low[j][0] < upr[k][0])
			{
				arr[i][0] =low[j][0];
				arr[i][1] =low[j][1];
				arr[i++][2] =low[j++][2];
			}
			else
			{
				if(low[j][0] > upr[k][0])
				{
					arr[i][0] =upr[k][0];
					arr[i][1] =upr[k][1];
					arr[i++][2] =upr[k++][2];
				}
				else
				{
					if(low[j][1] < upr[k][1])
					{
						arr[i][0] =low[j][0];
						arr[i][1] =low[j][1];
						arr[i++][2] =low[j++][2];
					}
					else
					{
						arr[i][0] =upr[k][0];
						arr[i][1] =upr[k][1];
						arr[i++][2] =upr[k++][2];
					}
				}
			}
		}
 
		while(j < low.length)
		{
			arr[i][0] =low[j][0];
			arr[i][1] =low[j][1];
			arr[i++][2] =low[j++][2];
		}
 
		while(k < upr.length)
		{
			arr[i][0] =upr[k][0];
			arr[i][1] =upr[k][1];
			arr[i++][2] =upr[k++][2];
		}
	}
	static void u (int id , int n , int v) {
		for ( ; id<=n ; id += (id&(-id)))	bit[id] += v ;
	}
	static int qu (int id) {
		int r =0 ;	for ( ; id>0 ; id -= (id&(-id)))	r += bit[id] ;
		return r ;
	}
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br =new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st==null || (!st.hasMoreElements())) 
			{
				try
				{
					st =new StringTokenizer(br.readLine());
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				
			}
			return st.nextToken();
		}

		String nextLine() {
			String str ="";

			try
			{
				str =br.readLine();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}

			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next()) ;
		}
	}
}