이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct defaultval {
long long i = -1;
operator long long() const { return i; }
defaultval (long long i) : i (i) {}
defaultval () : i (-1) {}
};
int main () {
ios::sync_with_stdio (false);
cin.tie (0);
cout.tie (0);
long long n = 0;
cin >> n;
vector <long long> v (n + 1);
for (long long i = 1; i <= n; i++)
cin >> v[i];
long long pref = 0;
map <long long, defaultval> prefmap;
vector <vector <long long> > binlift (n + 1, vector <long long> (20, 1e9));
prefmap[0] = 0;
for (long long 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 (long long i = n - 1; i >= 1; i--)
if (binlift[i][0] == 1e9)
binlift[i] = binlift[i + 1];
else
for (long long j = 1; j <= 19; j++)
if (binlift[i][j - 1] + 1 > n)
break;
else
binlift[i][j] = min (binlift[binlift[i][j - 1] + 1][j - 1], binlift[i + 1][j]);
long long q = 0;
cin >> q;
while (q--) {
long long l = 0, r = 0;
cin >> l >> r;
long long ans = 0;
for (long long 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... |