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>
#define LL long long
using namespace std;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
LL a[n + 1];
for(int i = 1;i <= n;i ++){
cin >> a[i];
}
const int LOG = 19;
LL pre[n + 1];
pre[0] = 0;
for(int i = 1;i <= n;i ++){
pre[i] = pre[i - 1] + a[i];
}
vector<int> o(n + 1);
iota(o.begin() , o.end() , 0);
sort(o.begin() , o.end() , [&](int i , int j){
return pair<int , int>{pre[i] , i} < pair<int , int>{pre[j] , j};
});
vector<int> lst(n + 1);
for(int i = 0;i <= n;i ++){
int j = i;
while(j <= n && pre[o[i]] == pre[o[j]]){
lst[o[j]] = (j > i ? o[j - 1] : -1);
++j;
}
i = j - 1;
}
for(int i = 1;i <= n;i ++){
lst[i] = max(lst[i] , lst[i - 1]);
}
int q;
cin >> q;
vector<int> l(q) , r(q) , ans(q);
for(int i = 0;i < q;i ++){
cin >> l[i] >> r[i];
}
for(int bit = LOG - 1;bit >= 0;bit --){
auto e = lst;
for(int c = 0;c < bit;c ++){
for(int i = n;i >= 0;i --){
e[i] = (e[i] > -1 ? e[e[i]] : -1);
}
}
for(int i = 0;i < q;i ++){
if(e[r[i]] >= l[i] - 1){
ans[i] += 1 << bit;
r[i] = e[r[i]];
}
}
}
for(int i = 0;i < q;i ++){
cout << ans[i] << '\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... |