// 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;
}
constexpr int max_N = int(4E5) + 5;
int dep[max_N];
int prv[max_N];
std::vector<int> adj[max_N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::map<i64, int> lst;
i64 pre = 0;
lst[0] = 0;
prv[0] = -1;
dep[0] = 0;
for (int i = 1; i <= N; ++i) {
int x;
std::cin >> x;
pre += x;
if (lst.count(pre)) {
prv[i] = std::max(lst[pre], prv[i - 1]);
} else {
prv[i] = prv[i - 1];
}
dep[i] = prv[i] == -1 ? 0 : dep[prv[i]] + 1;
lst[pre] = i;
}
lst.clear();
for (int i = 0; i <= N; ++i) {
if (prv[i] != -1) {
adj[prv[i]].emplace_back(i);
}
prv[i] = -1;
}
int tim = 0;
auto dfs = [&](auto&& self, int v) -> void {
prv[v] = tim++;
for (auto u : adj[v]) {
self(self, u);
}
};
for (int i = 0; i <= N; ++i) {
if (prv[i] == -1) {
dfs(dfs, i);
}
}
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 (prv[L] >= prv[R]) {
ans -= 1;
}
std::cout << ans << '\n';
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |