# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
990230 | duzinho039 | Sum Zero (RMI20_sumzero) | C++14 | 530 ms | 55252 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;
using vi = vector<int>;
using vvi = vector<vi>;
using ii = pair<int, int>;
using vii = vector<ii>;
vi c;
vii queries;
int n, q;
void read() {
cin >> n;
c.resize(n);
for(int &x: c) cin >> x;
cin >> q;
queries.resize(q);
for(auto &[l, r]: queries) cin >> l >> r;
}
const int MXL = 15;
void solve() {
map<int, int> where;
vvi anc(n, vi(MXL, -100));
int currsum = 0;
int prev = -100;
for(int i = 0; i < n; ++i) {
currsum += c[i];
if(where.find(currsum) != where.end()) {
prev = max(prev, where[currsum]);
}
if(currsum == 0) {
prev = max(prev, -1);
}
anc[i][0] = prev;
for(int b = 1; b < MXL; ++b) {
if(anc[i][b - 1] <= -1) {
break;
}
anc[i][b] = anc[anc[i][b - 1]][b - 1];
}
where[currsum] = i;
}
for(auto [l, r]: queries) {
l--;
r--;
int ans = 0;
for(int b = MXL - 1; b >= 0; --b) {
if(r == -1) break;
int nr = anc[r][b];
if(nr >= l - 1) {
r = nr;
ans |= 1 << b;
}
}
cout << ans << '\n';
}
}
int main() {
read();
solve();
}
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... |