Language: C++

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2>
inline void chmax(T1 &a, const T2 &b)
{
    if (a < b)
    {
        a = b;
    }
}
template <class T1, class T2>
inline void chmin(T1 &a, const T2 &b)
{
    if (a > b)
    {
        a = b;
    }
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int MX = 500001;
int llist[MX][MX];
int rlist[MX][MX];
int main()
{
    int N, Q;
    cin >> N >> Q;
    vector<int> c(N);
    rep(i, N)
    {
        cin >> c[i];
        --c[i];
    }

    ++llist[0][c[0]];
    for (int i = 1; i < N; ++i)
    {
        rep(j, N)
        {
            llist[i][j] = llist[i - 1][j];
        }
        ++llist[i][c[i]];
    }
    ++rlist[N - 1][c[N - 1]];
    for (int i = N - 2; i >= 0; ++i)
    {
        rep(j, N)
        {
            rlist[i][j] = rlist[i + 1][j];
        }
        ++rlist[i][c[i]];
    }

    string output;
    rep(xxx, Q)
    {
        int l, r;
        cin >> l >> r;
        --l;
        --r;

        int ans = 0;
        if (l != 0 && r != N - 1)
        {
            rep(i, N)
            {
                if (llist[N - 1][i] - llist[l - 1][i] - rlist[r + 1][i] != 0)
                {
                    ++ans;
                }
            }
        }
        else if (l == 0 && r == N - 1)
        {
            rep(i, N)
            {
                if (llist[N - 1][i] != 0)
                {
                    ++ans;
                }
            }
        }
        else if (l == 0)
        {
            rep(i, N)
            {
                if (llist[N - 1][i] - rlist[r + 1][i] != 0)
                {
                    ++ans;
                }
            }
        }
        else if (r == N - 1)
        {
            rep(i, N)
            {
                if (llist[N - 1][i] - llist[l - 1][i] != 0)
                {
                    ++ans;
                }
            }
        }
        output += to_string(ans) + "\n";
    }
    cout << output << endl;
}

Compiler output:
/tmp/ccaY0C6X.o: in function `main':
a.cc:(.text+0x26a): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x28e): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x2e0): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x306): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x357): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x37e): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x4ca): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
a.cc:(.text+0x5e7): relocation truncated to fit: R_X86_64_PC32 against symbol `rlist' defined in .bss section in /tmp/ccaY0C6X.o
collect2: error: ld returned 1 exit status