#include <bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define MOD2 998244353
using namespace std;
void solve(int cas){
int n; cin>>n;
vector<ll> nums(n);
for (auto& a: nums) cin>>a;
ll s = 0;
map<ll,ll> suff;
suff[0] = n;
vector<ll> nxt(n + 1, INT_MAX);
for (int i = n - 1; i >= 0; i--){
s += nums[i];
nxt[i] = nxt[i+1];
if (suff.count(s)){
nxt[i] = min(nxt[i], suff[s]);
}
suff[s] = i;
}
int lg = 32 - __builtin_clz(n);
vector<vector<int>> jump(n, vector<int> (lg));
for (int i = 0; i < n; i++){
jump[i][0] = nxt[i];
}
for (int j = 1; j < lg; j++){
for (int i = 0; i < n; i++){
if (jump[i][j-1]==INT_MAX){
jump[i][j] = INT_MAX;
continue;
}
else if (jump[i][j-1]==n){
jump[i][j] = INT_MAX;
continue;
}
jump[i][j] = jump[jump[i][j-1]][j-1];
}
}
int q; cin>>q;
while (q--){
int l,r; cin>>l>>r;
--l;--r;
int mx = 0;
for (int bit = lg-1; bit >= 0; bit--){
if (l <= r && jump[l][bit] <= r + 1){
mx |= (1<<bit);
l = jump[l][bit];
}
}
cout << mx << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
//cin>>t;
for (int i = 1; i <= t; i++){
solve(i);
}
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... |