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>
#define f first
#define s second
//#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 4e5 + 5, mod = 1e9 + 7; // !
int t, R[N][20], a[N];
map<int,int> f;
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i]; a[i] += a[i - 1];
}
int mn = n + 1;
for(int j = 0; j <= 19; j++) R[n + 1][j] = n + 1;
for(int i = n; i >= 0; i--) {
if(f[a[i]]) mn = min(mn, f[a[i]]);
f[a[i]] = i;
R[i][0] = mn;
for(int j = 0 + 1; j < 20; j++) R[i][j] = R[R[i][j - 1]][j - 1];
}
int q;
cin >> q;
while(q--) {
int l, r, ans = 0;
cin >> l >> r; --l;
for(int j = 19; j >= 0; j--) {
if(R[l][j] <= r) ans += (1 << j), l = R[l][j];
}
cout << ans << "\n";
}
}
Compilation message (stderr)
sumzero.cpp:10:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
10 | 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... |