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;
const int BASE = 10;
const int LOG10 = 6;
int pow10[LOG10];
int dp[400005][LOG10];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
pow10[0] = 1;
for (int i = 1; i < LOG10; i++) {
pow10[i] = BASE * pow10[i - 1];
}
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
// dp[i][x] = minimum j, such that [i, j - 1] contains 6^x valid segments
{ // Compute dp[]
for (int i = 0; i <= N + 1; i++) {
for (int j = 0; j < LOG10; j++) {
dp[i][j] = N + 1;
}
}
long long sum = 0;
vector<long long> coords;
coords.emplace_back(sum);
for (int i = N - 1; i >= 0; i--) {
sum += A[i];
coords.emplace_back(sum);
}
sort(begin(coords), end(coords));
coords.resize(unique(begin(coords), end(coords)) - begin(coords));
sum = 0;
vector<int> last(coords.size(), N + 1);
const auto Get = [&](long long x) {
return lower_bound(begin(coords), end(coords), x) - begin(coords);
};
last[Get(sum)] = N;
for (int i = N - 1; i >= 0; i--) {
sum += A[i];
dp[i][0] = last[Get(sum)];
last[Get(sum)] = i;
}
for (int i = N - 2; i >= 0; i--) {
dp[i][0] = min(dp[i][0], dp[i + 1][0]);
}
for (int j = 1; j < LOG10; j++) {
for (int i = 0; i < N; i++) {
dp[i][j] = dp[dp[dp[dp[dp[dp[dp[dp[dp[dp[i][j - 1]][j - 1]][j - 1]][j - 1]][j - 1]][j - 1]][j - 1]][j - 1]][j - 1]][j - 1];
}
}
}
int Q;
cin >> Q;
while (Q--) {
int L, R;
cin >> L >> R;
L--;
int ans = 0;
int x = LOG10 - 1;
while (x >= 0) {
if (dp[L][x] <= R) {
ans += pow10[x];
L = dp[L][x];
} else {
x--;
}
}
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... |