This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define sz(x) int(x.size())
#define pb push_back
using ll = long long;
template<class T> using V = vector<T>;
using vl = V<ll>;
const int nax = 4e5+5;
const int K = 16;
const int LG = 5;
int nxt[nax][LG];
int A[nax], N;
unordered_map<ll, int> lst;
int main() {
cin.tie(0)->sync_with_stdio(0);
for(int i = 0; i < nax; i++) for(int j = 0; j < LG; j++) nxt[i][j] = nax - 1;
cin >> N;
ll S = 0;
for(int i = 0; i < N; i++) {
cin >> A[i];
S += A[i];
}
lst[S] = N;
for(int i = N - 1; i >= 0; i--) {
S -= A[i];
if (lst.find(S) == lst.end()) nxt[i][0] = nax - 1;
else nxt[i][0] = lst[S];
if (i + 1 < N) nxt[i][0] = min(nxt[i][0], nxt[i + 1][0]);
lst[S] = i;
for(int t = 1; t < LG; t++) {
// nxt[i][t] = nxt[nxt[nxt[...nxt[i][t-1]][t-1]][t-1]..]
nxt[i][t] = nxt[i][t-1];
for(int _ = 0; _ < K - 1; _++) nxt[i][t] = nxt[nxt[i][t]][t-1];
}
}
int Q, l, r, ans; cin >> Q;
while(Q--) {
cin >> l >> r; --l;
ans = 0;
for(int t = LG - 1; t >= 0; t--) {
while(nxt[l][t] <= r) {
ans += (1 << (4 * t));
l = nxt[l][t];
}
}
cout << ans << nl;
}
exit(0-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... |