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 <iostream>
#include <map>
using namespace std;
long long int a[400005], c[400005], f[400005];
int l[400005][20];
map<long long int, long long int> m;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
m[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
c[i] = c[i - 1] + a[i];
f[i] = f[i - 1];
if (m.count(c[i])) {
f[i] = max(f[i], m[c[i]] + 1);
}
m[c[i]] = i;
}
for (int i = 1; i <= n; i++) {
l[i][0] = f[i];
}
for (int i = 1; i <= 19; i++) {
for (int j = 1; j <= n; j++) {
if (l[j][i - 1] == 0) l[j][i] = 0;
else l[j][i] = l[l[j][i - 1] - 1][i - 1];
}
}
int q;
cin >> q;
for (int jj = 0; jj < q; jj++) {
int x, y, res = 0;
cin >> x >> y;
for (int i = 19; i >= 0; i--) {
if (y < x) break;
if (l[y][i] >= x) {
y = l[y][i] - 1;
res += (1 << i);
}
}
cout << res << '\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... |