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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5*(int)1e5+100;
int n, a[maxn], b[maxn], t[maxn*70], lt[maxn*70], rt[maxn*70], tVal[maxn*70], tot;
int lst[maxn];    //记录离散化后数组的上次出现位置
int q;

void init() {
    tot = 0;
    memset(lst, -1, sizeof(lst));
}
int build(int l, int r) {
    int node = ++tot;
    tVal[node] = 0;
    int mid = (l + r) >> 1;
    if (l < r) {
        lt[node] = build(l, mid);
        rt[node] = build(mid+1, r);
    }
    return node;
}
int update(int l, int r, int par, int p, int val) {
    int node = ++tot;
    lt[node] = lt[par]; rt[node] = rt[par]; tVal[node] = tVal[par] + val;
    int mid = (l + r) >> 1;
    if (l < r) {
        if (p <= mid) lt[node] = update(l, mid, lt[par], p, val);
        else rt[node] = update(mid+1, r, rt[par], p, val);
    }
    return node;
}
int query(int l, int r, int p, int node) {
    if (l == r) return tVal[node];
    int mid = (l + r) >> 1;
    if (p <= mid) return (query(l, mid, p, lt[node]) + tVal[rt[node]]);
    else return query(mid+1, r, p, rt[node]);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif

    init();
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    int len = unique(b + 1, b + 1 + n) - b;
    for (int i = 1; i <= n; ++i)
        a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;

    t[0] = build(1, n);
    for (int i = 1; i <= n; ++i) {
        if (lst[a[i]] == -1) t[i] = update(1, n, t[i-1], i, 1);
        else {
            int temp = update(1, n, t[i-1], lst[a[i]], -1);
            t[i] = update(1, n, temp, i, 1);
        }
        lst[a[i]] = i;
    }
    while (q--) {
        int l, r;
        cin >> l >> r;
        cout << query(1, n, l, t[r]) << '\n';
    }

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}