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;
using ll = long long;
#define x first
#define y second
ll N, Q, F[20][400005];
pair<ll, ll> B[400005];
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> N;
fill(F[0], F[19] + N + 3, N + 2);
for(ll i = 1; i <= N; i++){
ll v; cin >> v;
B[i] = {B[i - 1].x + v, i};
}
sort(B, B + N + 1);
for(ll i = 1; i <= N; i++){
if(B[i - 1].x == B[i].x){
F[0][B[i - 1].y + 1] = B[i].y + 1;
}
}
for(ll i = N; i >= 1; i--){
F[0][i] = min(F[0][i], F[0][i + 1]);
}
for(ll j = 1; j < 20; j++){
for(ll i = 1; i <= N; i++){
F[j][i] = F[j - 1][F[j - 1][i]];
}
}
cin >> Q;
while(Q--){
ll L, R; cin >> L >> R;
ll t = 0, u = L;
for(ll j = 19; j >= 0; j--){
if(F[j][u] > R + 1 || F[j][u] == 0) continue;
u = F[j][u], t |= (1LL << j);
}
cout << t << '\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... |