// 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 prv[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;
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];
}
lst[pre] = i;
}
int prvb[N + 1] {};
for (int i = 0; i <= N; ++i) {
prvb[i] = prv[i];
}
for (int k = 0; k < 8; ++k) {
for (int i = N; i >= 0; --i) {
if (prvb[i] != -1) {
prvb[i] = prvb[prvb[i]];
}
}
}
lst.clear();
int Q;
std::cin >> Q;
for (int i = 0; i < Q; ++i) {
int L, R;
std::cin >> L >> R;
--L;
int ans = 0;
while (prvb[R] >= L) {
R = prvb[R];
ans += 1 << 8;
}
while (prv[R] >= L) {
R = 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... |