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;
const int maxn = 4e5+5;
const int step = 74;
int n;
pair<long long, int> A[maxn];
int mx[maxn], mx_b[maxn], mx_b2[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
memset(mx, -1, sizeof(mx));
for(int i = 1; i <= n; i++){
cin >> A[i].first;
A[i].first += A[i-1].first;
A[i].second = i;
}
sort(A, A+n+1);
for(int i = 1; i <= n; i++){
if(A[i].first == A[i-1].first)mx[A[i].second] = A[i-1].second;
}
for(int i = 0; i <= n; i++){
if(i)mx[i] = max(mx[i], mx[i-1]);
mx_b[i] = i;
for(int j = 0; j < step; j++){
if(mx_b[i] == -1)break;
mx_b[i] = mx[mx_b[i]];
}
mx_b2[i] = i;
for(int j = 0; j < step; j++){
if(mx_b2[i] == -1)break;
mx_b2[i] = mx_b[mx_b2[i]];
}
}
int q;
cin >> q;
while(q--){
int l, r, ans = 0;
cin >> l >> r;
while(mx[r] >= l-1){
if(mx_b2[r] >= l-1){
r = mx_b2[r];
ans += step*step;
}
if(mx_b[r] >= l-1){
r = mx_b[r];
ans += step;
}
if(mx[r] >= l-1){
r = mx[r];
ans++;
}
}
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... |