이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
namespace std {
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Dimension must be positive");
template <typename... Args>
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, T val = T()) : std::vector<T>(n, val) {}
};
/* Example
Vec<4, int64_t> f(n, k, 2, 2); // = f[n][k][2][2];
Vec<2, int> adj(n); // graph
*/
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
/* Example
auto fun = y_combinator([&](auto self, int x) -> void {
self(x + 1);
});
*/
} // namespace std
const int N = 400002;
int a[N], jump[N], prv[N], depth[N], b[N + 1];
int64_t pref[N + 1];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 2; i <= n + 1; i++) cin >> a[i];
for (int i = 2; i <= n + 1; i++) pref[i + 1] = pref[i] + a[i];
sort(pref + 2, pref + n + 3);
int k = unique(pref + 2, pref + n + 3) - pref + 2;
memset(b, -1, sizeof b);
b[lower_bound(pref + 2, pref + k + 2, 0) - pref + 2] = 1;
prv[0] = 0;
prv[1] = 0;
depth[0] = 0;
depth[1] = 1;
jump[1] = 0;
int64_t sum = 0;
for (int i = 2; i <= n + 1; i++) {
sum += a[i];
int x = lower_bound(pref + 2, pref + 2 + k, sum) - pref + 2;
if (b[x] != -1) {
prv[i] = b[x];
prv[i] = max(prv[i], prv[i - 1]);
} else {
prv[i] = prv[i - 1];
}
depth[i] = depth[prv[i]] + 1;
if (depth[prv[i]] - depth[jump[prv[i]]] == depth[jump[prv[i]]] - depth[jump[jump[prv[i]]]]) {
jump[i] = jump[jump[prv[i]]];
} else {
jump[i] = prv[i];
}
b[x] = i;
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
r++;
int res = 0;
while (r >= l) {
if (jump[r] >= l) {
res += depth[r] - depth[jump[r]];
r = jump[r];
} else if (prv[r] >= l) {
res++;
r = prv[r];
} else {
break;
}
}
cout << res << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |