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.CodeDom;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace AtCoder
{
    public static class Ex
    {
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); }
        public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); }
        public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); }
        public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); }

        public static int PopCount(this int bits)
        {
            bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
            bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
            bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
            bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
            return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
        }
    }

    partial class AtCoder
    {
        public string GetStr() { return Console.ReadLine().Trim(); }
        public char GetChar() { return Console.ReadLine().Trim()[0]; }
        public int GetInt() { return int.Parse(Console.ReadLine().Trim()); }
        public long GetLong() { return long.Parse(Console.ReadLine().Trim()); }
        public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); }
        public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); }
        public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); }
        public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); }
        public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); }
        public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); }
        public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H).Select(s => Enumerable.Repeat(value, W).ToArray()).ToArray(); }
        public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetIntArray().ToArray()).ToArray(); }
        public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetLongArray().ToArray()).ToArray(); }
        public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetStr().ToCharArray()).ToArray(); }
        public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); }

        Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed)
        {
            var dic = new Dictionary<int, List<int>>();
            foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); }
            for (int i = 0; i < M; i++)
            {
                var input = GetIntArray();
                var a = isNode_0indexed ? input[0] : input[0] - 1;
                var b = isNode_0indexed ? input[1] : input[1] - 1;
                dic[a].Add(b);
                if (isDirected == false) dic[b].Add(a);
            }
            return dic;
        }
        Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed)
        {
            var dic = new Dictionary<int, List<Edge>>();
            foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); }
            for (int i = 0; i < M; i++)
            {
                var input = GetIntArray();
                var a = isNode_0indexed ? input[0] : input[0] - 1;
                var b = isNode_0indexed ? input[1] : input[1] - 1;
                var c = input[2];
                dic[a].Add(new Edge(b, c));
                if (isDirected == false) dic[b].Add(new Edge(a, c));
            }
            return dic;
        }

        bool eq<T, U>() => typeof(T).Equals(typeof(U));
        T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                           : eq<T, long>() ? ct<T, long>(long.Parse(s))
                           : eq<T, double>() ? ct<T, double>(double.Parse(s))
                           : eq<T, char>() ? ct<T, char>(s[0])
                                             : ct<T, string>(s);
        void Multi<T>(out T a) => a = cv<T>(GetStr());
        void Multi<T, U>(out T a, out U b)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        void Multi<T, U, V>(out T a, out U b, out V c)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }
        void Multi<T, U, V, W>(out T a, out U b, out V c, out W d)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]);
        }
        void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]);
        }
        void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]);
        }
    }

    class Edge
    {
        public Edge(int to, long length)
        {
            To = to;
            Length = length;
        }
        public int To { get; set; }
        public long Length { get; set; }
    }

    class Comparable : IComparable<Comparable>
    {
        public Comparable(int value)
        {
            Value = value;
        }

        public int Value { get; set; }
        public int CompareTo(Comparable c)
        {
            var a = Math.Abs(Value);
            var b = Math.Abs(c.Value);
            return a > b ? 1 : a == b ? 0 : -1;
        }
    }

    class Comparer<T> : IComparer<T>
    {
        public int Compare(T t1, T t2)
        {
            return 1;
        }
    }



    partial class Program
    {
        static void Main()
        {
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
            new AtCoder().Solve();
            Console.Out.Flush();
            Console.Read();
        }
    }

    public partial class AtCoder
    {
        public void Solve()
        {
            int N;int Q;
            Multi(out N, out Q);
            var C = GetIntArray();
            var dic = new Dictionary<int, Stack<int>>();
            for(int i = 0; i < N; i++)
            {
                var c = C[i];
                if (dic.ContainsKey(c) == false) dic.Add(c, new Stack<int>());
                dic[c].Push(i);
            }

            var segtree = new SegmentTree<int>(N, (int a, int b) => a + b, 0);
            foreach(var d in dic)
            {
                segtree.Set(d.Value.Pop(), 1);
            }
            segtree.Build();

            var query = GetIntJaggedArray(Q).Select((s, queryId) => new { queryId, l = s[0]-1, r = s[1]-1 }).OrderByDescending(o => o.r).ToArray();
            var ans = new int[Q];
            for(int i = 0,r=N-1; i < Q; i++)
            {
                while (r > query[i].r)
                {
                    var color = C[r];
                    if (dic[color].Any()) segtree.Update(dic[color].Pop(), 1);
                    r--;
                }
                segtree.Build();
                ans[query[i].queryId] = segtree.RangeQuery(query[i].l, query[i].r+1);
            }

            foreach (var a in ans) Console.WriteLine(a);
        }

        class SegmentTree<T>
        {
            /// <summary>
            /// セグ木のコンストラクタ
            /// </summary>
            /// <param name="_n">数列の長さ</param>
            /// <param name="_func">モノイドに行う処理</param>
            /// <param name="_gen">モノイドの単位限</param>
            public SegmentTree(int _n, Func<T, T, T> _func, T _gen)
            {
                n = (int)Math.Pow(2, Math.Floor(Math.Log(_n, 2)) + 1);
                data = new T[2 * n - 1].Select(s => _gen).ToArray();
                func = _func;
                gen = _gen;
            }

            readonly int n;
            T[] data;
            readonly Func<T, T, T> func;
            readonly T gen;

            /// <summary>
            /// セグ木の葉に要素をセットする
            /// </summary>
            /// <param name="i">数列の位置</param>
            /// <param name="x">セットする要素</param>
            public void Set(int i, T x)
            {
                data[i + n - 1] = x;
            }

            /// <summary>
            /// セグ木を構築する。
            /// </summary>
            public void Build()
            {
                for (int k = n - 2; k >= 0; k--)
                {
                    data[k] = func(data[2 * k + 1], data[2 * k + 2]);
                }
            }

            /// <summary>
            /// 要素の更新を行う
            /// </summary>
            /// <param name="i">更新を行う位置</param>
            /// <param name="x">更新する値</param>
            public void Update(int i, T x)
            {
                i += n - 1;
                data[i] = x;
                while (i > 0)
                {
                    i = (i - 1) / 2;
                    data[i] = func(data[i * 2 + 1], data[i * 2 + 2]);
                }
            }

            /// <summary>
            /// 区間[a,b)でfuncを実行した結果
            /// </summary>
            /// <param name="a"></param>
            /// <param name="b"></param>
            /// <returns></returns>
            public T RangeQuery(int a, int b)
            {
                return RangeQuerySub(a, b, 0, 0, n);
            }

            T RangeQuerySub(int a, int b, int k, int l, int r)
            {
                if (r <= a || b <= l) return gen;
                else if (a <= l && r <= b) return data[k];
                else
                {
                    T vl = RangeQuerySub(a, b, k * 2 + 1, l, (l + r) / 2);
                    T vr = RangeQuerySub(a, b, k * 2 + 2, (l + r) / 2, r);
                    return func(vl, vr);
                }
            }

            public T GetValue(int i)
            {
                return data[i + n - 1];
            }
        }
    }
}