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>
#include<random>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)
using namespace std;
const ll N = 400009;
ll n , q;
ll a[N];
ll p[N];
map<ll,ll> mp;
ll idx[N];
ll id[N];
ll b[N][20];
int main()
{
d3
cin >> n;
for(int i = 1 ; n>=i ; i++)
{
cin >> a[i];
p[i]=p[i-1]+a[i];
}
for(int i = 1 ; n>=i ; i++)
id[i]=n+1;
mp[0]=0;
for(ll i = 1 ; n>=i ; i++)
{
idx[i]=-1;
if(mp[p[i]]==0&&p[i]!=0)
{
mp[p[i]]=i;
continue;
}
idx[i]=mp[p[i]]+1;
mp[p[i]]=i;
id[idx[i]]=min(id[idx[i]],i);
}
ll mn = n+1;
for(int i = n ; i>0 ; i--)
{
mn=min(mn,id[i]);
b[i][0]=mn;
}
for(int j = 0 ; 20>j ; j++)
b[n+1][j]=n+1;
for(int j = 1 ; 20>j ; j++)
{
for(int i = 1 ; n>=i ; i++)
{
b[i][j]=b[b[i][j-1]+1][j-1];
if(b[i][j]==0)
b[i][j]=n+1;
}
}
cin >> q;
while(q--)
{
ll l , r;
cin >> l >> r;
ll ans = 0;
ll i = l;
for(int j = 19 ; j>=0 ; j--)
{
if(i>r)
break;
if(b[i][j]>r)
continue;
ans+=(1<<j);
i=b[i][j]+1;
}
cans
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |