Language: C++

#include <algorithm>
int i;
#define rep2(i, a, b) for(i = a; i <= b; ++i)
#define rep(i, n) for(i = 0; i < n; ++i)
using namespace std;
// _________コーディングはここから！！___________
template <const int sz = 5 * 10000000, typename T = long long> struct IO {
    char reader[sz], writer[sz];
    char *now, *now2 = writer;
    IO() {
        reader[fread(reader, sizeof(char), sizeof(char) * sz, stdin)];
        now = reader;
    }
    inline T read() {
        while(*now && *now <= 32) now++;
        if(*now == '-') {
            now++;
            T res = 0;
            while('0' <= *now and *now <= '9') { res = res * 10 + *now++ - '0'; }
            return -res;
        } else {
            T res = 0;
            while('0' <= *now and *now <= '9') { res = res * 10 + *now++ - '0'; }
            return res;
        }
    }
    inline void write(T x, char margin = ' ') {
        if(x == 0) {
            putchar('0');
            putchar(margin);
            return;
        }
        if(x < 0) {
            putchar('-');
            x = -x;
        }
        while(x) {
            *now2 = '0' + x % 10;
            now2++;
            x /= 10;
        }
        do {
            now2--;
            putchar(*now2);
        } while(now2 != writer);
        putchar(margin);
    }
};
IO io;
inline int read() { return io.read(); }
inline void write(int x) { io.write(x); }
inline void write(int x, char c) { io.write(x, c); }
constexpr int N = 500003;
int c[N];
struct T {
    int l, r, idx;
    constexpr bool operator<(const T &rhs) const noexcept { return r < rhs.r; }
} q[N];
int ans[N], last[N], dat[N];

inline void dec(int k) {
    for(++k; k < N; k += k & -k) --dat[k];
}
inline int sum(int k) {
    int res = 0;
    for(++k; k > 0; k -= k & -k) res += dat[k];
    return res;
}
int n, Q, now = 1;

// 頑張れ！！
// #pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
// 頑張れ！！

int main() {
    n = read(), Q = read();
    rep2(i, 1, n) c[i] = read();
    rep(i, Q) q[i].l = read(), q[i].r = read() + 1, q[i].idx = i;
    sort(q, q + Q);
    rep(i, Q) {
        for(; now < q[i].r; ++now) {
            if(last[c[now]]) dec(last[c[now]]);
            last[c[now]] = now;
        }
        ans[q[i].idx] = sum(q[i].r - 1) - sum(q[i].l - 1) + q[i].r - q[i].l;
    }
    rep(i, Q) write(ans[i], '\n');
}


Compiler output:
a.cc: In constructor 'IO<sz, T>::IO()':
a.cc:11:63: error: 'stdin' was not declared in this scope
   11 |         reader[fread(reader, sizeof(char), sizeof(char) * sz, stdin)];
      |                                                               ^~~~~
a.cc:2:1: note: 'stdin' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    1 | #include <algorithm>
  +++ |+#include <cstdio>
    2 | int i;
a.cc: In member function 'void IO<sz, T>::write(T, char)':
a.cc:29:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   29 |             putchar('0');
      |             ^~~~~~~
a.cc:29:13: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:30:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   30 |             putchar(margin);
      |             ^~~~~~~
a.cc:34:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   34 |             putchar('-');
      |             ^~~~~~~
a.cc:44:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   44 |             putchar(*now2);
      |             ^~~~~~~
a.cc:46:9: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   46 |         putchar(margin);
      |         ^~~~~~~
a.cc: In instantiation of 'IO<sz, T>::IO() [with int sz = 50000000; T = long long int]':
a.cc:49:4:   required from here
   49 | IO io;
      |    ^~
a.cc:11:68: error: 'fread' was not declared in this scope; did you mean 'read'?
   11 |         reader[fread(reader, sizeof(char), sizeof(char) * sz, stdin)];
      |                                                                    ^
      |                                                                    read
a.cc: In instantiation of 'void IO<sz, T>::write(T, char) [with int sz = 50000000; T = long long int]':
a.cc:51:36:   required from here
   51 | inline void write(int x) { io.write(x); }
      |                            ~~~~~~~~^~~
a.cc:29:20: error: 'putchar' was not declared in this scope; did you mean 'u_char'?
   29 |             putchar('0');
      |             ~~~~~~~^~~~~
      |             u_char
a.cc:30:20: error: 'putchar' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
   30 |             putchar(margin);
      |             ~~~~~~~^~~~~~~~
a.cc:29:20: note: 'putchar' declared here, later in the translation unit
   29 |             putchar('0');
      |             ~~~~~~~^~~~~
a.cc:34:20: error: 'putchar' was not declared in this scope; did you mean 'u_char'?
   34 |             putchar('-');
      |             ~~~~~~~^~~~~
      |             u_char
a.cc:44:20: error: 'putchar' was not declared in this scope; did you mean 'u_char'?
   44 |             putchar(*now2);
      |             ~~~~~~~^~~~~~~
      |             u_char
a.cc:46:16: error: 'putchar' was not declared in this scope; did you mean 'u_char'?
   46 |         putchar(margin);
      |         ~~~~~~~^~~~~~~~
      |         u_char