// 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<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
std::vector<i64> pre(N + 1);
for (int i = 0; i < N; ++i) {
pre[i + 1] = pre[i] + A[i];
}
debug(pre);
const int LG = std::__lg(N + 1);
std::vector<std::vector<int>> prv(LG + 1, std::vector<int>(N + 1));
std::map<i64, int> lst;
lst[0] = 0;
prv[0][0] = -1;
for (int i = 1; i <= N; ++i) {
if (lst.count(pre[i])) {
prv[0][i] = std::max(lst[pre[i]], prv[0][i - 1]);
} else {
prv[0][i] = prv[0][i - 1];
}
lst[pre[i]] = i;
}
for (int i = 1; i <= LG; ++i) {
for (int j = 0; j <= N; ++j) {
if (prv[i - 1][j] == -1) {
prv[i][j] = prv[i - 1][j];
} else {
prv[i][j] = prv[i - 1][prv[i - 1][j]];
}
}
}
int Q;
std::cin >> Q;
for (int i = 0; i < Q; ++i) {
int L, R;
std::cin >> L >> R;
--L;
int x = R;
int ans = 0;
for (int j = LG; j >= 0; --j) {
if (prv[j][x] >= L) {
x = prv[j][x];
ans += 1 << j;
}
}
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... |