// 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) + 1;
int dep[max_N];
int prv[max_N];
std::list<int> adj[max_N];
int tim = 0;
void dfs(int v) {
prv[v] = tim++;
for (auto u : adj[v]) {
dfs(u);
}
}
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;
int x, y;
lst[0] = 0;
prv[0] = -1;
dep[0] = 0;
for (int i = 1; i <= N; ++i) {
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;
}
for (int i = 0; i <= N; ++i) {
if (prv[i] == -1) {
dfs(i);
}
}
int Q;
std::cin >> Q;
for (int i = 0; i < Q; ++i) {
std::cin >> x >> y;
--x;
int ans = dep[y] - dep[x];
if (prv[x] >= prv[y]) {
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... |