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: Runtime Error)

#include <iostream>
#include <math.h>
#include <cstdlib>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <cstdio>
#include <vector>
#include <cstring>
#include <bits/stdc++.h>

using namespace std;

pair <int,int> T_z[200100], T_or[200100];
int T_l[200100], T_najb[200100], tree[524288], sts[200100];

bool comparison(const pair<int,int> &a,const pair<int,int> &b){
    return ((a.first>b.first)||((a.first==b.first)&&(a.second>b.second)));
}
void wstaw(int x, int indx, int drz[])
{
    drz[indx]=x;
    while (indx!=1)
    {
        indx/=2; drz[indx]=drz[indx*2]+drz[indx*2+1];
    }
}
int suma_prz(int l, int p, int drz[])
{
    if (l==p) return drz[l];
    if (l/2==p/2) return drz[l/2];
    int res=drz[l]+drz[p];
    if (l%2==0) res+=drz[l+1];
    if (p%2==1) res+=drz[p-1];
    while (l/2/2!=p/2/2)
    {
        l/=2; p/=2;
        if (l%2==0) res+=drz[l+1];
        if (p%2==1) res+=drz[p-1];
    }
    return res;
}
int su_bin(int p1, int p2, int poc, int kon,  pair <int,int> T_wys[])
{
    int res, sr;
    while (poc<=kon)
    {
    sr=(poc+kon)/2;
    if (T_wys[sr].first<p1) {res=sr; kon=sr-1; continue;}
    if ((T_wys[sr].first==p1)&&(T_wys[sr].second<=p2)) {res=sr; kon=sr-1; continue;}
    poc=sr+1;
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, q; cin>>n>>q;
    for (int i=0;i<n;i++) cin>>T_l[i];
    for (int i=0;i<q;i++)
    {
    cin>>T_z[i].first>>T_z[i].second;
    T_or[i].first=T_z[i].first; T_or[i].second=T_z[i].second;
    }
    sort(T_z, T_z+q, comparison);
    int pot;
    for (int i=1;;i=i*2)
    {
        if (i>=n) { pot=i; break; }
    }
    int kt_q=0; bool prz=false;
    for (int i=n-1;i>=0;i--)
    {
       if (prz) break;
       if (T_najb[T_l[i]]!=0) wstaw(0, pot+T_najb[T_l[i]], tree);
       wstaw(1, pot+i, tree);
       T_najb[T_l[i]]=i;
       while (T_z[kt_q].first==i+1)
       {
           sts[kt_q]=suma_prz(T_z[kt_q].first+pot-1, T_z[kt_q].second+pot-1, tree);
           kt_q++;
           if (kt_q==q)  {prz=true; break;}
       }
    }
    for (int i=0;i<kt_q;i++)
    {
        cout<<sts[su_bin(T_or[i].first,T_or[i].second, 0, kt_q, T_z)]<<"\n";
    }

}