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.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		public static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush =
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var N = cin.ReadInt;
			var Q = cin.ReadInt;
			var C = cin.ReadIntArray(N);
			var S = new SegmentTree<IEnumerable<int>>(N, (s, t) => s.Union(t), new List<int>());
			for(int i = 0; i < N; i++) {
				S.SetImmediate(i, new List<int> { C[i]-1 });
			}

			for(int j = 0; j < Q; j++) {
				int l = cin.ReadInt, r = cin.ReadInt;
				Console.WriteLine(S.GetValue(l - 1, r).Count());
			}
		}

	}

	static class ExtendClass {
		public static string Join<T>(this IEnumerable<T> X, string separator = " ")
			=> string.Join(separator, X);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator = separator;
			this._stream = stream;
			inputStream = new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count != 0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i < tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i < N; ++i)
				ret[i] = Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek() == -1; }
		}
	}

	class SegmentTree<T> {
		//UI is 0-indexed, internal is 1-indexed
		public readonly int rank;
		public T[] Nodes { get; }
		public Func<T, T, T> OPER { get; }
		public T unit { get; set; }
		protected int width { get { return 1 << (rank - 1); } }
		private T raise(int c) {
			return (c & 1) == 0
			? OPER(Nodes[c - 1], Nodes[(c - 1) + 1]) //$c is even
			: OPER(Nodes[(c - 1) - 1], Nodes[c - 1]); //odd
		}
		public IEnumerable<T> leafs() => Nodes.Skip(width - 1);
		protected int leafIndex(int x) => (1 << (rank - 1)) + (x - 1);
		public SegmentTree(int count, Func<T, T, T> _OPER, T _unit) {
			if(count == 0) throw new ArgumentNullException();
			var last = count - 1;
			rank = 0;
			if(last > 0) {
				while((last >> rank) > 1) rank++;
				rank += 2;
			} else rank = 1;
			Nodes = Enumerable.Repeat(_unit, (1 << rank) - 1).ToArray();
			OPER = _OPER;
			unit = _unit;
		}
		public virtual void SetImmediate(int x, T value) {
			var r = rank - 1;
			var jc = (1 << r) + x;
			Nodes[jc - 1] = value;
			for(r--; r >= 0; r--) {
				var parentIndex = (jc >> 1) - 1;
				var oldValue = Nodes[parentIndex];
				Nodes[parentIndex] = raise(jc);
				if(oldValue.Equals(Nodes[parentIndex])) break;
				jc >>= 1;
			}
		}
		public virtual void ImmediateOperate<S>(int x, S value, Func<S, T, T> LeftOP) {
			SetImmediate(x, LeftOP(value, Nodes[leafIndex(x)]));
		}
		public virtual T GetValue(int start, int end) => GV(start, end, 1, 0, width);
		private T GV(int start, int end, int count, int focusStart, int focusEnd) {
			if(focusEnd <= start || end <= focusStart) return unit;
			if(start <= focusStart && focusEnd <= end) return Nodes[count - 1];
			var mid = focusStart + (focusEnd - focusStart) / 2;
			return OPER(
				GV(start, end, count * 2, focusStart, mid),
				GV(start, end, count * 2 + 1, mid, focusEnd)
			);
		}
		public IEnumerable<T> GetLayer(int c) => Nodes.Skip((1 << (c - 1)) - 1).Take(1 << (c - 1));
		public IEnumerable<T> GetLeaves() => Nodes.Skip((1 << (rank - 1)) - 1).Take(1 << (rank - 1));
	}
}