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;
const int MAXN = 1e5 + 25;
const int inf2 = 17;
#define ll long long
const ll inf = 1e9;
ll c[MAXN], c2[MAXN], n;
ll lst[MAXN];
ll pp[MAXN];
ll nxt[inf2][MAXN];
ll dd[MAXN];
pair <ll, ll> ls[MAXN];
int mnn = inf;
int pos = inf;
int dep[MAXN];
vector <ll> comp = {0};
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n; memset(dd, -1, sizeof(dd));
for (int i = 0; i < MAXN; i++) ls[i] = {inf, inf};
for (int i = 1; i <= n; i++) {
cin >> c[i];
c[i] += c[i - 1]; comp.push_back(c[i]);
}
sort(comp.begin(), comp.end()); comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
for (int i = 0; i <= n; i++) c2[i] = lower_bound(comp.begin(), comp.end(), c[i]) - comp.begin();
for (int i = n; i >= 0; i--) {
lst[i] = dd[c2[i]] != -1 ? dd[c2[i]] : inf;
dd[c2[i]] = i;
swap(ls[c2[i]].first, ls[c2[i]].second);
ls[c2[i]].second = i;
if (ls[c2[i]].first < mnn) {
mnn = ls[c2[i]].first; pos = ls[c2[i]].second;
}
pp[i] = pos;
}
for (int i = n; i >= 0; i--) {
if (lst[i] == inf) nxt[0][i] = inf;
else nxt[0][i] = pp[lst[i]];
dep[i] = (nxt[0][i] == inf ? 1 : 1 + dep[nxt[0][i]]);
}
for (int i = 1; i < inf2; i++) for (int j = 0; j <= n; j++) {
if (nxt[i - 1][j] == inf) { nxt[i][j] = inf; }
else nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int cur = pp[l - 1];
if (cur == inf || lst[cur] > r) {
cout << "0\n";
continue;
}
for (int i = inf2 - 1; i >= 0; i--) {
if (nxt[i][cur] != inf && nxt[i][cur] <= r) {
cur = nxt[i][cur];
}
}
int z = -dep[cur] + dep[pp[l - 1]];
if (lst[cur] <= r) cur = lst[cur], z++;
cout << z << '\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... |