# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
857030 | chanhchuong123 | Sum Zero (RMI20_sumzero) | C++14 | 252 ms | 29780 KiB |
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;
#define task ""
const int MAX = 400040;
int n, q;
int c[MAX];
int to[MAX];
int ans[MAX];
long long sf = 0;
int jump[2][MAX];
int l[MAX], r[MAX];
map<long long, int> last;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
}
for (int i = 1; i <= n + 2; ++i) {
to[i] = n + 2;
}
last[0] = n + 1;
for (int i = n; i >= 1; --i) {
sf += c[i];
to[i] = min(to[i], to[i + 1]);
if (last[sf] > 0) to[i] = min(to[i], last[sf]); last[sf] = i;
}
cin >> q;
for (int i = 1; i <= q; ++i) {
cin >> l[i] >> r[i];
}
for (int j = 18; j >= 0; --j) {
for (int i = 1; i <= n + 2; ++i) {
jump[0][i] = to[i];
}
for (int k = 1; k <= j; ++k) {
int prv = (k - 1) & 1, cur = k & 1;
for (int i = 1; i <= n + 2; ++i) {
jump[cur][i] = jump[prv][jump[prv][i]];
}
}
for (int i = 1; i <= q; ++i) {
if (jump[j & 1][l[i]] - 1 <= r[i]) {
l[i] = jump[j & 1][l[i]];
ans[i] += 1 << j;
}
}
}
for (int i = 1; i <= q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |