Language: C++

v#include<bits/stdc++.h>

using namespace std;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


struct Mo {

  int width;
  vector< int > left, right, order;
  vector< bool > v;

  Mo(int N, int Q) : width((int) sqrt(N)), order(Q), v(N) {
    iota(begin(order), end(order), 0);
  }

  void add(int l, int r) { /* [l, r) */
    left.emplace_back(l);
    right.emplace_back(r);
  }

  template< typename ADD, typename DEL, typename REM >
  void run(const ADD &add, const DEL &del, const REM &rem) {
    assert(left.size() == order.size());
    sort(begin(order), end(order), [&](int a, int b) {
      int ablock = left[a] / width, bblock = left[b] / width;
      if(ablock != bblock) return ablock < bblock;
      if(ablock & 1) return right[a] < right[b];
      return right[a] > right[b];
    });
    int nl = 0, nr = 0;
    auto push = [&](int idx) {
      v[idx].flip();
      if(v[idx]) add(idx);
      else del(idx);
    };
    for(auto idx : order) {
      while(nl > left[idx]) push(--nl);
      while(nr < right[idx]) push(nr++);
      while(nl < left[idx]) push(nl++);
      while(nr > right[idx]) push(--nr);
      rem(idx);
    }
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector< int > A(N);
  for(int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector< int > f(1000001);
  Mo mo(N, Q);
  for(int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    mo.add(--l, r);
  }
  int ret = 0;
  auto add = [&](int idx) { if(f[A[idx]]++ == 0) ret++; };
  auto del = [&](int idx) { if(f[A[idx]]-- == 1) --ret; };
  vector< int > ans(Q);
  auto rem = [&](int idx) { ans[idx] = ret; };
  mo.run(add, del, rem);
  for(int i = 0; i < Q; i++) {
    cout << ans[i] << "\n";
  }
}


Compiler output:
a.cc:1:2: error: stray '#' in program
    1 | v#include<bits/stdc++.h>
      |  ^
a.cc:1:1: error: 'v' does not name a type
    1 | v#include<bits/stdc++.h>
      | ^
a.cc: In constructor 'IoSetup::IoSetup()':
a.cc:7:5: error: 'cin' was not declared in this scope
    7 |     cin.tie(nullptr);
      |     ^~~
a.cc:8:5: error: 'ios' has not been declared
    8 |     ios::sync_with_stdio(false);
      |     ^~~
a.cc:9:5: error: 'cout' was not declared in this scope
    9 |     cout << fixed << setprecision(10);
      |     ^~~~
a.cc:9:13: error: 'fixed' was not declared in this scope
    9 |     cout << fixed << setprecision(10);
      |             ^~~~~
a.cc:9:22: error: 'setprecision' was not declared in this scope
    9 |     cout << fixed << setprecision(10);
      |                      ^~~~~~~~~~~~
a.cc:10:5: error: 'cerr' was not declared in this scope
   10 |     cerr << fixed << setprecision(10);
      |     ^~~~
a.cc: At global scope:
a.cc:18:3: error: 'vector' does not name a type
   18 |   vector< int > left, right, order;
      |   ^~~~~~
a.cc:19:3: error: 'vector' does not name a type
   19 |   vector< bool > v;
      |   ^~~~~~
a.cc: In constructor 'Mo::Mo(int, int)':
a.cc:21:34: error: 'sqrt' was not declared in this scope
   21 |   Mo(int N, int Q) : width((int) sqrt(N)), order(Q), v(N) {
      |                                  ^~~~
a.cc:21:44: error: class 'Mo' does not have any field named 'order'
   21 |   Mo(int N, int Q) : width((int) sqrt(N)), order(Q), v(N) {
      |                                            ^~~~~
a.cc:21:54: error: class 'Mo' does not have any field named 'v'
   21 |   Mo(int N, int Q) : width((int) sqrt(N)), order(Q), v(N) {
      |                                                      ^
a.cc:22:16: error: 'order' was not declared in this scope
   22 |     iota(begin(order), end(order), 0);
      |                ^~~~~
a.cc:22:10: error: 'begin' was not declared in this scope
   22 |     iota(begin(order), end(order), 0);
      |          ^~~~~
a.cc:22:24: error: 'end' was not declared in this scope
   22 |     iota(begin(order), end(order), 0);
      |                        ^~~
a.cc:22:5: error: 'iota' was not declared in this scope
   22 |     iota(begin(order), end(order), 0);
      |     ^~~~
a.cc: In member function 'void Mo::add(int, int)':
a.cc:26:5: error: 'left' was not declared in this scope
   26 |     left.emplace_back(l);
      |     ^~~~
a.cc:27:5: error: 'right' was not declared in this scope
   27 |     right.emplace_back(r);
      |     ^~~~~
a.cc: In member function 'void Mo::run(const ADD&, const DEL&, const REM&)':
a.cc:32:12: error: 'left' was not declared in this scope
   32 |     assert(left.size() == order.size());
      |            ^~~~
a.cc:32:27: error: 'order' was not declared in this scope
   32 |     assert(left.size() == order.size());
      |                           ^~~~~
a.cc:32:5: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   32 |     assert(left.size() == order.size());
      |     ^~~~~~
a.cc:32:5: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:33:10: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
   33 |     sort(begin(order), end(order), [&](int a, int b) {
      |          ^~~~~
a.cc:33:24: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
   33 |     sort(begin(order), end(order), [&](int a, int b) {
      |                        ^~~
a.cc: In lambda function:
a.cc:35:20: error: 'bblock' was not declared in this scope; did you mean 'ablock'?
   35 |       if(ablock != bblock) return ablock < bblock;
      |                    ^~~~~~
      |                    ablock
a.cc:36:29: error: 'right' was not declared in this scope
   36 |       if(ablock & 1) return right[a] < right[b];
      |                             ^~~~~
a.cc:37:14: error: 'right' was not declared in this scope
   37 |       return right[a] > right[b];
      |              ^~~~~
a.cc: In lambda function:
a.cc:41:7: error: 'v' was not declared in this scope
   41 |       v[idx].flip();
      |       ^
a.cc: In member function 'void Mo::run(const ADD&, const DEL&, const REM&)':
a.cc:47:18: error: 'right' was not declared in this scope
   47 |       while(nr < right[idx]) push(nr++);
      |                  ^~~~~
a.cc:49:18: error: 'right' was not declared in this scope
   49 |       while(nr > right[idx]) push(--nr);
      |                  ^~~~~
a.cc: In function 'int main()':
a.cc:57:3: error: 'cin' was not declared in this scope
   57 |   cin >> N >> Q;
      |   ^~~
a.cc:58:3: error: 'vector' was not declared in this scope
   58 |   vector< int > A(N);
      |   ^~~~~~
a.cc:58:11: error: expected primary-expression before 'int'
   58 |   vector< int > A(N);
      |           ^~~
a.cc:60:12: error: 'A' was not declared in this scope
   60 |     cin >> A[i];
      |            ^
a.cc:62:11: error: expected primary-expression before 'int'
   62 |   vector< int > f(1000001);
      |           ^~~
a.cc: In lambda function:
a.cc:70:32: error: 'f' was not declared in this scope
   70 |   auto add = [&](int idx) { if(f[A[idx]]++ == 0) ret++; };
      |                                ^
a.cc:70:34: error: 'A' was not declared in this scope
   70 |   auto add = [&](int idx) { if(f[A[idx]]++ == 0) ret++; };
      |                                  ^
a.cc: In lambda function:
a.cc:71:32: error: 'f' was not declared in this scope
   71 |   auto del = [&](int idx) { if(f[A[idx]]-- == 1) --ret; };
      |                                ^
a.cc:71:34: error: 'A' was not declared in this scope
   71 |   auto del = [&](int idx) { if(f[A[idx]]-- == 1) --ret; };
      |                                  ^
a.cc: In function 'int main()':
a.cc:72:11: error: expected primary-expression before 'int'
   72 |   vector< int > ans(Q);
      |           ^~~
a.cc: In lambda function:
a.cc:73:29: error: 'ans' was not declared in this scope
   73 |   auto rem = [&](int idx) { ans[idx] = ret; };
      |                             ^~~
a.cc: In function 'int main()':
a.cc:76:5: error: 'cout' was not declared in this scope
   76 |     cout << ans[i] << "\n";
      |     ^~~~
a.cc:76:13: error: 'ans' was not declared in this scope
   76 |     cout << ans[i] << "\n";
      |             ^~~
a.cc: In instantiation of 'void Mo::run(const ADD&, const DEL&, const REM&) [with ADD = main()::<lambda(int)>; DEL = main()::<lambda(int)>; REM = main()::<lambda(int)>]':
a.cc:74:9:   required from here
   74 |   mo.run(add, del, rem);
      |   ~~~~~~^~~~~~~~~~~~~~~
a.cc:32:39: error: 'assert' was not declared in this scope
   32 |     assert(left.size() == order.size());
      |                                       ^
a.cc:1:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
  +++ |+#include <cassert>
    1 | v#include<bits/stdc++.h>
a.cc:33:15: error: 'begin' was not declared in this scope
   33 |     sort(begin(order), end(order), [&](int a, int b) {
      |          ~~~~~^~~~~~~
a.cc:33:27: error: 'end' was not declared in this scope
   33 |     sort(begin(order), end(order), [&](int a, int b) {
      |                        ~~~^~~~~~~
a.cc:33:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   33 |     sort(begin(order), end(order), [&](int a, int b) {
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   34 |       int ablock = left[a] / width, bblock = left[b] / width;
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   35 |       if(ablock != bblock) return ablock < bblock;
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   36 |       if(ablock & 1) return right[a] < right[b];
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   37 |       return right[a] > right[b];
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
   38 |     });
      |     ~~