#include <bits/stdc++.h>
using namespace std;
#define append push_back
// #define int long long
const int N=1e5+10,LG=17;
int mod=998244353;
int a[N],dp[N][LG];
void solve(int tst) {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i-1];
}
map<int,int> last;
for(int j=0;j<=n+2;j++) for(int i=0;i<LG;i++) dp[j][i]=n+1;
for(int i = n; i >= 1; i--) {
last[a[i]] = i;
if (last.count(a[i-1])){
dp[i][0] = min(dp[i+1][0],last[a[i-1]]);
} else {
dp[i][0] = (i == n ? n + 1 : dp[i+1][0]);
}
for(int j = 1; j < LG; j++) {
dp[i][j] = (dp[i][j-1] == n + 1 ? n + 1 : dp[dp[i][j-1] + 1][j-1]);
if(!dp[i][j]) dp[i][j]=n+1;
}
}
int q;
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
int ans = 0;
for(int i = LG - 1; i >= 0; i--) {
if(l>r) break;
if (dp[l][i] <= r && dp[l][i] != 0) {
ans += (1 << i);
l = dp[l][i] + 1;
}
}
cout << ans << endl;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) {
solve(i);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |