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;
#define ar array
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
vector<vector<int>> par(n + 1);
map<long long, int> last;
long long pref = 0; last[pref] = n;
for(int i=n-1;~i;i--){
pref += a[i];
if(last.count(pref)) par[i].push_back(last[pref]); // = last[pref];
last[pref] = i;
if(i + 1 < n && !par[i+1].empty()){
if(par[i].empty()) par[i].push_back(par[i+1][0]);
else par[i][0] = min(par[i][0], par[i+1][0]);
}
}
for(int j=1;j<15;j++){
for(int i=0;i<n;i++){
if((int)par[i].size() >= j && (int)par[par[i][j-1]].size() >= j){
par[i].push_back(par[par[i][j-1]][j-1]);
}
}
}
//~ cout<<"here"<<endl;
int res = 0, q, l, r; cin>>q;
while(q--){
cin>>l>>r; l--;
res=0;
for(int j=14;~j && l < r;){
if((int)par[l].size() > j && ~par[l][j] && par[l][j] <= r){
res += (1 << j), l = par[l][j];
} else {
j--;
}
}
cout<<res<<"\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... |