Language: C++

#pragma GCC optimize("O3")
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 string read_str() {
        string res;
        while(*now and *now != '\n' and *now != ' ') res += *now++;
        now++;
        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);
    }
    inline void write_str(string s, char margin = ' ') {
        for(auto c : s) putchar(c);
        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")
// 頑張れ！！

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:27:12: error: 'string' does not name a type
   27 |     inline string read_str() {
      |            ^~~~~~
a.cc:54:27: error: 'string' has not been declared
   54 |     inline void write_str(string s, char margin = ' ') {
      |                           ^~~~~~
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:1:1: note: 'stdin' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | #pragma GCC optimize("O3")
a.cc: In member function 'void IO<sz, T>::write(T, char)':
a.cc:35:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   35 |             putchar('0');
      |             ^~~~~~~
a.cc:35:13: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:36:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   36 |             putchar(margin);
      |             ^~~~~~~
a.cc:40:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   40 |             putchar('-');
      |             ^~~~~~~
a.cc:50:13: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   50 |             putchar(*now2);
      |             ^~~~~~~
a.cc:52:9: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   52 |         putchar(margin);
      |         ^~~~~~~
a.cc: In member function 'void IO<sz, T>::write_str(int, char)':
a.cc:55:22: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
   55 |         for(auto c : s) putchar(c);
      |                      ^
a.cc:55:22: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
a.cc:56:9: error: there are no arguments to 'putchar' that depend on a template parameter, so a declaration of 'putchar' must be available [-fpermissive]
   56 |         putchar(margin);
      |         ^~~~~~~
a.cc: At global scope:
a.cc:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   86 | main() {
      | ^~~~
a.cc: In function 'int main()':
a.cc:90:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   90 |     sort(q, q + Q);
      |     ^~~~
      |     short
a.cc: In instantiation of 'IO<sz, T>::IO() [with int sz = 50000000; T = long long int]':
a.cc:59:4:   required from here
   59 | 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:61:36:   required from here
   61 | inline void write(int x) { io.write(x); }
      |                            ~~~~~~~~^~~
a.cc:35:20: error: 'putchar' was not declared in this scope; did you mean 'char'?
   35 |             putchar('0');
      |             ~~~~~~~^~~~~
      |             char
a.cc:36:20: error: 'putchar' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
   36 |             putchar(margin);
      |             ~~~~~~~^~~~~~~~
a.cc:35:20: note: 'putchar' declared here, later in the translation unit
   35 |             putchar('0');
      |             ~~~~~~~^~~~~
a.cc:40:20: error: 'putchar' was not declared in this scope; did you mean 'char'?
   40 |             putchar('-');
      |             ~~~~~~~^~~~~
      |             char
a.cc:50:20: error: 'putchar' was not declared in this scope; did you mean 'char'?
   50 |             putchar(*now2);
      |             ~~~~~~~^~~~~~~
      |             char
a.cc:52:16: error: 'putchar' was not declared in this scope; did you mean 'char'?
   52 |         putchar(margin);
      |         ~~~~~~~^~~~~~~~
      |         char