Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
//typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
//const ll mod = 998244353;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

template< typename Monoid >
struct SegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;

  int sz;
  vector< Monoid > seg;

  const F f;
  const Monoid M1;

  // n: 要素数，F: モノイド間の演算，M1: 演算の単位元
  // F は無名関数で渡す
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  // k番目の要素にxを代入
  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }

  // 構築
  void build() {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  // k番目の値をxに変更
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  // 区間[a, b)に関して演算して結果を返す
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }

  // k番目の値を返す(set[k]でアクセス)
  Monoid operator[](const int &k) const {
    return seg[k + sz];
  }


  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }


  template< typename C >
  //[a, x)がcheckを満たす最初の要素位置xを返す
  // ↑嘘かも．[a, x]が正しい
  // 存在しない場合は-1を返すことに注意
  // Cには判定関数を入れる
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, seg[1]))) return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, seg[a]);
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }

  //[x, b)がcheckを満たす最後の要素位置xを返す
  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(seg[1], R))) return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(seg[--b], R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, Q;
    cin >> N >> Q;
    vector<ll> c(N);
    REP(i, N) cin >> c[i];
    REP(i, N) c[i]--;
    vector<ll> l(Q), r(Q);
    REP(i, Q) cin >> l[i] >> r[i];
    REP(i, Q) l[i]--, r[i]--;
    vl res(Q, -1);

    vvl pos(N);
    REP(i, Q){
        pos[r[i]].push_back(i);
    }

    SegmentTree<ll> seg(N+1, [](ll a, ll b){return a+b;}, 0);

    vector<ll> memo(N, -1);

    REP(i, N){
        if(memo[c[i]] != -1){
            seg.update(memo[c[i]], 0);
        }
        memo[c[i]] = i;
        seg.update(i, 1);

        for(auto idx: pos[i]){
            ll tmp = seg.query(l[idx], r[idx]+1);
            res[idx] = tmp;
        }
    }
    
    REP(i, Q) cout << res[i] << "\n";

    return 0;
}#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
//typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
//const ll mod = 998244353;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

template< typename Monoid >
struct SegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;

  int sz;
  vector< Monoid > seg;

  const F f;
  const Monoid M1;

  // n: 要素数，F: モノイド間の演算，M1: 演算の単位元
  // F は無名関数で渡す
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  // k番目の要素にxを代入
  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }

  // 構築
  void build() {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  // k番目の値をxに変更
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  // 区間[a, b)に関して演算して結果を返す
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }

  // k番目の値を返す(set[k]でアクセス)
  Monoid operator[](const int &k) const {
    return seg[k + sz];
  }


  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }


  template< typename C >
  //[a, x)がcheckを満たす最初の要素位置xを返す
  // ↑嘘かも．[a, x]が正しい
  // 存在しない場合は-1を返すことに注意
  // Cには判定関数を入れる
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, seg[1]))) return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, seg[a]);
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }

  //[x, b)がcheckを満たす最後の要素位置xを返す
  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(seg[1], R))) return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(seg[--b], R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, Q;
    cin >> N >> Q;
    vector<ll> c(N);
    REP(i, N) cin >> c[i];
    REP(i, N) c[i]--;
    vector<ll> l(Q), r(Q);
    REP(i, Q) cin >> l[i] >> r[i];
    REP(i, Q) l[i]--, r[i]--;
    vl res(Q, -1);

    vvl pos(N);
    REP(i, Q){
        pos[r[i]].push_back(i);
    }

    SegmentTree<ll> seg(N+1, [](ll a, ll b){return a+b;}, 0);

    vector<ll> memo(N, -1);

    REP(i, N){
        if(memo[c[i]] != -1){
            seg.update(memo[c[i]], 0);
        }
        memo[c[i]] = i;
        seg.update(i, 1);

        for(auto idx: pos[i]){
            ll tmp = seg.query(l[idx], r[idx]+1);
            res[idx] = tmp;
        }
    }
    
    REP(i, Q) cout << res[i] << "\n";

    return 0;
}

Compiler output:
a.cc:189:2: error: stray '#' in program
  189 | }#include <bits/stdc++.h>
      |  ^
a.cc:189:3: error: 'include' does not name a type
  189 | }#include <bits/stdc++.h>
      |   ^~~~~~~
a.cc:201:10: error: redefinition of 'const ll mod'
  201 | const ll mod = 1e9 + 7;
      |          ^~~
a.cc:13:10: note: 'const ll mod' previously defined here
   13 | const ll mod = 1e9 + 7;
      |          ^~~
a.cc:210:24: error: redefinition of 'template<class T> bool chmax(T&, const T&)'
  210 | template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:22:24: note: 'template<class T> bool chmax(T&, const T&)' previously declared here
   22 | template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:211:24: error: redefinition of 'template<class T> bool chmin(T&, const T&)'
  211 | template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:23:24: note: 'template<class T> bool chmin(T&, const T&)' previously declared here
   23 | template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:213:37: error: redefinition of 'template<class S, class T> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)'
  213 | template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
      |                                     ^~~~~~~~
a.cc:25:37: note: 'template<class S, class T> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)' previously declared here
   25 | template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
      |                                     ^~~~~~~~
a.cc:216:28: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>)'
  216 | template<class T> ostream& operator << (ostream& os, const vector<T> v){
      |                            ^~~~~~~~
a.cc:28:28: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>)' previously declared here
   28 | template<class T> ostream& operator << (ostream& os, const vector<T> v){
      |                            ^~~~~~~~
a.cc:219:28: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<std::vector<_Tp> >)'
  219 | template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
      |                            ^~~~~~~~
a.cc:31:28: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<std::vector<_Tp> >)' previously declared here
   31 | template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
      |                            ^~~~~~~~
a.cc:223:27: error: redefinition of 'template<class T> void debug(std::vector<std::vector<_Tp> >&, ll, ll)'
  223 | template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
      |                           ^~~~~
a.cc:35:27: note: 'template<class T> void debug(std::vector<std::vector<_Tp> >&, ll, ll)' previously declared here
   35 | template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
      |                           ^~~~~
a.cc:225:27: error: redefinition of 'template<class T> void debug(std::vector<_Tp>&, ll)'
  225 | template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
      |                           ^~~~~
a.cc:37:27: note: 'template<class T> void debug(std::vector<_Tp>&, ll)' previously declared here
   37 | template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
      |                           ^~~~~
a.cc:230:8: error: redefinition of 'struct SegmentTree<Monoid>'
  230 | struct SegmentTree {
      |        ^~~~~~~~~~~
a.cc:42:8: note: previous definition of 'struct SegmentTree<Monoid>'
   42 | struct SegmentTree {
      |        ^~~~~~~~~~~
a.cc:338:5: error: redefinition of 'int main()'
  338 | int main(){
      |     ^~~~
a.cc:150:5: note: 'int main()' previously defined here
  150 | int main(){
      |     ^~~~