This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
}
vector<int> R(n);
map<long long, int> mp;
long long s = 0;
mp[0] = n;
for (int i = n - 1; i >= 0; i--) {
s += c[i];
if (mp.count(s)) {
R[i] = mp[s] - 1;
} else {
R[i] = n;
}
mp[s] = i;
if (i + 1 < n) {
R[i] = min(R[i], R[i + 1]);
}
}
const int L = 5;
vector<int> pw(1, 8);
for (int i = 0; i < L; i++) {
pw.push_back(pw.back() * 8);
}
vector<vector<int>> pos(n, vector<int>(L));
for (int i = 0; i < n; i++) {
int x = R[i];
for (int j = 0; j < 7; j++) {
if (x + 1 < n) {
x = R[x + 1];
} else {
x = n;
}
}
pos[i][0] = x;
}
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
int x = pos[i][j - 1];
for (int iter = 0; iter < 7; iter++) {
if (x + 1 < n) {
x = pos[x + 1][j - 1];
} else {
x = n;
}
}
pos[i][j] = x;
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l; --r;
int ans = 0;
for (int i = L - 1; i >= 0; i--) {
while (l < n && pos[l][i] <= r) {
ans += pw[i];
l = pos[l][i] + 1;
}
}
while (l < n && R[l] <= r) {
ans++;
l = R[l] + 1;
}
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... |