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>
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxn=4e5+5;
const ll inf=1e18;
const ll mod=1e9+7;
ll n;
pair<ll,int> a[maxn];
int nxt[maxn],mn[maxn],p[maxn],jump[maxn];
const ll block=630;
void solve()
{
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i].fi,a[i].fi+=a[i-1].fi,a[i].se=i;
sort(a,a+n+1);
for(int i=0;i<=n;i++)
{
if(i<n&&a[i].fi==a[i+1].fi) nxt[a[i].se+1]=a[i+1].se;
else nxt[a[i].se+1]=n+1;
}
nxt[n+1]=n+1;
mn[n+1]=n+1;
for(int i=n;i>=1;i--)
{
if(nxt[i]<nxt[mn[i+1]]) mn[i]=i;
else mn[i]=mn[i+1];
}
for(int i=n;i>=1;i--)
{
ll u=nxt[i];
if(u==n+1) p[i]=n+1;
else u++,p[i]=mn[u];
}
p[n+1]=n+1;
for(int i=1;i<=n;i++)
{
ll cnt=0;
ll u=i;
while(cnt<block)
{
u=p[u];
cnt++;
if(u==n+1) break;
}
jump[i]=u;
}
ll q;
cin >> q;
for(int i=1;i<=q;i++)
{
int l,r;
cin >> l >> r;
l=mn[l];
if(nxt[l]>r) {cout << 0 << '\n';continue;}
else
{
ll ans=1;
while(nxt[jump[l]]<=r)
{
ans+=block;
l=jump[l];
}
while(nxt[p[l]]<=r)
{
ans++;
l=p[l];
}
cout << ans << '\n';
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |