Submission #1225659

#TimeUsernameProblemLanguageResultExecution timeMemory
1225659Ghulam_JunaidSum Zero (RMI20_sumzero)C++20
22 / 100
2 ms3652 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 4e4 + 10, LG = 19;
int n, q, a[N], par[N][LG];
ll p[N];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    cin >> n;
    map<ll, int> last;
    last[0] = 0;
    for (int i = 1; i <= n + 2; i ++)
        par[i][0] = n + 2;
    for (int i = 1; i <= n; i ++){
        cin >> a[i];
        p[i] = p[i - 1] + a[i];
        if (last.find(p[i]) != last.end())
            par[last[p[i]] + 1][0] = i + 1;
        last[p[i]] = i;
    }
    for (int i = n; i > 0; i --)
        par[i][0] = min(par[i][0], par[i + 1][0]);

    for (int j = 1; j < LG; j ++)
        for (int i = 1; i <= n + 2; i ++)
            par[i][j] = par[par[i][j - 1]][j - 1];

    cin >> q;
    while (q--){
        int l, r;
        cin >> l >> r;

        int cur = l, ans = 0;
        for (int i = LG - 1; i >= 0; i --){
            if (par[cur][i] <= r + 1){
                cur = par[cur][i];
                ans += (1 << i);
            }
        }
        cout << ans << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...