#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <tuple>
#include <algorithm>
using namespace std;
const int MAXA = 100000;
const int MAXN = 100000;
const int MAXQ = 100000;
int a[MAXN+1];
typedef tuple<int, int, int, int> Query;
Query query[MAXQ];
inline void mo_algorithm(const int n, const int a[], const int q, tuple<int, int, int, int> query[])
{
const int block_size = (int)sqrt((long double)n);
const auto getLeft = [](const Query &q) {return get<0>(q); };
const auto getRight = [](const Query &q) {return get<1>(q); };
const auto getBlockIndex = [=](const Query &q) {return getLeft(q) / block_size; };
sort(query, query + q, [=](const Query &a, const Query &b) {
return
getBlockIndex(a) < getBlockIndex(b) ||
getBlockIndex(a) == getBlockIndex(b) && getRight(a) > getRight(b);
});
static int count[MAXA + 1];
memset(count, 0, sizeof(count));
set < pair < int, int > > count_value;
const auto remove = [&](const int index) {
count_value.erase(make_pair(count[a[index]], a[index]));
--count[a[index]];
count_value.insert(make_pair(count[a[index]], a[index]));
};
const auto add = [&](const int index) {
count_value.erase(make_pair(count[a[index]], a[index]));
++count[a[index]];
count_value.insert(make_pair(count[a[index]], a[index]));
};
int left = 0, right = -1;
for (int i = 0; i < q; ++i)
{
for (; left < getLeft(query[i]); ++left)
{
remove(left);
}
for (; left > getLeft(query[i]); )
{
add(--left);
}
for (; right < getRight(query[i]); )
{
add(++right);
}
for (; right > getRight(query[i]); --right)
{
remove(right);
}
get<3>(query[i]) = count_value.rbegin()->first;
}
sort(query, query + q, [=](const Query &a, const Query &b) {
return get<2>(a) < get<2>(b);
});
}
int main()
{
int n, q;
while (~scanf("%d%d", &n, &q))
{
for (int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}
for (int k = 0; k < q; ++k)
{
scanf("%d%d", &get<0>(query[k]), &get<1>(query[k]));
get<2>(query[k]) = k;
}
mo_algorithm(n, a, q, query);
for (int k = 0; k < q; ++k)
{
printf("%d\n", get<3>(query[k]));
}
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2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