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 Kotlin (1.3.71) (judge verdict: Time Limit Exceeded)

fun main() = Thread(null, ::abc174f, "(^o^)", 128 * 1024 * 1024).apply { start() }.join()

@OptIn(ExperimentalStdlibApi::class)
fun abc174f() {
    val (n, q) = readLine()!!.split(" ").map { it.toInt() }
    val cList = readLine()!!.split(" ").map { it.toInt() }
    val queries = List(q) {
        val (l, r) = readLine()!!.split(" ").map { it.toInt() - 1 }
        l to r
    }

    val st = SegmentTree(cList.map { setOf(it) }.toTypedArray(), emptySet()) { set1, set2 -> set1 + set2 }
    val answer = StringBuilder()

    for ((l, r) in queries) {
        answer.appendln(st[l..r].size)
    }

    print(answer)
}

private class SegmentTree(
    init: Array<Set<Int>>,
    val identityElement: Set<Int>,
    val monad: (Set<Int>, Set<Int>) -> Set<Int>
) {
    private var indices: Int
    private var nodes: Array<Set<Int>>

    init {
        var indices = 1
        while (indices < init.size) indices = indices shl 1
        val nodes = Array(2 * indices - 1) { identityElement }
        for (i in init.indices) nodes[i + indices - 1] = init[i]
        for (i in indices - 2 downTo 0) nodes[i] = monad(nodes[2 * i + 1], nodes[2 * i + 2])
        this.indices = indices
        this.nodes = nodes
    }

    operator fun set(x: Int, value: Set<Int>) {
        var index = x + indices - 1
        nodes[index] = value
        while (index > 0) {
            index = (index - 1) / 2
            nodes[index] = monad(nodes[2 * index + 1], nodes[2 * index + 2])
        }
    }

    operator fun get(range: IntRange) = get(range.first, range.last + 1)

    fun get(a: Int, b: Int, k: Int = 0, l: Int = 0, r: Int = indices): Set<Int> {
        if (r <= a || b <= l) return identityElement
        if (a <= l && r <= b) return nodes[k]
        return monad(
            get(a, b, 2 * k + 1, l, (l + r) / 2),
            get(a, b, 2 * k + 2, (l + r) / 2, r)
        )
    }
}