This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 4e5 + 2;
const int K = 32; // base
const int L = 4;  // K^L >= N
int a[N];
int p[N];
int go[N][L];
void solve(){
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    unordered_map<ll, int> mp;
    mp[0] = n + 1;
    ll sum = 0;
    for (int i = n; i >= 1; i--){
        sum += a[i];
        if (mp.find(sum) != mp.end()){
            p[i] = mp[sum];
        } else {
            p[i] = -1;
        }
        mp[sum] = i;
    }
    int where = -1;
    for (int i = n; i >= 1; i--){
        if (p[i] != -1){
            if (where == -1){
                where = p[i];
            } else {
                where = min(where, p[i]);
            }
        }
        go[i][0] = where;
    }
    go[n + 1][0] = -1;
    for (int j = 1; j < L; j++){
        for (int i = 1; i <= n + 1; i++){
            int v = i;
            for (int t = 0; t < K && v != -1; t++){
                v = go[v][j - 1];
            }
            go[i][j] = v;
        }
    }
    vector<int> pw(L);
    pw[0] = 1;
    for (int i = 1; i < L; i++) pw[i] = pw[i - 1] * K;
    int q;
    cin >> q;
    while (q--){
        int l, r;
        cin >> l >> r;
        ++r;
        int ans = 0;
        int v = l;
        for (int i = L - 1; i >= 0; i--){
            while (go[v][i] != -1 && go[v][i] <= r){
                v = go[v][i];
                ans += pw[i];
            }
        }
        cout << ans << "\n";
    }
}
int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test_cases = 1;
    //cin >> test_cases;
    for (int test = 1; test <= test_cases; test++){
        //cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }
#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
    return 0;
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:99:13: warning: unused variable 'startTime' [-Wunused-variable]
   99 |     clock_t startTime = clock();
      |             ^~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |