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.Linq;

public class SegmentTree
{
    public int N { get; private set; }
    public HashSet<int>[] Node;
    public HashSet<int> InvalidVal { get; private set; }

    public SegmentTree(int[] a, HashSet<int> invVal)
    {
        InvalidVal = invVal;
        for (N = 1; N < a.Length; N *= 2) ;
        Node = new HashSet<int>[2 * N - 1];
        for (int i = 0; i < Node.Length; i++)
        {
            Node[i] = InvalidVal;
        }
        for (int i = 0; i < a.Length; i++)
        {
            Node[N + i - 1] = new HashSet<int> { a[i] };
        }
        for (int i = N - 2; i >= 0; i--)
        {
            Node[i] = Marge(Node[2 * i + 1], Node[2 * i + 2]);
        }
    }

    public HashSet<int> Get(int a, int b)
    {
        return Get(a, b, 0, 0, N);
    }

    private HashSet<int> Get(int a, int b, int k, int l, int r)
    {
        if (r <= a || b <= l)
        {
            return InvalidVal;
        }
        if (a <= l && r <= b)
        {
            return Node[k];
        }

        var vl = Get(a, b, 2 * k + 1, l, (l + r) / 2);
        var vr = Get(a, b, 2 * k + 2, (l + r) / 2, r);
        return Marge(vl, vr);
    }

    private static HashSet<int> Marge(HashSet<int> a, HashSet<int> b)
    {
        return new HashSet<int>(a.Union(b));
    }
}

public class Query
{
    public static int nextID = 0;
    public int ID;
    public int L;
    public int R;
    public int Answer;

    public Query(string s)
    {
        var vals = s.Split(' ').Select(int.Parse).ToArray();
        L = vals[0] - 1;
        R = vals[1];
        ID = nextID++;
    }
}

public static class Solution
{
    public static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var N = vals[0];
        var Q = vals[1];
        var c = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var queries = new List<Query>();
        for (int q = 0; q < Q; q++)
        {
            queries.Add(new Query(Console.ReadLine()));
        }

        var segtree = new SegmentTree(c, new HashSet<int>());
        var answers = queries.Select(q => segtree.Get(q.L, q.R).Count).ToArray();
        Console.WriteLine(string.Join(Environment.NewLine, answers));
    }
}