이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::pair<ll, ll> pll;
typedef std::pair<ld, ld> pld;
#define MAXN 410000ll
#define LOGN ((ll) log2(MAXN) + 1ll)
int jump[MAXN][LOGN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> arr(n);
for (auto &i: arr) cin >> i;
#define INF INT32_MAX
vector<int> next(n, INF);
{
map<ll, set<int> > sums;
vector<ll> pre_sum(n + 1);
pre_sum[0] = 0;
for (int i = 0; i < n; i++) {
pre_sum[i + 1] = pre_sum[i] + arr[i];
sums[pre_sum[i + 1]].insert(i + 1);
}
ll curr_sum = 0;
for (int i = 0; i < n; i++) {
if (sums.count(curr_sum)) {
auto it = sums[curr_sum].upper_bound(i);
if (it != sums[curr_sum].end()) next[i] = *it;
}
curr_sum += arr[i];
}
for (int i = n - 2; i >= 0; i--)
next[i] = min(next[i], next[i + 1]);
}
for (int i = 0; i < n; i++) jump[i][0] = next[i];
for (int k = 1; k < LOGN; k++)
for (int i = 0; i < n; i++)
jump[i][k] = ((jump[i][k - 1] >= n) ? INF : jump[jump[i][k - 1]][k - 1]);
int q;
cin >> q;
while (q-- > 0) {
int l, r;
cin >> l >> r;
--l;
int ans = 0;
int curr = l;
for (int k = LOGN - 1; k >= 0 && curr < r; k--) {
if (jump[curr][k] <= r) {
curr = jump[curr][k];
ans += pow(2, k);
}
}
cout << ans << "\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... |