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 Java (OpenJDK 11.0.6) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve();
    }
    public void solve() {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int N = scan.nextInt();
        int Q = scan.nextInt();
        int[] c = new int[N];
        for (int i = 0; i < N; i++) {
            c[i] = scan.nextInt()-1;
        }
        SegmentTree<Integer>[] seg = new SegmentTree[N];
        for (int i = 0; i < N; i++) {
            seg[i] = new SegmentTree<>(N, 0, Integer::max);
        }
        for (int i = 0; i < N; i++) {
            seg[c[i]].update(i, 1);
        }
        for (int i = 0; i < Q; i++) {
            int l = scan.nextInt()-1;
            int r = scan.nextInt();
            int ans = 0;
            for (int j = 0; j < N; j++) {
                ans += seg[j].query(l, r);
            }
            System.out.println(ans);
        }
    }
    class SegmentTree<T> {
        int size;
        T initialValue;
        List<T> list;
        BiFunction<? super T, ? super T, ? extends T> biFunction;
        SegmentTree(int n, T initialValue, BiFunction<? super T, ? super T, ? extends T> biFunction) {
            int size = 1;
            while (size < n) {
                size *= 2;
            }
            this.size = size;
            List<T> list = new ArrayList<T>();
            for (int i = 0; i < size * 2; i++) {
                list.add(initialValue);
            }
            this.list = list;
            this.initialValue = initialValue;
            this.biFunction = biFunction;
        }
        void update(int index, T value) {
            index += this.size - 1;
            list.set(index, value);
            while (0 < index) {
                index = (index - 1) / 2;
                list.set(index, biFunction.apply(list.get(2 * index + 1), list.get(2 * index + 2)));
            }
        }
        T query(int start, int end) {
            return query(start, end, 0, 0, this.size);
        }
        T query(int start, int end, int index, int left, int right) {
            if (right <= start || end <= left) {
                return this.initialValue;
            }
            if (start <= left && right <= end) {
                return list.get(index);
            }
            T value_left = query(start, end, index * 2 + 1, left, (left + right) / 2);
            T value_right = query(start, end, index * 2 + 2, (left + right) / 2, right);
            return biFunction.apply(value_left, value_right);
        }
    }
}