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 <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>
#include <bitset>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct BIT {
    int n;
    vi bit;
    BIT(int num) : bit(num + 1, 0) {n = num; }
    void add(int i, int w){
        for(int x = i; x <= n; x += x & -x) bit[x] += w;
    }
    int sum(int i){ // sum of [1, i]
        int ret = 0;
        for(int x = i; x > 0; x -= x &- x) ret += bit[x];
        return ret;
    }
    int sum(int l, int r){  // sum of (l, r]
        return sum(r) - sum(l);
    }
};

typedef tuple<ll, ll, ll> tup;

void solve() {
    ll n, q;
    cin >> n >> q;
    vll c(n + 1);
    rep(i, n) cin >> c[i + 1];
    vector<tup> vtup;
    rep(i, q) {
        ll l, r;
        cin >> l >> r;
        vtup.emplace_back(r, l, i);
    }
    sort(vtup.begin(), vtup.end());

    vll last(n + 1, -1);
    vll ans(q);
    ll ic = 1;
    BIT bit(n);
    for(ll i = 0; i < q; i++) {
        ll r, l, idx;
        tie(r, l, idx) = vtup[i];
        while(ic <= n && ic <= r) {
            ll last_tmp = last[c[ic]];
            if(last_tmp != -1) bit.add(last_tmp, -1);
            last[c[ic]] = ic;
            bit.add(ic, 1);
            ++ic;
        }
        ans[idx] = bit.sum(l - 1, r);
    }
    for(ll& i : ans) cout << i << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}