이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 [intervals, nxt, mp] = [&]() -> tuple<vector<pair<int, 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, 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(intervals.size());
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] = intervals[u].second <= r ? dsu.getDepth(u) + 1 : 0;
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
}
컴파일 시 표준 에러 (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... |