Skip to content

Commit

Permalink
feat: upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphereth-A committed Apr 13, 2024
1 parent e3d9d91 commit d45dfd5
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 18 deletions.
28 changes: 14 additions & 14 deletions src/code/ds/d_ary_heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ constexpr I l_ch_(I it_ch1, int num_ch, C &&c) {
}
} // namespace d_ary_heap_impl_

template <int D, class I, class C>
constexpr void make_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
u32 len = end - begin;
template <int D, class I, class C = std::less<>>
constexpr void make_dary_heap(I begin, I end, C &&comp = C{}) {
u32 len = u32(end - begin);
if (len <= 1) return;
u32 idx = (len - 2) / D;
if (int num_ch_end = (len - 1) % D; num_ch_end) {
Expand All @@ -88,24 +88,24 @@ constexpr void make_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
I ch = begin;
if (idx_ch < len) ch = d_ary_heap_impl_::l_ch_<D>(begin + idx_ch1, comp);
else if (idx_ch1 >= len) break;
else ch = d_ary_heap_impl_::l_ch_<D>(begin + idx_ch1, len - idx_ch1, comp);
else ch = d_ary_heap_impl_::l_ch_<D>(begin + idx_ch1, int(len - idx_ch1), comp);
if (!comp(value, *ch)) break;
begin[mdidx] = std::move(*ch);
mdidx = ch - begin;
mdidx = u32(ch - begin);
}
begin[mdidx] = std::move(value);
if (idx == 0) break;
}
}
template <int D, class I, class C>
constexpr bool is_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
template <int D, class I, class C = std::less<>>
constexpr bool is_dary_heap(I begin, I end, C &&comp = C{}) {
u32 len = u32(end - begin);
for (u32 i = 1; i < len; ++i)
if (u32 p = d_ary_heap_impl_::pidx_<D>(i); comp(begin[p], begin[i])) return false;
return true;
}
template <int D, class I, class C>
constexpr void push_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
template <int D, class I, class C = std::less<>>
constexpr void push_dary_heap(I begin, I end, C &&comp = C{}) {
typename std::iterator_traits<I>::value_type val = std::move(end[-1]);
u32 idx = u32(end - begin) - 1;
while (idx > 0) {
Expand All @@ -116,8 +116,8 @@ constexpr void push_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
}
begin[idx] = std::move(val);
}
template <int D, class I, class C>
constexpr void pop_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
template <int D, class I, class C = std::less<>>
constexpr void pop_dary_heap(I begin, I end, C &&comp = C{}) {
u32 len = u32(end - begin) - 1;
typename std::iterator_traits<I>::value_type val = std::move(end[-1]);
end[-1] = std::move(begin[0]);
Expand All @@ -127,11 +127,11 @@ constexpr void pop_dary_heap(I begin, I end, C &&comp = std::less<>{}) {
I ch = d_ary_heap_impl_::l_ch_<D>(begin + ch1, comp);
if (!comp(val, *ch)) break;
begin[idx] = std::move(*ch);
idx = ch - begin;
idx = u32(ch - begin);
} else if (ch1 < len) {
if (I ch = d_ary_heap_impl_::l_ch_<D>(begin + ch1, len - ch1, comp); comp(val, *ch)) {
if (I ch = d_ary_heap_impl_::l_ch_<D>(begin + ch1, int(len - ch1), comp); comp(val, *ch)) {
begin[idx] = std::move(*ch);
idx = ch - begin;
idx = u32(ch - begin);
}
break;
} else break;
Expand Down
8 changes: 4 additions & 4 deletions src/code/graph/cle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ template <class T>
constexpr vec<std::tuple<T, u32, u32>> cle(u32 n, u32 root, vec<std::tuple<T, u32, u32>> const &arcs) {
ds::SkewHeap<T> heap;
ds::dsu_basic<> uf(n);
vec<u32> used(n, -1_u32), from(n), come(n, -1_u32);
vecu used(n, -1_u32), from(n), come(n, -1_u32);
vec<T> from_cost(n);

used[root] = root;
vec<u32> par_e(arcs.size(), -1_u32), stem(n, -1_u32), idxs;
vecu par_e(arcs.size(), -1_u32), stem(n, -1_u32), idxs;

for (u32 i = 0; i < arcs.size(); ++i) {
auto [w, u, v] = arcs[i];
Expand All @@ -26,7 +26,7 @@ constexpr vec<std::tuple<T, u32, u32>> cle(u32 n, u32 root, vec<std::tuple<T, u3
for (u32 start = 0; start < n; start++) {
if (~used[start]) continue;
u32 now = start;
vec<u32> chi_e;
vecu chi_e;
u32 cycle = 0;
while (!~used[now] || used[now] == start) {
used[now] = start;
Expand Down Expand Up @@ -65,7 +65,7 @@ constexpr vec<std::tuple<T, u32, u32>> cle(u32 n, u32 root, vec<std::tuple<T, u3
}
}

vec<u32> used_e(arcs.size());
vecu used_e(arcs.size());
vec<std::tuple<T, u32, u32>> res;
for (u32 _ = (u32)idxs.size(); _--;) {
u32 idx = idxs[_];
Expand Down
16 changes: 16 additions & 0 deletions src/test_cpverifier/aizu/alds1_9_b.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_9_B"

#include "../../code/ds/d_ary_heap.hpp"
#include "../../code/io/ios_container.hpp"

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
u32 n;
std::cin >> n;
vec<i32> v(n);
std::cin >> v;
tifa_libs::ds::make_dary_heap<2>(v.begin(), v.end());
std::cout << ' ' << v << '\n';
return 0;
}
26 changes: 26 additions & 0 deletions src/test_cpverifier/aizu/alds1_9_c.2-ary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_9_C"

#include "../../code/ds/d_ary_heap.hpp"

constexpr int D = 2;

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
strn s;
vec<u32> v;
while (std::cin >> s) {
if (s == "end") break;
if (s[0] == 'i') {
u32 x;
std::cin >> x;
v.push_back(x);
tifa_libs::ds::push_dary_heap<D>(v.begin(), v.end());
} else {
tifa_libs::ds::pop_dary_heap<D>(v.begin(), v.end());
std::cout << v.back() << '\n';
v.pop_back();
}
}
return 0;
}
26 changes: 26 additions & 0 deletions src/test_cpverifier/aizu/alds1_9_c.3-ary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_9_C"

#include "../../code/ds/d_ary_heap.hpp"

constexpr int D = 3;

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
strn s;
vec<u32> v;
while (std::cin >> s) {
if (s == "end") break;
if (s[0] == 'i') {
u32 x;
std::cin >> x;
v.push_back(x);
tifa_libs::ds::push_dary_heap<D>(v.begin(), v.end());
} else {
tifa_libs::ds::pop_dary_heap<D>(v.begin(), v.end());
std::cout << v.back() << '\n';
v.pop_back();
}
}
return 0;
}
26 changes: 26 additions & 0 deletions src/test_cpverifier/aizu/alds1_9_c.4-ary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_9_C"

#include "../../code/ds/d_ary_heap.hpp"

constexpr int D = 4;

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
strn s;
vec<u32> v;
while (std::cin >> s) {
if (s == "end") break;
if (s[0] == 'i') {
u32 x;
std::cin >> x;
v.push_back(x);
tifa_libs::ds::push_dary_heap<D>(v.begin(), v.end());
} else {
tifa_libs::ds::pop_dary_heap<D>(v.begin(), v.end());
std::cout << v.back() << '\n';
v.pop_back();
}
}
return 0;
}
24 changes: 24 additions & 0 deletions src/test_cpverifier/aizu/alds1_9_c.depq.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_9_C"

#include "../../code/ds/depq.hpp"

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
strn s;
vec<u32> v;
while (std::cin >> s) {
if (s == "end") break;
if (s[0] == 'i') {
u32 x;
std::cin >> x;
v.push_back(x);
tifa_libs::ds::push_minmax_heap(v.begin(), v.end());
} else {
tifa_libs::ds::pop_minmax_heap_max(v.begin(), v.end());
std::cout << v.back() << '\n';
v.pop_back();
}
}
return 0;
}
32 changes: 32 additions & 0 deletions src/test_cpverifier/aizu/itp2_2_c.2-ary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/all/ITP2_2_C"

#include "../../code/ds/d_ary_heap.hpp"

constexpr int D = 2;

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
u32 n, q;
std::cin >> n >> q;
vvec<i32> v(n);
for (u32 i = 0; i < q; ++i) {
u32 op, t;
std::cin >> op >> t;
if (op == 0) {
i32 x;
std::cin >> x;
v[t].push_back(x);
tifa_libs::ds::push_dary_heap<D>(v[t].begin(), v[t].end());
} else {
if (v[t].empty()) continue;
if (op == 1) {
std::cout << v[t].front() << '\n';
} else {
tifa_libs::ds::pop_dary_heap<D>(v[t].begin(), v[t].end());
v[t].pop_back();
}
}
}
return 0;
}
32 changes: 32 additions & 0 deletions src/test_cpverifier/aizu/itp2_2_c.3-ary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/all/ITP2_2_C"

#include "../../code/ds/d_ary_heap.hpp"

constexpr int D = 3;

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
u32 n, q;
std::cin >> n >> q;
vvec<i32> v(n);
for (u32 i = 0; i < q; ++i) {
u32 op, t;
std::cin >> op >> t;
if (op == 0) {
i32 x;
std::cin >> x;
v[t].push_back(x);
tifa_libs::ds::push_dary_heap<D>(v[t].begin(), v[t].end());
} else {
if (v[t].empty()) continue;
if (op == 1) {
std::cout << v[t].front() << '\n';
} else {
tifa_libs::ds::pop_dary_heap<D>(v[t].begin(), v[t].end());
v[t].pop_back();
}
}
}
return 0;
}
32 changes: 32 additions & 0 deletions src/test_cpverifier/aizu/itp2_2_c.4-ary.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/all/ITP2_2_C"

#include "../../code/ds/d_ary_heap.hpp"

constexpr int D = 4;

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
u32 n, q;
std::cin >> n >> q;
vvec<i32> v(n);
for (u32 i = 0; i < q; ++i) {
u32 op, t;
std::cin >> op >> t;
if (op == 0) {
i32 x;
std::cin >> x;
v[t].push_back(x);
tifa_libs::ds::push_dary_heap<D>(v[t].begin(), v[t].end());
} else {
if (v[t].empty()) continue;
if (op == 1) {
std::cout << v[t].front() << '\n';
} else {
tifa_libs::ds::pop_dary_heap<D>(v[t].begin(), v[t].end());
v[t].pop_back();
}
}
}
return 0;
}

0 comments on commit d45dfd5

Please sign in to comment.