#include<bits/stdc++.h>
using namespace std;
#define author rajat1603
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define N 110000
inline int scan( ) {
char c = getchar_unlocked( ) ;
int x = 0 ;
while ( c< '0' || c> '9' ) {
c= getchar_unlocked( ) ;
}
while ( c>= '0' && c<= '9' ) {
x= ( x<< 1 ) + ( x<< 3 ) + c- '0' ;
c= getchar_unlocked( ) ;
}
return x;
}
#define index second.second.second
#define lef second.second.first
#define rig second.first
#define blok first
int n,q;
priority_queue< pair< int ,pair< int ,pair< int ,int > > > > PQ; //first will have l/sqn , second.first will have right , second.second.first will have left and second.second.second will have index
int freq[ N] = { 0 } ;
int counter[ N] = { 0 } ;
int arr[ N] ;
int ans[ N] = { 0 } ;
int curl= 1 ,curr= 0 ;
int tempans= 0 ;
void process( pair< int ,pair< int ,pair< int ,int > > > a) {
int l= - a.lef ;
int r= - a.rig ;
int indice= a.index ;
// cout<<l<<" "<<r<<" "<<indice<<endl;
//1 1 3 3 3 3 5 12 12 12 , 2 ,3
while ( r> curr) { //add
curr++ ;
int val= arr[ curr] ;
int c= freq[ val] ;
counter[ c] -- ;
freq[ val] ++ ;
counter[ freq[ val] ] ++ ;
tempans= max( tempans,freq[ val] ) ;
// cout<<"r add ";
// cout<<val<<" "<<freq[val]<<" "<<counter[freq[val]]<<" "<<tempans<<endl;
}
while ( l> curl) { //remove
int val= arr[ curl] ;
counter[ freq[ val] ] -- ;
freq[ val] -- ;
counter[ freq[ val] ] ++ ;
curl++ ;
while ( counter[ tempans] == 0 ) {
tempans-- ;
}
// cout<<"l remove ";
// cout<<val<<" "<<freq[val]<<" "<<counter[freq[val]]<<" "<<tempans<<endl;
}
while ( l< curl) { //add
curl-- ;
int val = arr[ curl] ;
int c = freq[ val] ;
counter[ c] -- ;
freq[ val] ++ ;
counter[ freq[ val] ] ++ ;
tempans= max( tempans,freq[ val] ) ;
// cout<<"l add ";
// cout<<val<<" "<<freq[val]<<" "<<counter[freq[val]]<<" "<<tempans<<endl;
}
while ( r< curr) { //remove
int val= arr[ curr] ;
counter[ freq[ val] ] -- ;
freq[ val] -- ;
counter[ freq[ val] ] ++ ;
curr-- ;
while ( counter[ tempans] == 0 ) {
tempans-- ;
}
// cout<<"r remove ";
// cout<<val<<" "<<freq[val]<<" "<<counter[freq[val]]<<" "<<tempans<<endl;
}
ans[ indice] = tempans;
//cout<<l<<" "<<r<<" "<<counter[tempans]<<endl;
}
int main( ) {
n= scan( ) ;
int sqn= sqrt ( n) ;
counter[ 0 ] = n;
q= scan( ) ;
for ( int i= 1 ; i<= n; i++ ) {
arr[ i] = scan( ) ;
}
for ( int i= 1 ; i<= q; i++ ) {
int l= scan( ) + 1 ,r= scan( ) + 1 ;
int pos= l/ sqn;
pair< int ,pair< int ,pair< int ,int > > > temp;
temp.blok = - pos;
temp.rig = - r;
temp.lef = - l;
temp.index = i;
PQ.push ( temp) ;
}
while ( ! PQ.empty ( ) ) {
pair< int ,pair< int ,pair< int ,int > > > x = PQ.top ( ) ;
PQ.pop ( ) ;
process( x) ;
}
for ( int i= 1 ; i<= q; i++ ) {
printf ( "%d\n " ,ans[ i] ) ;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBhdXRob3IgcmFqYXQxNjAzCiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBOIDExMDAwMAppbmxpbmUgaW50IHNjYW4oKXsKICAgIGNoYXIgYyA9IGdldGNoYXJfdW5sb2NrZWQoKTsKICAgIGludCB4ID0gMDsKICAgIHdoaWxlKGM8JzAnfHxjPic5Jyl7CiAgICAgICAgYz1nZXRjaGFyX3VubG9ja2VkKCk7CiAgICB9CiAgICB3aGlsZShjPj0nMCcmJmM8PSc5Jyl7CiAgICAgICAgeD0oeDw8MSkrKHg8PDMpK2MtJzAnOwogICAgICAgIGM9Z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hcnJbY3Vycl07CiAgICAgICAgY291bnRlcltmcmVxW3ZhbF1dLS07CiAgICAgICAgZnJlcVt2YWxdLS07CiAgICAgICAgY291bnRlcltmcmVxW3ZhbF1dKys7CiAgICAgICAgY3Vyci0tOwogICAgICAgIHdoaWxlKGNvdW50ZXJbdGVtcGFuc109PTApewogICAgICAgICAgICB0ZW1wYW5zLS07CiAgICAgICAgfQogICAgICAgLy8gY291dDw8InIgcmVtb3ZlICI7CiAgICAgICAvLyBjb3V0PDx2YWw8PCIgIjw8ZnJlcVt2YWxdPDwiICI8PGNvdW50ZXJbZnJlcVt2YWxdXTw8IiAiPDx0ZW1wYW5zPDxlbmRsOwogICAgfQogICAgYW5zW2luZGljZV09dGVtcGFuczsKICAgIC8vY291dDw8bDw8IiAiPDxyPDwiICI8PGNvdW50ZXJbdGVtcGFuc108PGVuZGw7Cn0KaW50IG1haW4oKXsKICAgIG49c2NhbigpOwogICAgaW50IHNxbj1zcXJ0KG4pOwogICAgY291bnRlclswXT1uOwogICAgcT1zY2FuKCk7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgYXJyW2ldPXNjYW4oKTsKICAgIH0KICAgIGZvcihpbnQgaT0xO2k8PXE7aSsrKXsKICAgICAgICBpbnQgbD1zY2FuKCkrMSxyPXNjYW4oKSsxOwogICAgICAgIGludCBwb3M9bC9zcW47CiAgICAgICAgcGFpcjxpbnQscGFpcjxpbnQscGFpcjxpbnQsaW50PiA+ID4gdGVtcDsKICAgICAgICB0ZW1wLmJsb2sgPSAtcG9zOwogICAgICAgIHRlbXAucmlnID0gLXI7CiAgICAgICAgdGVtcC5sZWYgPSAtbDsKICAgICAgICB0ZW1wLmluZGV4ID0gaTsKICAgICAgICBQUS5wdXNoKHRlbXApOwogICAgfQogICAgd2hpbGUoIVBRLmVtcHR5KCkpewogICAgICAgIHBhaXI8aW50LHBhaXI8aW50LHBhaXI8aW50LGludD4gPiA+ICB4ID0gUFEudG9wKCk7CiAgICAgICAgUFEucG9wKCk7CiAgICAgICAgcHJvY2Vzcyh4KTsKICAgIH0KICAgIGZvcihpbnQgaT0xO2k8PXE7aSsrKXsKICAgICAgICBwcmludGYoIiVkXG4iLGFuc1tpXSk7CiAgICB9Cn0gICAgICA=