#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
//#define int long long
#define pii pair<int,int>
#define vi vector<int>
#define ff first
#define ss second
#define sp << " " <<
#define all(x) x.begin(),x.end()
#define big(x) ((int)(x.size()))
using namespace std;
const int MOD = 1e9+7, LIM = 1e6+1, inf = 2e18;
const int N = 1e5+1;
const int L = 19;
void solve() {
int n;
cin >> n;
vi a(n+1);
for (int i=1;i<=n;i++) cin >> a[i];
int up[n+2];
int sm = 0;
map<int,int> sms;
up[n+1] = n+1;
sms[0] = n+1;
for (int i = n;i>=1;i--) {
up[i] = up[i+1];
sm+=a[i];
if (sms.count(sm)) up[i] = min(up[i],sms[sm]-1);
sms[sm] = i;
}
vi jump(n+2),jumpdist(n+2),papa(n+2),criterion(n+2);
for (int i = n+2;i>=1;i--) {
if (i >= n+1) {
jump[i] = i;
jumpdist[i] = 0;
papa[i] = i;
criterion[i] = 0;
continue;
}
papa[i] = up[i];
if (criterion[jump[papa[i]+1]] == criterion[papa[i]+1]) {
jump[i] = jump[jump[papa[i]+1]];
jumpdist[i] = 2*jumpdist[papa[i]+1]+1;
criterion[i] = criterion[papa[i]+1]+1;
}
else {
jump[i] = papa[i];
jumpdist[i] = 1;
criterion[i] = 1;
}
}
int q;
cin >> q;
while (q--) {
int l,r;
cin >> l >> r;
int ans = 0;
int cur = l;
while (cur <= r) {
if (jump[cur] <= r) {
ans+=jumpdist[cur];
cur = jump[cur]+1;
}
else if (papa[cur] <= r) {
ans++;
cur = papa[cur]+1;
}
else break;
}
cout << ans << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message (stderr)
sumzero.cpp:13:43: warning: overflow in conversion from 'double' to 'int' changes value from '2.0e+18' to '2147483647' [-Woverflow]
13 | const int MOD = 1e9+7, LIM = 1e6+1, inf = 2e18;
| ^~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |