Submission #1297491

#TimeUsernameProblemLanguageResultExecution timeMemory
1297491MisterReaperSum Zero (RMI20_sumzero)C++20
61 / 100
192 ms24820 KiB
// File B.cpp created on 01.12.2025 at 09:17:41
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

template<typename T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int N;
    std::cin >> N;

    std::vector<i64> pre(N + 1);
    for (int i = 0; i < N; ++i) {
        int x;
        std::cin >> x;
        pre[i + 1] = pre[i] + x;
    }

    debug(pre);

    std::vector<int> dep(N + 1);
    std::vector<int> prv(N + 1);
    std::map<i64, int> lst;
    lst[0] = 0;
    prv[0] = -1;
    dep[0] = 0;
    for (int i = 1; i <= N; ++i) {
        if (lst.count(pre[i])) {
            prv[i] = std::max(lst[pre[i]], prv[i - 1]);
        } else {
            prv[i] = prv[i - 1];
        }
        dep[i] = prv[i] == -1 ? 0 : dep[prv[i]] + 1;
        lst[pre[i]] = i;
    }

    lst.clear();
    pre.clear();

    std::vector<std::vector<int>> adj(N + 1);
    for (int i = 0; i <= N; ++i) {
        if (prv[i] != -1) {
            adj[prv[i]].emplace_back(i);
        }
    }
    prv.clear();

    int tim = 0;
    std::vector<int> tin(N + 1, -1);
    auto dfs = [&](auto&& self, int v) -> void {
        tin[v] = tim++;
        for (auto u : adj[v]) {
            self(self, u);
        }
    };
    for (int i = 0; i <= N; ++i) {
        if (tin[i] == -1) {
            dfs(dfs, i);
        }
    }

    debug(prv);
    debug(dep);

    int Q;
    std::cin >> Q;

    for (int i = 0; i < Q; ++i) {
        int L, R;
        std::cin >> L >> R;
        --L;
        int ans = dep[R] - dep[L];
        if (tin[L] >= tin[R]) {
            ans -= 1;
        }
        std::cout << ans << '\n';
    }
  
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...