#pragma GCC optimize("ily despite the warnings")
#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 = 4e5+23;
int dp[LG][sze];
void fast(){
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
map<int,int> lan;
lan[0]=n;
int sum=0;
for(int i =0;i<=n;i++){
for(int j=0;j<LG;j++){
dp[j][i]=n;
}
}
for(int i=n-1;i>=0;i--){
sum+=arr[i];
if(lan.count(sum)){
dp[0][i] = lan[sum];
}
else{
dp[0][i]=dp[0][i+1];
}
lan[sum]=i;
}
for(int i = 1;i<LG;i++){
for(int j=0;j<n;j++){
dp[i][j]=dp[i-1][dp[i-1][j]];
}
}
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
--l;--r;
int ans=0;
for(int i = LG-1;i>=0;i--){
if(dp[i][l]<=r){
l = dp[i][l];
// cout<<i<<" "<<l<<endl;
ans|=(1<<i);
}
}
// cout<<ans<<" "<<dp[0][l]<<endl;
ans+=( dp[0][l] <=r+1);
cout<<ans<<endl;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt =1;
// cin>>tt;
while(tt--){
fast();
}
return 0;
}
/*
*/
Compilation message (stderr)
sumzero.cpp:1:48: warning: bad option '-fily despite the warnings' to pragma 'optimize' [-Wpragmas]
1 | #pragma GCC optimize("ily despite the warnings")
| ^
sumzero.cpp:19:11: warning: bad option '-fily despite the warnings' to attribute 'optimize' [-Wattributes]
19 | void fast(){
| ^
sumzero.cpp:69:13: warning: bad option '-fily despite the warnings' to attribute 'optimize' [-Wattributes]
69 | signed main(){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |