#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define fi first
#define sec second
#define ld long double
const int MAXN = 1e5;
const ll INF = 4e18;
const int MOD = 1e9 + 7;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for(;tc--;){
ll N; cin >> N;
vector<ll> a(N + 5), ps(N + 5);
for(int i = 1; i <= N; i++){
cin >> a[i];
ps[i] = ps[i - 1] + a[i];
}
vector<vector<ll>> up(N + 5, vector<ll>(20));
map<ll, ll> mp;
vector<ll> dp(N + 5, N + 1);
for(int i = N; i >= 0; --i){
dp[i] = dp[i + 1];
if(mp.count(ps[i])){
dp[i] = min(dp[i], mp[ps[i]]);
}
mp[ps[i]] = i;
up[i][0] = dp[i];
}
for(int LOG = 0; LOG < 20; LOG++){
up[N + 1][LOG] = N + 1;
}
for(int LOG = 1; LOG < 20; LOG++){
for(int i = 0; i <= N; i++){
up[i][LOG] = up[up[i][LOG - 1]][LOG - 1];
}
}
ll Q; cin >> Q;
for(int i = 1; i <= Q; i++){
ll l, r; cin >> l >> r;
ll cur = l - 1, ans = 0;
for(int LOG = 19; LOG >= 0; --LOG){
if(up[cur][LOG] <= r){
ans += (1LL << LOG);
cur = up[cur][LOG];
}
}
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... |