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[nx], r[nx], vl[nx], dp[2][nx], res[nx];
unordered_map<ll, int> mp;
ll qs;
void build(int x)
{
for (int i=1; i<=n; i++) dp[0][i]=vl[i];
for (int i=1; i<=x; i++)
{
int c=i%2, p=1-c;
for (int j=1; j<=n; j++) dp[c][j]=dp[p][max(dp[p][j]-1, 0)];
}
}
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()) vl[i]=mp[qs];
mp[qs]=i+1;
}
mp.clear();
for (int i=1; i<=n; i++) vl[i]=max(vl[i], vl[i-1]);
cin>>q;
for (int i=1; i<=q; i++) cin>>l[i]>>r[i];
for (int i=kx-1; i>=0; i--)
{
build(i);
for (int j=1; j<=q; j++) if (dp[i%2][r[j]]>=l[j]) r[j]=dp[i%2][r[j]]-1, res[j]+=(1<<i);
}
for (int i=1; i<=q; i++) cout<<res[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... |