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;
map <long long,int> last;
int best[100001][17];
long long s[100001];
int logue[100001];
int main()
{
int n,x,i,j;
cin>>n;
best[0][0]=-1;
for(i=2;i<=n;i++)
logue[i]=logue[i/2]+1;
for(i=1;i<=n;i++)
{
cin>>x;
s[i]=s[i-1]+x;
best[i][0]=best[i-1][0];
if(s[i]==0||last[s[i]])
best[i][0]=max(last[s[i]],best[i][0]);
last[s[i]]=i;
}
for(j=1;j<=logue[n];j++)
for(i=0;i<=n;i++)
if(best[i][j-1]!=-1)
best[i][j]=best[best[i][j-1]][j-1];
else
best[i][j]=-1;
int q,a,b;
cin>>q;
for(i=1;i<=q;i++)
{
cin>>a>>b;
if(best[b][0]==-1)
{
cout<<"0\n";
continue;
}
int rez=0;
if(s[b]!=s[best[b][0]])
{
b=best[b][0];
rez=1;
}
if(b<a-1)
{
cout<<"0\n";
continue;
}
int start=b,pas=logue[n];
while(pas!=-1)
{
if(best[start][pas]>=a-1)
{
rez+=(1<<pas);
start=best[start][pas];
}
pas--;
}
cout<<rez<<'\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... |