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>
#warning That's the baby, that's not my baby
typedef long long ll;
using namespace std;
const int NMAX = 4e5;
const int QMAX = 4e5;
vector<pair<int, int>>queries[QMAX + 1];
int l[QMAX + 1], r[QMAX + 1], answer[QMAX + 1];
ll sum[NMAX + 5];
int nxt[NMAX + 5];
const int p2[18] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072};
vector<int>g[NMAX + 1];
bitset<NMAX + 1>vis;
int st[NMAX + 1];
int top;
void dfs (int u) {
vis[u] = true;
st[++top] = u;
// cout << u << " ";
// cout << "! ";
// for (int i = 1; i <= top; i++) {
// cout << st[i] << ' ';
// }
// cout << '\n';
for (const auto &[pos, index] : queries[u]) {
int l = 1, r = top;
while (l < r) {
int mid = (l + r) / 2;
if (pos >= st[mid]) {
r = mid;
} else {
l = mid + 1;
}
}
// cout << "? " << st[top] << " - " << pos << '\n';
answer[index] = top - r;
}
for (const auto &v : g[u]) {
if (!vis[v]) {
dfs(v);
}
}
top--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> sum[i];
sum[i] += sum[i - 1];
}
map<int, int>pos;
nxt[n + 1] = n + 2;
for (int i = n; i > 0; i--) {
pos[sum[i]] = i;
if (!pos.count(sum[i - 1])) {
nxt[i] = n + 2;
} else {
nxt[i] = pos[sum[i - 1]] + 1;
}
nxt[i] = min(nxt[i], nxt[i + 1]);
g[nxt[i]].push_back(i);
// cout << " " << nxt[i] << ' ' << i << '\n';
}
int q;
cin >> q;
int lg2N = __lg(n);
for (int i = 1; i <= q; i++) {
cin >> l[i] >> r[i];
r[i]++;
queries[l[i]].push_back({r[i], i});
}
for (int i = n + 2; i > 0; i--) {
if (!vis[i]) {
dfs(i);
}
}
for (int i = 1; i <= q; i++) {
cout << answer[i] << '\n';
}
return 0;
}
Compilation message (stderr)
sumzero.cpp:2:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
2 | #warning That's the baby, that's not my baby
| ^~~~~~~
sumzero.cpp: In function 'int main()':
sumzero.cpp:88:9: warning: unused variable 'lg2N' [-Wunused-variable]
88 | int lg2N = __lg(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... |