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