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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

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

    public SegmentTree(int[] a, int invVal)
    {
        InvalidVal = invVal;
        for (N = 1; N < a.Length; N *= 2) ;
        Node = new 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] = a[i];
        }
        for (int i = N - 2; i >= 0; i--)
        {
            Node[i] = Sum(Node[2 * i + 1], Node[2 * i + 2]);
        }
    }

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

    public void Update(int x, int v)
    {
        x += (N - 1);
        Node[x] = v;

        while (x > 0)
        {
            x = (x - 1) / 2;
            Node[x] = Sum(Node[2 * x + 1], Node[2 * x + 2]);
        }
    }

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

    private 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 Sum(vl, vr);
    }

    private static int Sum(int a, int b)
    {
        return a + b;
    }
}

public class Query
{
    // 1オリジン [L,R)
    public int L;
    public int R;
    public int Answer;

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

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];
        // 1オリジン c[0]はダミー
        var c = new int[1].Concat(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(0, N + 1);
        var cur = 1;
        var lastIndex = new int[N + 1];
        foreach (var q in queries.OrderBy(q => q.R))
        {
            for (; cur < q.R; cur++)
            {
                var v = c[cur];
                segtree.Update(lastIndex[v], 0);
                segtree.Update(cur, 1);
                lastIndex[v] = cur;
            }
            q.Answer = segtree.Get(q.L, q.R);
        }

        Console.WriteLine(string.Join(Environment.NewLine, queries.Select(q => q.Answer)));
    }
}