이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define nl '\n'
#ifdef LOCAL
#include "template\debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<std::pair<int64_t, int>> vals(n + 1);
vals[0] = {0, 0};
int64_t sum = 0;
for (int i = 1; i <= n; i++) {
int x;
std::cin >> x;
sum += x;
vals[i] = {sum, i};
}
std::sort(vals.begin(), vals.end());
std::vector<int> R(n + 1);
for (int i = 0; i <= n; i++) {
int j = i;
while (j <= n && vals[i].first == vals[j].first) {
j++;
}
// now we talking
R[vals[j - 1].second] = n + 1;
for (int k = j - 2; k >= i; k--) {
R[vals[k].second] = vals[k + 1].second;
}
}
vals.clear();
vals.shrink_to_fit();
std::vector<int> jump(n + 2), depth(n + 2), parent(n + 2);
jump[n + 1] = n + 1;
for (int i = n, to = n + 1; i >= 0; i--) {
to = std::min(to, R[i]);
parent[i] = to;
depth[i] = depth[parent[i]] + 1;
if (depth[parent[i]] - depth[jump[parent[i]]] == depth[jump[parent[i]]] - depth[jump[jump[parent[i]]]]) {
jump[i] = jump[jump[parent[i]]];
} else {
jump[i] = parent[i];
}
}
int Q;
std::cin >> Q;
while (Q--) {
int l, r;
std::cin >> l >> r;
l--;
int counter = 0;
int u = l;
while (parent[u] <= r) {
if (jump[u] <= r) {
counter += depth[u] - depth[jump[u]];
u = jump[u];
} else {
counter++;
u = parent[u];
}
}
std::cout << counter << nl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |