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 c=4e5+5;
typedef long long ll;
int n,q;
ll arr[c];
int seg[c];
map<ll,int> mp;
int up[c][20];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n; mp[0]=0;
for(int i=1;i<=n;i++){
cin>>arr[i];
arr[i]+=arr[i-1];
if(mp.find(arr[i]) != mp.end()){
seg[i]=mp[arr[i]]+1;
}
else seg[i]=-1;
mp[arr[i]]=i;
up[i][0]=c;
}
up[n+1][0]=n+2; up[n+2][0]=n+2;
for(int i=n;i>=1;i--){
if(seg[i]!=-1) up[seg[i]][0]=i+1;
up[i][0]=min(up[i][0],up[i+1][0]);
}
for(int j=1;j<20;j++){
for(int i=1;i<=n+2;i++){
up[i][j]=up[up[i][j-1]][j-1];
}
}
cin>>q;
while(q--){
int l,r; cin>>l>>r;
int ans=0; int cur=l;
for(int i=19;i>=0;i--){
if(up[cur][i]<=r+1){
cur=up[cur][i];
ans+=1<<i;
}
}
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... |