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;
const int nx=4e5+5, kx=19;
#define ll long long
int n, q, a[nx], l, r, dp[nx][kx];
map<ll, int> mp;
ll qs;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n;i ++) cin>>a[i];
mp[0]=1;
for (int i=1; i<=n; i++)
{
qs+=a[i];
if (mp.find(qs)!=mp.end()) dp[i][0]=mp[qs];
mp[qs]=i+1;
}
for (int i=1; i<=n; i++) dp[i][0]=max(dp[i][0], dp[i-1][0]); //cout<<"dp "<<i<<' '<<dp[i][0]<<'\n';
for (int i=1; i<kx; i++) for (int j=1; j<=n; j++) dp[j][i]=dp[max(0, dp[j][i-1]-1)][i-1];
//for (int i=0; i<=3; i++) for (int j=1; j<=n; j++) cout<<"debug "<<i<<' '<<j<<' '<<dp[j][i]<<'\n';
cin>>q;
while (q--)
{
cin>>l>>r;
int res=0;
for (int i=kx-1; i>=0; i--)
{
if (dp[r][i]>=l) res+=(1<<i), r=dp[r][i]-1;
}
cout<<res<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |