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