#pragma GCC optimize("O1")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ins insert
#define pb push_back
#define pii pair<int,int>
#define endl '\n'
#define all(x) x.begin(),x.end()
const int mod = 998244353;
const int inf = INT_MAX;
const int LG = 18;
const int sze = 1e5+23;
int dp[LG][sze];
void fast(){
int n;
cin>>n;
vector<int> arr(n);
for(int i=1;i<=n;i++){
cin>>arr[i];
}
for(int i =0;i<LG;i++){
for(int j=0;j<sze;j++){
dp[i][j]=inf;
}
}
map<int,int> last;
last[0]=n+1;
int sum=0;
for(int i = n;i>=1;i--){
sum+=arr[i];
dp[0][i]=dp[0][i+1];
if(last.count(sum)){
dp[0][i]=min(dp[0][i],last[sum]);
}
last[sum]=i;
}
for(int i=1;i<LG;i++){
for(int j=1;j<=n;j++){
if(dp[i-1][j]!=inf){
dp[i][j]=dp[i-1][dp[i-1][j]];
}
}
}
// cout<<dp[2][1]<<endl;
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
int res=0;
for(int i = LG-1;i>=0;i--){
if(dp[i][l]<=r+1){
res|=(1<<i);
// cout<<i<<" :"<<l<<" "<<dp[i][l]<<endl;
l=dp[i][l];
}
}
cout<<res<<endl;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt =1;
// cin>>tt;
while(tt--){
fast();
}
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... |