#include<bits/stdc++.h>
using namespace std;
#define author rajat1603
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define N 100002
#define SN 400000
#define SQN 327
inline int scan( ) {
char c = getchar_unlocked( ) ;
int x = 0 ;
bool b= 0 ;
while ( c< '0' || c> '9' ) {
if ( c== '-' ) {
b= 1 ;
}
c= getchar_unlocked( ) ;
}
while ( c>= '0' && c<= '9' ) {
x= ( x<< 1 ) + ( x<< 3 ) + c- '0' ;
c= getchar_unlocked( ) ;
}
if ( b) {
return - x;
}
return x;
}
#define index second.second.second
#define lef second.second.first
#define rig second.first
#define blok first
int n,m= 100002 ,k;
int leftmost[ N] = { 0 } ,rightmost[ N] = { 0 } ,nextright[ N] = { 0 } ,nextleft[ N] = { 0 } ,templeft[ N] ,tempright[ N] ;
int sqn;
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 arr[ N] ;
int segtree[ SN] ;
int ans[ N] ;
int curl= 1 ,curr= 0 ;
int posintree[ N] ;
void build( int l,int r,int node) {
if ( l== r) {
posintree[ l] = node;
return ;
}
int lc= node<< 1 ;
int rc= lc| 1 ;
int mid = ( l+ r) >> 1 ;
build( l,mid,lc) ;
build( mid+ 1 ,r,rc) ;
}
void update( int inde,int val) {
int x= posintree[ inde] ;
segtree[ x] = val;
while ( x) {
x>>= 1 ;
int lc= x<< 1 ;
int rc= lc| 1 ;
segtree[ x] = max( segtree[ lc] ,segtree[ rc] ) ;
}
}
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<<endl;
//3,5 to 5,6 , 4 5 6 6 5 7 4
while ( curr> r) {
int val= arr[ curr] ;
rightmost[ val] = nextleft[ curr] ;
if ( nextleft[ curr] == 0 ) {
rightmost[ val] = curr;
}
update( val,rightmost[ val] - leftmost[ val] ) ;
curr-- ;
}
while ( r> curr) {
curr++ ;
int val= arr[ curr] ;
rightmost[ val] = curr;
if ( leftmost[ val] == 0 ) {
leftmost[ val] = curr;
}
int nmax = rightmost[ val] - leftmost[ val] ;
update( val,nmax) ;
//cout<<"curr++ "<<val<<" "<<nmax<<endl;
}
while ( l> curl) {
int val= arr[ curl] ;
leftmost[ val] = nextright[ curl] ;
if ( nextright[ curl] == 0 ) {
rightmost[ val] = 0 ;
}
update( val,rightmost[ val] - leftmost[ val] ) ;
curl++ ;
}
while ( l< curl) {
curl-- ;
int val= arr[ curl] ;
leftmost[ val] = curl;
if ( rightmost[ val] == 0 ) {
rightmost[ val] = curl;
}
update( val,rightmost[ val] - leftmost[ val] ) ;
// cout<<"curl-- "<<val<<" "<<nmax<<endl;
}
ans[ indice] = segtree[ 1 ] ;
if ( ans[ indice] < 0 ) {
ans[ indice] = 0 ;
}
}
int main( ) {
arr[ 1 ] = 50001 ;
n= scan( ) ,k= scan( ) ;
sqn= sqrt ( n+ 1 ) ;
build( 1 ,100002 ,1 ) ;
for ( int i= 1 ; i<= n; i++ ) {
arr[ i+ 1 ] = arr[ i] + scan( ) ;
}
n++ ;
for ( int i= 1 ; i<= n; i++ ) {
int cur = arr[ i] ;
nextleft[ i] = templeft[ cur] ;
templeft[ cur] = i;
}
for ( int i= n; i>= 1 ; i-- ) {
int cur= arr[ i] ;
nextright[ i] = tempright[ cur] ;
tempright[ cur] = i;
}
int ma= 0 ;
for ( int i= 1 ; i<= k; i++ ) {
int l= scan( ) ,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<= k; i++ ) {
printf ( "%d\n " ,ans[ i] ) ;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4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pcnN0IHdpbGwgaGF2ZSByaWdodCAsIHNlY29uZC5zZWNvbmQuZmlyc3Qgd2lsbCBoYXZlIGxlZnQgYW5kIHNlY29uZC5zZWNvbmQuc2Vjb25kIHdpbGwgaGF2ZSBpbmRleAppbnQgZnJlcVtOXT17MH07CmludCBhcnJbTl07CmludCBzZWd0cmVlW1NOXTsKaW50IGFuc1tOXTsKaW50IGN1cmw9MSxjdXJyPTA7CmludCBwb3NpbnRyZWVbTl07CnZvaWQgYnVpbGQoaW50IGwsaW50IHIsaW50IG5vZGUpewogICAgaWYobD09cil7CiAgICAgICAgcG9zaW50cmVlW2xdPW5vZGU7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaW50IGxjPW5vZGU8PDE7CiAgICBpbnQgcmM9bGN8MTsKICAgIGludCBtaWQgPSAobCtyKT4+MTsKICAgIGJ1aWxkKGwsbWlkLGxjKTsKICAgIGJ1aWxkKG1pZCsxLHIscmMpOwp9CnZvaWQgdXBkYXRlKGludCBpbmRlLGludCB2YWwpewogICAgaW50IHg9cG9zaW50cmVlW2luZGVdOwogICAgc2VndHJlZVt4XT12YWw7CiAgICB3aGlsZSh4KXsKICAgICAgICB4Pj49MTsKICAgICAgICBpbnQgbGM9eDw8MTsKICAgICAgICBpbnQgcmM9bGN8MTsKICAgICAgICBzZWd0cmVlW3hdPW1heChzZWd0cmVlW2xjXSxzZWd0cmVlW3JjXSk7CiAgICB9Cn0Kdm9pZCBwcm9jZXNzKHBhaXI8aW50LHBhaXI8aW50LHBhaXI8aW50LGludD4gPiA+IGEpewogICAgaW50IGw9LWEubGVmOwogICAgaW50IHI9LWEucmlnOwogICAgaW50IGluZGljZT1hLmluZGV4OwogICAgLy9jb3V0PDxsPDwiICI8PHI8PGVuZGw7CiAgICAvLzMsNSB0byA1LDYgLCA0IDUgNiA2IDUgNyA0CiAgICB3aGlsZShjdXJyPnIpewogICAgICAgIGludCB2YWw9YXJyW2N1cnJdOwogICAgICAgIHJpZ2h0bW9zdFt2YWxdPW5leHRsZWZ0W2N1cnJdOwogICAgICAgIGlmKG5leHRsZWZ0W2N1cnJdPT0wKXsKICAgICAgICAgICAgcmlnaHRtb3N0W3ZhbF09Y3VycjsKICAgICAgICB9CiAgICAgICAgdXBkYXRlKHZhbCxyaWdodG1vc3RbdmFsXS1sZWZ0bW9zdFt2YWxdKTsKICAgICAgICBjdXJyLS07CiAgICB9CiAgICB3aGlsZShyPmN1cnIpewogICAgICAgIGN1cnIrKzsKICAgICAgICBpbnQgdmFsPWFycltjdXJyXTsKICAgICAgICByaWdodG1vc3RbdmFsXT1jdXJyOwogICAgICAgIGlmKGxlZnRtb3N0W3ZhbF09PTApewogICAgICAgICAgICBsZWZ0bW9zdFt2YWxdPWN1cnI7CiAgICAgICAgfQogICAgICAgIGludCBubWF4ID0gcmlnaHRtb3N0W3ZhbF0tbGVmdG1vc3RbdmFsXTsKICAgICAgICB1cGRhdGUodmFsLG5tYXgpOwogICAgICAgIC8vY291dDw8ImN1cnIrKyAiPDx2YWw8PCIgIjw8bm1heDw8ZW5kbDsKICAgIH0KICAgIHdoaWxlKGw+Y3VybCl7CiAgICAgICAgaW50IHZhbD1hcnJbY3VybF07CiAgICAgICAgbGVmdG1vc3RbdmFsXT1uZXh0cmlnaHRbY3VybF07CiAgICAgICAgaWYobmV4dHJpZ2h0W2N1cmxdPT0wKXsKICAgICAgICAgICAgcmlnaHRtb3N0W3ZhbF09MDsKICAgICAgICB9CiAgICAgICAgdXBkYXRlKHZhbCxyaWdodG1vc3RbdmFsXS1sZWZ0bW9zdFt2YWxdKTsKICAgICAgICBjdXJsKys7CiAgICB9CiAgICB3aGlsZShsPGN1cmwpewogICAgICAgIGN1cmwtLTsKICAgICAgICBpbnQgdmFsPWFycltjdXJsXTsKICAgICAgICBsZWZ0bW9zdFt2YWxdPWN1cmw7CiAgICAgICAgaWYocmlnaHRtb3N0W3ZhbF09PTApewogICAgICAgICAgICByaWdodG1vc3RbdmFsXT1jdXJsOwogICAgICAgIH0KICAgICAgICB1cGRhdGUodmFsLHJpZ2h0bW9zdFt2YWxdLWxlZnRtb3N0W3ZhbF0pOwogICAgICAgLy8gY291dDw8ImN1cmwtLSAiPDx2YWw8PCIgIjw8bm1heDw8ZW5kbDsKICAgIH0gCiAgICBhbnNbaW5kaWNlXT1zZWd0cmVlWzFdOwogICAgaWYoYW5zW2luZGljZV08MCl7CiAgICAgICAgYW5zW2luZGljZV09MDsKICAgIH0KfQppbnQgbWFpbigpewogICAgYXJyWzFdPTUwMDAxOwogICAgbj1zY2FuKCksaz1zY2FuKCk7CiAgICBzcW49c3FydChuKzEpOwogICAgYnVpbGQoMSwxMDAwMDIsMSk7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgYXJyW2krMV09YXJyW2ldK3NjYW4oKTsKICAgIH0KICAgIG4rKzsKICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKXsKICAgICAgICBpbnQgY3VyID0gYXJyW2ldOwogICAgICAgIG5leHRsZWZ0W2ldPXRlbXBsZWZ0W2N1cl07CiAgICAgICAgdGVtcGxlZnRbY3VyXT1pOwogICAgfQogICAgZm9yKGludCBpPW47aT49MTtpLS0pewogICAgICAgIGludCBjdXI9YXJyW2ldOwogICAgICAgIG5leHRyaWdodFtpXT10ZW1wcmlnaHRbY3VyXTsKICAgICAgICB0ZW1wcmlnaHRbY3VyXT1pOwogICAgfQogICAgaW50IG1hPTA7CiAgICBmb3IoaW50IGk9MTtpPD1rO2krKyl7CiAgICAgICAgaW50IGw9c2NhbigpLHI9c2NhbigpKzE7CiAgICAgICAgaW50IHBvcz1sL3NxbjsKICAgICAgICBwYWlyPGludCxwYWlyPGludCxwYWlyPGludCxpbnQ+ID4gPiB0ZW1wOwogICAgICAgIHRlbXAuYmxvayA9IC1wb3M7CiAgICAgICAgdGVtcC5yaWcgPSAtcjsKICAgICAgICB0ZW1wLmxlZiA9IC1sOwogICAgICAgIHRlbXAuaW5kZXggPSBpOwogICAgICAgIFBRLnB1c2godGVtcCk7CiAgICB9CiAgICB3aGlsZSghUFEuZW1wdHkoKSl7CiAgICAgICAgcGFpcjxpbnQscGFpcjxpbnQscGFpcjxpbnQsaW50PiA+ID4gIHggPSBQUS50b3AoKTsKICAgICAgICBQUS5wb3AoKTsKICAgICAgICBwcm9jZXNzKHgpOwogICAgfQogICAgZm9yKGludCBpPTE7aTw9aztpKyspewogICAgICAgIHByaW50ZigiJWRcbiIsYW5zW2ldKTsKICAgIH0KfSAgICAgIA==