이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
unordered_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]);
}
}
mp.clear();
const int L = 4;
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... |