This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n,q;
int a[400005];
map<long long,int>dd;
int l[400005];
int nxt[400005];
int pa[20][100005];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)l[i]=n+1;
long long sum=0;
dd[0]=n;
for(int i=n;i>=1;i--)
{
sum+=a[i];
if(dd.count(sum))
{
l[i]=dd[sum];
}
dd[sum]=i-1;
}
int pos=n+1;
int minn=2e9;
for(int i=n;i>=1;i--)
{
if(minn>=l[i])
{
minn=l[i];
pos=i;
}
nxt[i]=pos;
}
for(int i=1;i<=n;i++)
{
pa[0][i]=l[nxt[i]]+1;
}
for(int i=1;i<=18;i++)
for(int j=1;j<=n;j++)
{
pa[i][j]=pa[i-1][pa[i-1][j]];
}
cin>>q;
while(q--)
{
int l,r;
cin>>l>>r;
int ans=0;
for(int i=18;i>=0;i--)
{
if(pa[i][l]!=0&&pa[i][l]-1<=r)
{
ans+=(1<<i);l=pa[i][l];
}
}
cout<<ans<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |