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;
vector<pair<lli, int>> xs{{0, 0}};
lli sum = 0;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
sum += x;
xs.emplace_back(sum, i);
}
vector<int> ant(n+1);
for (int i = 1; i <= n; ++i)
ant[i] = -1;
sort(xs.begin(), xs.end());
for (int i = 1; i <= n; ++i)
if (xs[i].first == xs[i-1].first)
ant[xs[i].second] = xs[i-1].second;
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";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |