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 <vector>
#include <map>
using namespace std;
struct defaultval {
long long i = -1;
operator int() const { return i; }
defaultval (int i) : i (i) {}
defaultval () : i (-1) {}
};
int main () {
ios::sync_with_stdio (false);
cin.tie (0);
cout.tie (0);
int n = 0;
cin >> n;
vector <int> v (n + 1);
for (int i = 1; i <= n; i++)
cin >> v[i];
vector <vector <int> > binlift (n + 1, vector <int> (20, 1e9));
long long pref = 0;
map <long long, defaultval> prefmap;
prefmap[0] = 0;
for (int i = 1; i <= n; i++) {
pref += v[i];
if (prefmap[pref] == -1)
prefmap[pref] = i;
else {
if (binlift[prefmap[pref] + 1][0] == 1e9)
binlift[prefmap[pref] + 1][0] = i;
prefmap[pref] = i;
}
}
for (int i = n - 1; i >= 1; i--) {
binlift[i][0] = min (binlift[i][0], binlift[i + 1][0]);
for (int j = 1; j <= 19; j++)
if (binlift[i][j - 1] + 1 > n)
break;
else
binlift[i][j] = binlift[binlift[i][j - 1] + 1][j - 1];
}
int q = 0;
cin >> q;
while (q--) {
int l = 0, r = 0;
cin >> l >> r;
int ans = 0;
for (int p = 19; p >= 0; p--)
if (l <= n && binlift[l][p] <= r)
ans += (1 << p), l = binlift[l][p] + 1;
cout << ans << '\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... |