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 = 4e5 + 25;
#define int long long
#define ll long long
const ll inf = 1e9;
ll c[MAXN], n;
ll lst[MAXN];
ll pp[MAXN];
ll nxt[20][MAXN];
map <ll, ll> dd;
int ans[MAXN];
map <int, vector <int>> ls;
set <pair <int, int>> po;
int dep[MAXN];
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
c[i] += c[i - 1];
}
for (int i = n; i >= 0; i--) {
lst[i] = dd.count(c[i]) ? dd[c[i]] : inf;
dd[c[i]] = i;
int z = ls[c[i]].size();
if (z >= 2) {
po.erase({ls[c[i]][z - 2], ls[c[i]][z - 1]});
}
z++; ls[c[i]].push_back(i);
if (z >= 2) {
po.insert({ls[c[i]][z - 2], ls[c[i]][z - 1]});
}
if (po.empty()) pp[i] = inf;
else pp[i] = (*(po.begin())).second;
}
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]]);
if (lst[i] <= n && nxt[0][i] == inf) dep[i] = 1 + dep[lst[i]];
}
for (int i = 0; i <= n; i++) {
cout << i << ": " << dep[i] << " " << nxt[0][i] << " " << c[i] << " " << lst[i] << " " << pp[i] << '\n';
}
for (int i = 1; i < 20; 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];
for (int i = 19; i >= 0; i--) {
if (nxt[i][cur] != inf && nxt[i][cur] <= r) {
cur = nxt[i][cur];
}
}
if (lst[cur] <= r) cur = lst[cur];
cout << -dep[cur] + dep[pp[l - 1]] << '\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... |