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;
int a,b,c,d,e,i,j,ii,jj,zx,xc,tes,t,g[400009],l,r,dep,pi,ans[400009],lef,rig,mid,vi,q;
pair <long long, int> p[400009];
long long A;
//pair <long long, int> f[400009];
//vector <int> v[400009];
pair <int, int> v[400009];
//vector <pair <int, int> > vv[400009];
pair <int, pair <int, int> > vv[400009];
void dfs(){
/*for(vector <pair <int, int> >::iterator it=vv[q].begin(); it!=vv[q].end(); it++){
c=lower_bound(p+1,p+pi+1,make_pair((*it).first,0))-p;
if(c<=pi) ans[(*it).second]=dep[q]-p[c].second; else ans[(*it).second]=0;
}*/
lef=0;rig=tes+1;
while(1){
if(lef+1>=rig) break;
mid=(lef+rig)/2;
if(vv[mid].first>=q){
rig=mid;
}else{
lef=mid;
}
}
for(ii=rig; ii<=tes; ii++){
if(vv[ii].first!=q) break;
A=vv[ii].second.first;
c=lower_bound(p+1,p+pi+1,make_pair(A,0))-p;
if(c<=pi) ans[vv[ii].second.second]=dep-p[c].second; else ans[vv[ii].second.second]=0;
}
pi++;p[pi]={q,dep};
lef=0;rig=vi+1;
while(1){
if(lef+1>=rig) break;
mid=(lef+rig)/2;
if(v[mid].first>=q){
rig=mid;
}else{
lef=mid;
}
}
//for(vector <int>::iterator it=v[q].begin(); it!=v[q].end(); it++){
for(int I=rig; I<=vi; I++){
if(v[I].first!=q) break;
dep++;
q=v[I].second;
dfs(/*v[I].second*/);
q=p[pi].first;
dep--;
}
//}
pi--;
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a;
g[1]=0;
p[0].first=0;p[0].second=1;
for(i=1; i<=a; i++){
cin>>p[i].first;p[i].first+=p[i-1].first;p[i].second=i+1;
/*g[i+1]=m[f[i]];
m[f[i]]=i+1;*/
}
sort(p,p+a+1);
for(i=1; i<=a; i++){
if(p[i].first==p[i-1].first){
g[p[i].second]=p[i-1].second;
}else{
g[p[i].second]=0;
}
}
for(i=1; i<=a+1; i++){
g[i]=max(g[i],g[i-1]);
//v[g[i]].push_back(i);
vi++;v[vi]={g[i],i};
}
sort(v+1,v+vi+1);
cin>>tes;
for(t=1; t<=tes; t++){
cin>>l>>r;l++;r++;
//vv[r].push_back(make_pair(l-1,t));
vv[t]={r,{l-1,t}};
}
sort(vv+1,vv+tes+1);
dep=1;
q=0;
dfs();
for(t=1; t<=tes; t++){
cout<<ans[t]<<"\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |