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;
using lli = long long;
const int MAXN = 4e5 + 10;
const int MAXK = 20;
int ant[MAXN], dp[MAXN], prof[MAXN], ls[MAXN], rs[MAXN], ds[MAXN], tmp[MAXN];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n;
cin >> n;
map<lli, vector<int>> mp;
mp[0].push_back(0);
lli sum = 0;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
sum += x;
mp[sum].push_back(i);
}
for (int i = 1; i <= n; ++i)
ant[i] = -1;
for (auto [k, xs]: mp)
for (int i = 1; i < xs.size(); ++i)
ant[xs[i]] = xs[i-1];
dp[0] = -1;
for (int i = 1; i <= n; ++i) {
if (ant[i] < dp[i-1]) dp[i] = dp[i-1];
else dp[i] = ant[i];
prof[i] = 1 + (dp[i] == -1 ? -1 : prof[dp[i]]);
}
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
cin >> ls[i] >> rs[i];
ds[i] = prof[rs[i]] - prof[ls[i]-1];
}
for (int k = 0; k < MAXK; ++k) {
for (int i = 0; i < q; ++i)
if (ds[i] & (1 << k))
rs[i] = dp[rs[i]];
for (int i = 1; i <= n; ++i)
if (dp[i] != -1) tmp[i] = dp[dp[i]];
else tmp[i] = -1;
for (int i = 1; i <= n; ++i)
dp[i] = tmp[i];
}
for (int i = 0; i < q; ++i) {
if (rs[i] < ls[i] - 1) cout << ds[i] - 1 << "\n";
else cout << ds[i] << "\n";
}
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:32:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 1; i < xs.size(); ++i)
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |