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: Time Limit Exceeded)

#include<bits/stdc++.h>
#include<cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll> 
#define vd vector<double>
#define vld vector<long double>
#define ll int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for(ll i=0; i<n; i++) 
#define forr1(i, n) for(ll i=1; i<=n; i++)
using namespace std;
const ld PI =3.1415926535897923846;
const ll MOD = 1000000007;
const ll N=998244353;
ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;}
ll modinverse(ll a){return power(a, MOD-2);}
struct node{
    ll answer;
    map<ll, ll> mp1;
}segtree[2*1000002];
vl color;
node util(node x, node y)
{
    node ans;
    map<ll, ll>:: iterator itr;
    for(itr=(x.mp1).begin(); itr!=(x.mp1).end(); itr++)
    {
        ll col=itr->first;
        ans.mp1[col]++;
    }
    for(itr=(y.mp1).begin(); itr!=(y.mp1).end(); itr++)
    {
        ll col=itr->first;
        ans.mp1[col]++;
    }
    ans.answer=(ll)((ans.mp1).size());
    return ans;
}
node Query(ll start, ll end, ll r1, ll r2, ll put)
{
    if(start>=r1&&end<=r2)
    return segtree[put];
    node sex;
    map<ll, ll> mp10;
    sex.mp1=mp10;
    sex.answer=0;
    if(start>r2||end<r1)
    return sex;
    ll mid=start+(end-start)/2;
    return util(Query(start, mid, r1, r2, 2*put+1), Query(mid+1, end, r1, r2, 2*put+2));
}
node constructTree(ll start, ll end, ll put)
{
    if(start==end)
    {
        map<ll, ll> fuck;
        fuck[color[start]]++;
        segtree[put].answer=1;
        segtree[put].mp1=fuck;
        return segtree[put];
    }
    ll mid=start+(end-start)/2;
    segtree[put]=util(constructTree(start, mid, 2*put+1), constructTree(mid+1, end, 2*put+2));
    return segtree[put];
}
void solve()
{
    ll n, q;
	cin>>n>>q;
	color.clear();
	color.resize(n);
	forr(i, n)
	{
	    cin>>color[i];
	}
	ll x=(ll)ceil((log2(n)));
    x=(ll)pow(2, x);
    constructTree(0, n-1, 0);
	forr(i, q)
	{
	    ll lef, rig;
	    cin>>lef>>rig;
	    --lef;
	    --rig;
	    node lala=Query(0, n-1, lef, rig, 0);
        cout<<lala.answer<<endl;
	    
	}
}
int main()
{
    ll test=1;
    while(test--)
    {
        solve();
    }
}