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;
struct DSU {
vector<int> f;
vector<int> depth;
DSU(int n) : f(n), depth(n, 0) { iota(f.begin(), f.end(), 0); }
void fix(int u) {
if (f[u] == u) return;
fix(f[u]);
depth[u] += depth[f[u]];
f[u] = f[f[u]];
}
void unionize(int u, int p) {
f[u] = p;
depth[u] = 1;
}
int getDepth(int u) {
fix(u);
return depth[u];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
auto [k, nxt, mp] = [&]() -> tuple<int, vector<int>, vector<vector<pair<int, int>>>> {
vector<pair<long long, int>> a(n + 1);
a[0] = {0, 0};
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[i + 1] = { a[i].first + x, i + 1 };
}
sort(a.begin(), a.end());
vector<pair<int, int>> intervals;
for (int i = 0; i < n; i++) {
if (a[i].first == a[i + 1].first) {
intervals.push_back({a[i].second, a[i + 1].second});
}
}
vector<pair<long long, int>>().swap(a);
sort(intervals.begin(), intervals.end(), [&](pair<int, int> x, pair<int, int> y) {
return x.second < y.second;
});
vector<int> nxt(n + 1, -1);
int last_l = -1, last_r = -1;
for (int i = 0; i < intervals.size(); i++) {
auto [l, r] = intervals[i];
if (l <= last_l) continue;
nxt[l] = i;
last_l = l;
last_r = r;
}
for (int i = n - 1; i >= 0; i--) {
if (nxt[i] == -1) nxt[i] = nxt[i + 1];
}
vector<vector<pair<int, int>>> mp(n + 1);
last_l = -1, last_r = -1;
for (int i = 0; i < intervals.size(); i++) {
auto [l, r] = intervals[i];
if (l <= last_l) continue;
if (nxt[r] != -1) {
mp[intervals[nxt[r]].second].push_back({i, nxt[r]});
}
last_l = l;
last_r = r;
}
return {intervals.size(), nxt, mp};
}();
int q;
cin >> q;
vector<vector<pair<int, int>>> queries(n + 1);
vector<int> ans(q, -1);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
queries[r].push_back({l, i});
}
DSU dsu(k);
for (int r = 0; r <= n; r++) {
for (auto [u, p] : mp[r]) {
dsu.unionize(u, p);
}
for (auto [l, i] : queries[r]) {
int u = nxt[l];
if (u == -1) ans[i] = 0;
else ans[i] = dsu.getDepth(u) + 1;
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
}
Compilation message (stderr)
sumzero.cpp: In lambda function:
sumzero.cpp:55:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
sumzero.cpp:67:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
sumzero.cpp:54:26: warning: variable 'last_r' set but not used [-Wunused-but-set-variable]
54 | int last_l = -1, last_r = -1;
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |