이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct DSU {
};
//Bài như cầy, Author Pannda
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> nxt = [&] {
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});
}
}
a.clear();
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, n + 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] = r;
last_l = l;
last_r = r;
}
for (int i = n - 1; i >= 0; i--) {
if (nxt[i] == n + 1) nxt[i] = nxt[i + 1];
}
intervals.clear();
vector<pair<int, int>>().swap(intervals);
return nxt;
}();
int q;
cin >> q;
vector<array<int, 3>> queries(q);
vector<int> ans(q, -1);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
queries[i] = {r, l, i};
}
sort(queries.begin(), queries.end());
vector<int> f(n + 1, 0);
auto fix = [&](auto self, int i, int r) -> void {
if (nxt[i] > r) return;
f[i] = max(f[i], 1);
self(self, nxt[i], r);
f[i] += f[nxt[i]];
if (nxt[nxt[i]] <= r) nxt[i] = nxt[nxt[i]];
};
auto get = [&](int i, int r) {
fix(fix, i, r);
return f[i];
};
for (auto [r, l, i] : queries) {
ans[i] = get(l, r);
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
sumzero.cpp: In lambda function:
sumzero.cpp:36: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]
36 | for (int i = 0; i < intervals.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
sumzero.cpp:35:26: warning: variable 'last_r' set but not used [-Wunused-but-set-variable]
35 | 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... |