Submission #1225685

#TimeUsernameProblemLanguageResultExecution timeMemory
1225685Ghulam_JunaidSum Zero (RMI20_sumzero)C++20
61 / 100
543 ms25744 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 4e5 + 5, LG = 10;
int n, q, last[N], par[N][LG];
ll sm;

bool cmp(pair<ll, int> &a, pair<ll, int> &b){
    return (a.second < b.second);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    memset(last, -1, sizeof last);

    cin >> n;
    for (int i = 1; i <= n + 2; i ++)
        par[i][0] = n + 2;
    
    vector<pair<ll, int>> vec(n + 1);
    vec[0] = {sm, 0};
    for (int i = 1, x; i <= n; i ++){
        cin >> x;
        sm += x;
        vec[i] = {sm, i};
    }
    sort(vec.begin(), vec.end());

    for (int i = 0, r, j; i <= n; i ++){
        r = i;
        while (r <= n and vec[r].first == vec[i].first)
            r++;
        for (j = i; j < r; j ++)
            vec[j].first = i;
        i = r - 1;
    }
    sort(vec.begin(), vec.end(), cmp);

    last[vec[0].first] = 0;
    for (int i = 1; i <= n; i ++){
        if (last[vec[i].first] != -1) par[last[vec[i].first] + 1][0] = i + 1;
        last[vec[i].first] = 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;
    int jump, l, r, cur, ans;
    while (q--){
        l, r;
        cin >> l >> r;

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