#include<bits/stdc++.h>
using namespace std;
#define N 50005
#define B 225
int F1[B+5][N],F2[B+5][N];
int n,m;
int a[N],sum[N],temp[N];
void precalc()
{
for(int i=0;i<B;i++){
int l=i*B;
if(l>n)
break;
for(int r=l;r<=n;r++)
temp[sum[r]]=-1;
int tmp=0;
for(int r=l;r<=n;r++){
if(temp[sum[r]]==-1){
temp[sum[r]]=r;
}
else
tmp=max(tmp,r-temp[sum[r]]);
F1[i][r]=tmp;
// printf("%d %d = %d\n",i,r,tmp);
}
}
for(int i=0;i<B;i++){
int r=i*B;
if(r>n)
break;
int tmp=0;
for(int l=0;l<r;l++){
temp[sum[l]]=-1;
}
for(int l=r-1;l>=0;l--){
if(temp[sum[l]]==-1)
temp[sum[l]]=l;
else
tmp=max(tmp,temp[sum[l]]-l);
F2[i][l]=tmp;
// printf("%d %d = %d\n",i,l,tmp);
}
}
}
int solve(int l,int r)
{
int ll=(l+B-1)/B;
int rr=r/B;
int ans=0;
if(ll<rr){
printf("%d %d\n",ll,rr);
ans=max(ans,max(F1[ll][r],F2[rr][l-1]));
printf("%d %d\n",F1[ll][r],F2[rr][l-1]);
for(int i=l-1;i<=ll*B;i++)
temp[sum[i]]=-1;
for(int i=l-1;i<=ll*B;i++){
if(temp[sum[i]]==-1){
temp[sum[i]]=i;
}
}
for(int i=r;i>=rr*B;i--){
if(temp[sum[i]]!=-1)
ans=max(ans,i-temp[sum[i]]);
}
}
else
{
for(int i=l-1;i<=r;i++)
temp[sum[i]]=-1;
int tmp=0;
for(int i=l-1;i<=r;i++){
if(temp[sum[i]]==-1)
temp[sum[i]]=i;
else
tmp=max(tmp,i-temp[sum[i]]);
}
ans=tmp;
}
return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output1.txt","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
precalc();
while(m--){
int l,r;
scanf("%d %d",&l,&r);
if(l>r)
swap(l,r);
int ans=solve(l,r);
printf("%d\n",ans);
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBOIDUwMDA1CiNkZWZpbmUgQiAyMjUKaW50IEYxW0IrNV1bTl0sRjJbQis1XVtOXTsKaW50IG4sbTsKaW50IGFbTl0sc3VtW05dLHRlbXBbTl07CnZvaWQgcHJlY2FsYygpCnsKICAgIGZvcihpbnQgaT0wO2k8QjtpKyspewogICAgICAgIGludCBsPWkqQjsKICAgICAgICBpZihsPm4pCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGZvcihpbnQgcj1sO3I8PW47cisrKQogICAgICAgICAgICB0ZW1wW3N1bVtyXV09LTE7CgogICAgICAgIGludCB0bXA9MDsKCiAgICAgICAgZm9yKGludCByPWw7cjw9bjtyKyspewogICAgICAgICAgICBpZih0ZW1wW3N1bVtyXV09PS0xKXsKICAgICAgICAgICAgICAgIHRlbXBbc3VtW3JdXT1yOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHRtcD1tYXgodG1wLHItdGVtcFtzdW1bcl1dKTsKICAgICAgICAgICAgRjFbaV1bcl09dG1wOwogICAgICAgICAgIC8vIHByaW50ZigiJWQgJWQgPSAlZFxuIixpLHIsdG1wKTsKICAgICAgICB9CiAgICB9CiAgICBmb3IoaW50IGk9MDtpPEI7aSsrKXsKICAgICAgICBpbnQgcj1pKkI7CiAgICAgICAgaWYocj5uKQogICAgICAgICAgICBicmVhazsKICAgICAgICBpbnQgdG1wPTA7CiAgICAgICAgZm9yKGludCBsPTA7bDxyO2wrKyl7CiAgICAgICAgICAgIHRlbXBbc3VtW2xdXT0tMTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBsPXItMTtsPj0wO2wtLSl7CiAgICAgICAgICAgIGlmKHRlbXBbc3VtW2xdXT09LTEpCiAgICAgICAgICAgICAgICB0ZW1wW3N1bVtsXV09bDsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgdG1wPW1heCh0bXAsdGVtcFtzdW1bbF1dLWwpOwogICAgICAgICAgICBGMltpXVtsXT10bXA7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZCAlZCA9ICVkXG4iLGksbCx0bXApOwogICAgICAgIH0KICAgIH0KfQppbnQgc29sdmUoaW50IGwsaW50IHIpCnsKICAgIGludCBsbD0obCtCLTEpL0I7CiAgICBpbnQgcnI9ci9COwogICAgaW50IGFucz0wOwogICAgaWYobGw8cnIpewogICAgIHByaW50ZigiJWQgJWRcbiIsbGwscnIpOwogICAgYW5zPW1heChhbnMsbWF4KEYxW2xsXVtyXSxGMltycl1bbC0xXSkpOwogICAgcHJpbnRmKCIlZCAlZFxuIixGMVtsbF1bcl0sRjJbcnJdW2wtMV0pOwogICAgZm9yKGludCBpPWwtMTtpPD1sbCpCO2krKykKICAgICAgICB0ZW1wW3N1bVtpXV09LTE7CiAgICBmb3IoaW50IGk9bC0xO2k8PWxsKkI7aSsrKXsKICAgICAgICBpZih0ZW1wW3N1bVtpXV09PS0xKXsKICAgICAgICAgICAgdGVtcFtzdW1baV1dPWk7CiAgICAgICAgfQogICAgfQogICAgZm9yKGludCBpPXI7aT49cnIqQjtpLS0pewogICAgICAgIGlmKHRlbXBbc3VtW2ldXSE9LTEpCiAgICAgICAgYW5zPW1heChhbnMsaS10ZW1wW3N1bVtpXV0pOwogICAgfQogICAgfQogICAgZWxzZQogICAgewogICAgICAgIGZvcihpbnQgaT1sLTE7aTw9cjtpKyspCiAgICAgICAgICAgIHRlbXBbc3VtW2ldXT0tMTsKICAgICAgICBpbnQgdG1wPTA7CiAgICAgICAgZm9yKGludCBpPWwtMTtpPD1yO2krKyl7CiAgICAgICAgICAgIGlmKHRlbXBbc3VtW2ldXT09LTEpCiAgICAgICAgICAgICAgICB0ZW1wW3N1bVtpXV09aTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgdG1wPW1heCh0bXAsaS10ZW1wW3N1bVtpXV0pOwogICAgICAgIH0KICAgICAgICBhbnM9dG1wOwogICAgfQogICAgcmV0dXJuIGFuczsKfQppbnQgbWFpbigpCnsKICAgIC8vZnJlb3BlbigiaW5wdXQudHh0IiwiciIsc3RkaW4pOwogICAgLy9mcmVvcGVuKCJvdXRwdXQxLnR4dCIsInciLHN0ZG91dCk7CiAgICBzY2FuZigiJWQgJWQiLCZuLCZtKTsKICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKXsKICAgICAgICBzY2FuZigiJWQiLCZhW2ldKTsKICAgICAgICBzdW1baV09c3VtW2ktMV0rYVtpXTsKICAgIH0KICAgIHByZWNhbGMoKTsKICAgIHdoaWxlKG0tLSl7CiAgICAgICAgaW50IGwscjsKICAgICAgICBzY2FuZigiJWQgJWQiLCZsLCZyKTsKICAgICAgICBpZihsPnIpCiAgICAgICAgICAgIHN3YXAobCxyKTsKICAgICAgICBpbnQgYW5zPXNvbHZlKGwscik7CiAgICAgICAgcHJpbnRmKCIlZFxuIixhbnMpOwogICAgfQp9Cg==