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;
//#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int n, A[100005], q, lft[100005];
int par[20][100005];
map <int, int> mp;
void solve(){
cin >> n;
for(int i=1;i<=n;i++)cin >> A[i];
mp[0] = 0;
int cur = 0;
for(int i=1;i<=n;i++){
cur += A[i];
if(mp.find(cur) == mp.end())lft[i] = -1;
else lft[i] = mp[cur];
mp[cur] = i;
}
lft[0] = -1;
for(int i=1;i<=n;i++)lft[i] = max(lft[i], lft[i-1]);
for(int i=0;i<=n;i++)par[0][i] = lft[i];
for(int i=1;i<=19;i++)for(int j=0;j<=n;j++)par[i][j] = (par[i-1][j] != -1 ? par[i-1][par[i-1][j]] : -1);
cin >> q;
while(q--){
int l, r; cin >> l >> r;
int tmp = r, ans = 0;
for(int i=19;i>=0;i--){
if(par[i][tmp] >= l - 1)tmp = par[i][tmp], ans += (1 << i);
}
cout << ans << '\n';
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
sumzero.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
43 | 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... |