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

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var a=sc.Ia2;
		var h=new int[s[0]+1];
		Fc fc=new Fc(s[0]+1);
		StringBuilder sb=new StringBuilder();
		for(int i = 1;i<=s[0];i++) {
			if(h[a[i]]!=0){
				fc.Ud(h[a[i]],i);
			}
			h[a[i]]=i;
		}
		for(int i = 0;i<s[1];i++) {
			var e=sc.Ia;
			sb.Append((e[1]-e[0]+1-fc.Get(e[0],e[1],e[0],e[1]))+"\n");
		}
		Console.Write(sb);
	}
}
public class Fc{
	private List<int>[] li;
	private int[][] dat1,dat2,dat3;
	private int m=1,cnt;
	private bool bo=false;
	public Fc(int n){
		while(m<n){m<<=1;}
		cnt=n;
		li=new List<int>[n];
		dat1=new int[m-1][];
		dat2=new int[m-1][];
		dat3=new int[m-1][];
		for(int i = 0;i<n;i++) {li[i]=new List<int>();}
	}
	public void Ud(int x,int y){li[x].Add(y);}
	private void Dn(){
		for(int i = 0;i<cnt;i++) {li[i].Sort();}
		for(int i = 0;i<cnt;i+=2) {
			int l=(i>>1)+(m>>1)-1;
			if(i+1<cnt){
				dat1[l]=new int[li[i].Count+li[i+1].Count+1];
				dat2[l]=new int[li[i].Count+li[i+1].Count+1];
				dat3[l]=new int[li[i].Count+li[i+1].Count+1];
				int j=0,k=0;
				while(j<li[i].Count&&k<li[i+1].Count){
					if(li[i][j]<li[i+1][k]){dat1[l][j+k]=li[i][j];j++;}
					else{dat1[l][j+k]=li[i+1][k];k++;}
					dat2[l][j+k]=j;
					dat3[l][j+k]=k;
				}
				while(j<li[i].Count){
					dat1[l][j+k]=li[i][j];
					dat2[l][j+k]=j;
					dat3[l][j+k]=k;
					j++;
				}
				while(k<li[i+1].Count){
					dat1[l][j+k]=li[i+1][k];
					dat2[l][j+k]=j;
					dat3[l][j+k]=k;
					k++;
				}
				dat1[l][j+k]=int.MaxValue;
				dat2[l][j+k]=j;
				dat3[l][j+k]=k;
			}
			else{
				dat1[l]=new int[li[i].Count+1];
				dat2[l]=new int[li[i].Count+1];
				dat3[l]=new int[li[i].Count+1];
				for(int j = 0;j<li[i].Count;j++) {
					dat1[l][j]=li[i][j];
					dat2[l][j]=j;
				}
				dat1[l][li[i].Count]=int.MaxValue;
				dat2[l][li[i].Count]=li[i].Count;
			}
		}
		for(int i = (m>>1)-2;i>=0;i--) {
			if(dat1[(i<<1)+2]!=null){
				dat1[i]=new int[dat1[(i<<1)+1].Length+dat1[(i<<1)+2].Length-1];
				dat2[i]=new int[dat1[(i<<1)+1].Length+dat1[(i<<1)+2].Length-1];
				dat3[i]=new int[dat1[(i<<1)+1].Length+dat1[(i<<1)+2].Length-1];
				int j=0,k=0;
				while(j<dat1[(i<<1)+1].Length-1&&k<dat1[(i<<1)+2].Length-1){
					if(dat1[(i<<1)+1][j]<dat1[(i<<1)+2][k]){dat1[i][j+k]=dat1[(i<<1)+1][j];j++;}
					else{dat1[i][j+k]=dat1[(i<<1)+2][k];k++;}
					dat2[i][j+k]=j;
					dat3[i][j+k]=k;
				}
				while(j<dat1[(i<<1)+1].Length-1){
					dat1[i][j+k]=dat1[(i<<1)+1][j];
					dat2[i][j+k]=j;
					dat3[i][j+k]=k;
					j++;
				}
				while(k<dat1[(i<<1)+2].Length-1){
					dat1[i][j+k]=dat1[(i<<1)+2][k];
					dat2[i][j+k]=j;
					dat3[i][j+k]=k;
					k++;
				}
				dat1[i][j+k]=int.MaxValue;
				dat2[i][j+k]=j;
				dat3[i][j+k]=k;
			}
			else if(dat1[(i<<1)+1]!=null){
				dat1[i]=new int[dat1[(i<<1)+1].Length];
				dat2[i]=new int[dat1[(i<<1)+1].Length];
				dat3[i]=new int[dat1[(i<<1)+1].Length];
				Array.Copy(dat1[(i<<1)+1],dat1[i],dat1[i].Length-1);
				Array.Copy(dat2[(i<<1)+1],dat2[i],dat2[i].Length-1);
				Array.Copy(dat3[(i<<1)+1],dat3[i],dat3[i].Length-1);
				dat1[i][dat1[i].Length-1]=int.MaxValue;
				dat2[i][dat1[i].Length-1]=dat1[i].Length-1;
			}
			else{dat1[i]=new int[]{int.MaxValue};}
		}
		bo=true;
	}
	public int Get(int x1,int x2,int y1,int y2){
		if(!bo){Dn();}
		int lb=-1,ub=dat1[0].Length,mid=0;
		while(ub-lb>1){
			mid=(ub+lb)>>1;
			if(dat1[0][mid]>y2){ub=mid;}
			else{lb=mid;}
		}
		int p=ub;
		lb=-1;ub=dat1[0].Length;
		while(ub-lb>1){
			mid=(ub+lb)>>1;
			if(dat1[0][mid]>=y1){ub=mid;}
			else{lb=mid;}
		}
		return Fdg(x1,x2,0,0,m-1,ub,p);
	}
	private int Fdg(int a,int b,int k,int l,int r,int la,int ra){
		if(r<a||b<l||ra==la){return 0;}
		if(a<=l&&r<=b){return ra-la;}
		else{return Fdg(a,b,k*2+1,l,(l+r)>>1,dat2[k][la],dat2[k][ra])+Fdg(a,b,k*2+2,(l+r+1)>>1,r,dat3[k][la],dat3[k][ra]);}
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}