#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
//#define int long long
#define pb push_back
#define vi vector<int>
#define ff first
#define ss second
using namespace std;
const int N=4e5+5;
int nxt[N],ans[N],a[N],q[N],q1[N];
int n,Q,i,k,j;
ll sum;
unordered_map<ll,int>mp;
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(NULL);
cin>>n;
for(i=1;i<=n;i++)cin>>a[i];
mp[0]=n+1;
nxt[n+1]=n+2;
nxt[n+2]=n+2;
for(i=n;i>=1;i--){
sum+=a[i];
if(mp.find(sum)==mp.end())nxt[i]=nxt[i+1];
else nxt[i]=min(mp[sum],nxt[i+1]);
mp[sum]=i;
}
cin>>Q;
for(int i=0;i<Q;i++)cin>>q[i]>>q1[i];
for(k=18;k>=0;k--){
for(i=1;i<=n+2;i++)a[i]=nxt[i];
for(j=1;j<=k;j++){
for(i=1;i<=n+2;i++)a[i]=a[a[i]];
}
for(i=0;i<Q;i++){
if(a[q[i]]<=q1[i]+1){
ans[i]+=(1<<k);
q[i]=a[q[i]];
}
}
}
for(i=0;i<Q;i++)cout<<ans[i]<<"\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... |