Language: C++


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

  Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}

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

  /* ソート */
  void build()
  {
    order.resize(left.size());
    iota(begin(order), end(order), 0);
    sort(begin(order), end(order), [&](int a, int b)
    {
      if(left[a] / width != left[b] / width) return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process()
  {
    if(ptr == order.size()) return (-1);
    const auto id = order[ptr];
    while(nl > left[id]) distribute(--nl);
    while(nr < right[id]) distribute(nr++);
    while(nl < left[id]) distribute(nl++);
    while(nr > right[id]) distribute(--nr);
    
    return order[ptr++];
  }

  inline void distribute(int idx)
  {
    v[idx].flip();
    if(v[idx]) add(idx);
    else del(idx);
  }

  void add(int idx);

  void del(int idx);
};



void Mo::add(int idx)
{
  if(cnt[A[idx]]++ == 0) ++sum;
}

void Mo::del(int idx)
{
  if(--cnt[A[idx]] == 0) --sum;
}




int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n,q;
  cin>>n>>q;

  REP(i,n)cin>>A[i];
  vll l(q), r(q);
  REP(i,q){
    cin>>l[i]>>r[i];
    l[i]--;
    r[i];
  }

  Mo mo(n);
  REP(i,q)
    mo.insert(l[i],r[i]);
  mo.build();

  REP(i,MAX_N)cnt[i]=0;
  REP(i,MAX_N)ans[i]=0;
  sum=0;

  REP(i,q){
    ll res=mo.process();
    ans[res]=sum;
  }
  REP(i,q)cout<<ans[i]<<endl;


}



Compiler output:
a.cc:2:3: error: 'vector' does not name a type
    2 |   vector< int > left, right, order;
      |   ^~~~~~
a.cc:3:3: error: 'vector' does not name a type
    3 |   vector< bool > v;
      |   ^~~~~~
a.cc:7:3: error: ISO C++ forbids declaration of 'Mo' with no type [-fpermissive]
    7 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |   ^~
a.cc: In function 'int Mo(int)':
a.cc:7:15: error: only constructors take member initializers
    7 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |               ^~~~~
a.cc:7:27: error: 'sqrt' was not declared in this scope
    7 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |                           ^~~~
a.cc:7:65: warning: no return statement in function returning non-void [-Wreturn-type]
    7 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |                                                                 ^
a.cc: In function 'void insert(int, int)':
a.cc:11:5: error: 'left' was not declared in this scope
   11 |     left.push_back(l);
      |     ^~~~
a.cc:12:5: error: 'right' was not declared in this scope
   12 |     right.push_back(r);
      |     ^~~~~
a.cc: In function 'void build()':
a.cc:18:5: error: 'order' was not declared in this scope
   18 |     order.resize(left.size());
      |     ^~~~~
a.cc:18:18: error: 'left' was not declared in this scope
   18 |     order.resize(left.size());
      |                  ^~~~
a.cc:19:10: error: 'begin' was not declared in this scope
   19 |     iota(begin(order), end(order), 0);
      |          ^~~~~
a.cc:19:24: error: 'end' was not declared in this scope
   19 |     iota(begin(order), end(order), 0);
      |                        ^~~
a.cc:19:5: error: 'iota' was not declared in this scope
   19 |     iota(begin(order), end(order), 0);
      |     ^~~~
a.cc: In lambda function:
a.cc:23:14: error: 'right' was not declared in this scope
   23 |       return right[a] < right[b];
      |              ^~~~~
a.cc: In function 'void build()':
a.cc:20:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   20 |     sort(begin(order), end(order), [&](int a, int b)
      |     ^~~~
      |     short
a.cc: In function 'int process()':
a.cc:30:15: error: 'order' was not declared in this scope
   30 |     if(ptr == order.size()) return (-1);
      |               ^~~~~
a.cc:31:21: error: 'order' was not declared in this scope
   31 |     const auto id = order[ptr];
      |                     ^~~~~
a.cc:32:16: error: 'left' was not declared in this scope
   32 |     while(nl > left[id]) distribute(--nl);
      |                ^~~~
a.cc:32:26: error: 'distribute' was not declared in this scope
   32 |     while(nl > left[id]) distribute(--nl);
      |                          ^~~~~~~~~~
a.cc:33:16: error: 'right' was not declared in this scope
   33 |     while(nr < right[id]) distribute(nr++);
      |                ^~~~~
a.cc:33:27: error: 'distribute' was not declared in this scope
   33 |     while(nr < right[id]) distribute(nr++);
      |                           ^~~~~~~~~~
a.cc:34:16: error: 'left' was not declared in this scope
   34 |     while(nl < left[id]) distribute(nl++);
      |                ^~~~
a.cc:34:26: error: 'distribute' was not declared in this scope
   34 |     while(nl < left[id]) distribute(nl++);
      |                          ^~~~~~~~~~
a.cc:35:16: error: 'right' was not declared in this scope
   35 |     while(nr > right[id]) distribute(--nr);
      |                ^~~~~
a.cc:35:27: error: 'distribute' was not declared in this scope
   35 |     while(nr > right[id]) distribute(--nr);
      |                           ^~~~~~~~~~
a.cc: In function 'void distribute(int)':
a.cc:42:5: error: 'v' was not declared in this scope
   42 |     v[idx].flip();
      |     ^
a.cc:43:16: error: 'add' was not declared in this scope
   43 |     if(v[idx]) add(idx);
      |                ^~~
a.cc:44:10: error: 'del' was not declared in this scope
   44 |     else del(idx);
      |          ^~~
a.cc: At global scope:
a.cc:50:1: error: expected declaration before '}' token
   50 | };
      | ^
a.cc:54:6: error: 'Mo' is not a class, namespace, or enumeration
   54 | void Mo::add(int idx)
      |      ^~
a.cc: In function 'void add(int)':
a.cc:56:6: error: 'cnt' was not declared in this scope; did you mean 'int'?
   56 |   if(cnt[A[idx]]++ == 0) ++sum;
      |      ^~~
      |      int
a.cc:56:10: error: 'A' was not declared in this scope
   56 |   if(cnt[A[idx]]++ == 0) ++sum;
      |          ^
a.cc:56:28: error: 'sum' was not declared in this scope
   56 |   if(cnt[A[idx]]++ == 0) ++sum;
      |                            ^~~
a.cc: At global scope:
a.cc:59:6: error: 'Mo' is not a class, namespace, or enumeration
   59 | void Mo::del(int idx)
      |      ^~
a.cc: In function 'void del(int)':
a.cc:61:8: error: 'cnt' was not declared in this scope; did you mean 'int'?
   61 |   if(--cnt[A[idx]] == 0) --sum;
      |        ^~~
      |        int
a.cc:61:12: error: 'A' was not declared in this scope
   61 |   if(--cnt[A[idx]] == 0) --sum;
      |            ^
a.cc:61:28: error: 'sum' was not declared in this scope
   61 |   if(--cnt[A[idx]] == 0) --sum;
      |                            ^~~
a.cc: In function 'int main()':
a.cc:68:3: error: 'ios_base' has not been declared
   68 |   ios_base::sync_with_stdio(false);
      |   ^~~~~~~~
a.cc:69:3: error: 'cin' was not declared in this scope
   69 |   cin.tie(NULL);
      |   ^~~
a.cc:69:11: error: 'NULL' was not declared in this scope
   69 |   cin.tie(NULL);
      |           ^~~~
a.cc:1:1: note: 'NULL' is defined in header '<cstddef>'; this is probably fixable by adding '#include <cstddef>'
  +++ |+#include <cstddef>
    1 | 
a.cc:71:3: error: 'll' was not declared in this scope; did you mean 'nl'?
   71 |   ll n,q;
      |   ^~
      |   nl
a.cc:72:8: error: 'n' was not declared in this scope; did you mean 'nr'?
   72 |   cin>>n>>q;
      |        ^
      |        nr
a.cc:72:11: error: 'q' was not declared in this scope
   72 |   cin>>n>>q;
      |           ^
a.cc:74:7: error: 'i' was not declared in this scope
   74 |   REP(i,n)cin>>A[i];
      |       ^
a.cc:74:3: error: 'REP' was not declared in this scope
   74 |   REP(i,n)cin>>A[i];
      |   ^~~
a.cc:75:3: error: 'vll' was not declared in this scope
   75 |   vll l(q), r(q);
      |   ^~~
a.cc:82:5: error: expected ';' before 'mo'
   82 |   Mo mo(n);
      |     ^~~
      |     ;
a.cc:85:3: error: 'mo' was not declared in this scope; did you mean 'Mo'?
   85 |   mo.build();
      |   ^~
      |   Mo
a.cc:87:9: error: 'MAX_N' was not declared in this scope
   87 |   REP(i,MAX_N)cnt[i]=0;
      |         ^~~~~
a.cc:89:3: error: 'sum' was not declared in this scope
   89 |   sum=0;
      |   ^~~