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;
using ll=long long;
const int N=4e5+10, LG=9;
int n;
int *a, *nxt;
pair<ll, int> *pf;
int jump[LG][N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
a=new int[n+1];
nxt=new int[n+1];
pf=new pair<ll, int>[n+1];
memset(nxt, -1, (n+1)*(sizeof(int)));
pf[0]={0, 0};
for (int i=1; i<=n; ++i) cin >> a[i], pf[i]={pf[i-1].first+a[i], i};
sort(pf, pf+n+1);
for (int i=0; i<=n; ++i){
int j=i;
while (j<n && pf[j+1].first==pf[i].first) ++j;
for (int k=i; k<j; ++k) nxt[pf[k].second+1]=pf[k+1].second;
i=j;
}
delete[] a;
delete[] pf;
int mn=n+1;
jump[0][n+1]=n+1;
for (int i=n; i>=0; --i){
jump[0][i]=mn;
if (nxt[i]!=-1) mn=min(mn, nxt[i]);
}
delete[] nxt;
for (int k=1; k<LG; ++k) for (int i=0; i<=n+1; ++i) jump[k][i]=jump[k-1][jump[k-1][i]];
int q; cin >> q;
while (q--){
int x, y; cin >> x >> y;
int ans=0, cur=x-1;
for (int i=LG-1; i>=0; --i){
while (jump[i][cur]<=y){
cur=jump[i][cur];
ans+=1<<i;
}
}
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... |