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;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=4e5+9;
pli a[maxn];
int nxt[maxn];
int st[maxn][21];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n;
cin>>n;
for1(i,1,n){
cin>>a[i].fi;
a[i].fi+=a[i-1].fi;
a[i].se=i;
}
sort(a,a+1+n);
for1(i,0,n){
nxt[a[i].se+1]=n+1;
if (i<n&&a[i].fi==a[i+1].fi){
nxt[a[i].se+1]=a[i+1].se;
}
}
for2(i,n-1,1)nxt[i]=min(nxt[i],nxt[i+1]);
for1(i,1,n)st[i][0]=nxt[i];
for1(j,1,20){
for1(i,1,n){
if (st[i][j-1]+1>n){
st[i][j]=n+1;
continue;
}
st[i][j]=st[st[i][j-1]+1][j-1];
}
}
int q;
cin>>q;
for1(i,1,q){
int l,r;
cin>>l>>r;
int ans=0;
for2(j,20,0){
if (l>r)break;
if (st[l][j]<=r){
ans+=(1<<j);
l=st[l][j]+1;
}
}
cout<<ans<<'\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... |