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;
typedef long long int64;
const int maxn = 4e5 + 5;
const int lg = 17;
int64 a [maxn];
map <int64, int> ps;
int l [maxn];
int jump [maxn][lg];
int main() {
int n;
cin >> n;
for (int i = 0; i <= n + 1; i++){
l[i] = n + 1;
}
ps[0] = 0;
int64 s = 0;
for (int i = 1; i <= n; i++){
cin >> a[i];
s+=a[i];
if (ps.count(s)) {
l[ps[s] + 1] = i;
}
ps[s] = i;
}
for (int i = n; i >= 1; i--){
l[i] = min(l[i], l[i + 1]);
jump[i][0] = l[i];
}
for (int k = 1; k < lg; k++){
jump[n + 1][k - 1] = n + 1;
jump[n + 2][k - 1] = n + 1;
for (int i = 1; i <= n; i++){
jump[i][k] = jump[jump[i][k-1] + 1][k-1];
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int rs = 0;
for (int i = lg - 1; i >= 0; i--){
while (jump[l][i] <= r) {
rs+=(1 << i);
l = jump[l][i] + 1;
}
}
cout << rs << "\n";
}
// for (auto i: sv){
// cout << i.first << " " << i.second << "\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... |