Language: C++

#include<iostream>
#include<cstdio>
int N, M, A[100000], ans[100000];
int reach[100001], sum[200001], now;

struct Mo
{
  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)
{
  reach[sum[A[idx]]]--;
  ++sum[A[idx]];
  reach[sum[A[idx]]]++;
  if(sum[A[idx]] > now) ++now;
}

void Mo::del(int idx)
{
  reach[sum[A[idx]]]--;
  if(sum[A[idx]] == now && reach[sum[A[idx]]] == 0) --now;
  --sum[A[idx]];
  reach[sum[A[idx]]]++;
}

int main()
{
  while(scanf("%d", &N), N) {
    memset(sum, 0, sizeof(sum));
    memset(reach, 0, sizeof(reach));
    now = 0;

    scanf("%d", &M);
    for(int i = 0; i < N; i++) {
      scanf("%d", &A[i]);
      A[i] += 100000;
    }
    Mo mo(N);
    for(int i = 0; i < M; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      mo.insert(--x, y);
    }
    mo.build();
    for(int i = 0; i < M; i++) {
      ans[mo.process()] = now;
    }
    for(int i = 0; i < M; i++) {
      printf("%d\n", ans[i]);
    }
  }
}


Compiler output:
a.cc:8:3: error: 'vector' does not name a type
    8 |   vector< int > left, right, order;
      |   ^~~~~~
a.cc:9:3: error: 'vector' does not name a type
    9 |   vector< bool > v;
      |   ^~~~~~
a.cc: In constructor 'Mo::Mo(int)':
a.cc:13:27: error: 'sqrt' was not declared in this scope
   13 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |                           ^~~~
a.cc:13:59: error: class 'Mo' does not have any field named 'v'
   13 |   Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}
      |                                                           ^
a.cc: In member function 'void Mo::insert(int, int)':
a.cc:17:5: error: 'left' was not declared in this scope; did you mean 'std::left'?
   17 |     left.push_back(l);
      |     ^~~~
      |     std::left
In file included from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/ios_base.h:1062:3: note: 'std::left' declared here
 1062 |   left(ios_base& __base)
      |   ^~~~
a.cc:18:5: error: 'right' was not declared in this scope; did you mean 'std::right'?
   18 |     right.push_back(r);
      |     ^~~~~
      |     std::right
/usr/include/c++/14/bits/ios_base.h:1070:3: note: 'std::right' declared here
 1070 |   right(ios_base& __base)
      |   ^~~~~
a.cc: In member function 'void Mo::build()':
a.cc:24:5: error: 'order' was not declared in this scope
   24 |     order.resize(left.size());
      |     ^~~~~
a.cc:24:18: error: 'left' was not declared in this scope; did you mean 'std::left'?
   24 |     order.resize(left.size());
      |                  ^~~~
      |                  std::left
/usr/include/c++/14/bits/ios_base.h:1062:3: note: 'std::left' declared here
 1062 |   left(ios_base& __base)
      |   ^~~~
a.cc:25:10: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   25 |     iota(begin(order), end(order), 0);
      |          ^~~~~
      |          std::begin
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:25:24: error: 'end' was not declared in this scope; did you mean 'std::end'?
   25 |     iota(begin(order), end(order), 0);
      |                        ^~~
      |                        std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:25:5: error: 'iota' was not declared in this scope
   25 |     iota(begin(order), end(order), 0);
      |     ^~~~
a.cc: In lambda function:
a.cc:29:14: error: 'right' was not declared in this scope; did you mean 'std::right'?
   29 |       return right[a] < right[b];
      |              ^~~~~
      |              std::right
/usr/include/c++/14/bits/ios_base.h:1070:3: note: 'std::right' declared here
 1070 |   right(ios_base& __base)
      |   ^~~~~
a.cc: In member function 'void Mo::build()':
a.cc:26:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   26 |     sort(begin(order), end(order), [&](int a, int b)
      |     ^~~~
      |     short
a.cc: In member function 'int Mo::process()':
a.cc:36:15: error: 'order' was not declared in this scope
   36 |     if(ptr == order.size()) return (-1);
      |               ^~~~~
a.cc:37:21: error: 'order' was not declared in this scope
   37 |     const auto id = order[ptr];
      |                     ^~~~~
a.cc:38:16: error: 'left' was not declared in this scope; did you mean 'std::left'?
   38 |     while(nl > left[id]) distribute(--nl);
      |                ^~~~
      |                std::left
/usr/include/c++/14/bits/ios_base.h:1062:3: note: 'std::left' declared here
 1062 |   left(ios_base& __base)
      |   ^~~~
a.cc:39:16: error: 'right' was not declared in this scope; did you mean 'std::right'?
   39 |     while(nr < right[id]) distribute(nr++);
      |                ^~~~~
      |                std::right
/usr/include/c++/14/bits/ios_base.h:1070:3: note: 'std::right' declared here
 1070 |   right(ios_base& __base)
      |   ^~~~~
a.cc:40:16: error: 'left' was not declared in this scope; did you mean 'std::left'?
   40 |     while(nl < left[id]) distribute(nl++);
      |                ^~~~
      |                std::left
/usr/include/c++/14/bits/ios_base.h:1062:3: note: 'std::left' declared here
 1062 |   left(ios_base& __base)
      |   ^~~~
a.cc:41:16: error: 'right' was not declared in this scope; did you mean 'std::right'?
   41 |     while(nr > right[id]) distribute(--nr);
      |                ^~~~~
      |                std::right
/usr/include/c++/14/bits/ios_base.h:1070:3: note: 'std::right' declared here
 1070 |   right(ios_base& __base)
      |   ^~~~~
a.cc: In member function 'void Mo::distribute(int)':
a.cc:47:5: error: 'v' was not declared in this scope
   47 |     v[idx].flip();
      |     ^
a.cc: In function 'int main()':
a.cc:76:5: error: 'memset' was not declared in this scope
   76 |     memset(sum, 0, sizeof(sum));
      |     ^~~~~~
a.cc:3:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    2 | #include<cstdio>
  +++ |+#include <cstring>
    3 | int N, M, A[100000], ans[100000];