File tree 2 files changed +22
-8
lines changed
2 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -15,5 +15,15 @@ namespace cp_algo {
15
15
size_t kth_set_bit (uint64_t x, size_t k) {
16
16
return std::countr_zero (_pdep_u64 (1ULL << k, x));
17
17
}
18
+ template <int fl = 0 >
19
+ void with_bit_floor (size_t n, auto &&callback) {
20
+ if constexpr (fl >= 63 ) {
21
+ return ;
22
+ } else if (n >> (fl + 1 )) {
23
+ with_bit_floor<fl + 1 >(n, callback);
24
+ } else {
25
+ callback.template operator ()<1ULL << fl>();
26
+ }
27
+ }
18
28
}
19
29
#endif // CP_ALGO_UTIL_BIT_HPP
Original file line number Diff line number Diff line change 1
1
#ifndef CP_ALGO_UTIL_SORT_HPP
2
2
#define CP_ALGO_UTIL_SORT_HPP
3
+ #include " bit.hpp"
3
4
#include < algorithm>
4
5
#include < numeric>
5
6
#include < ranges>
6
7
#include < vector>
7
8
namespace cp_algo {
8
- void count_sort (auto &a, size_t maxc, auto &&proj = std::identity{}) {
9
- std::vector<int > cnt (maxc);
9
+ template <size_t maxc>
10
+ void count_sort (auto &a, auto &&proj = std::identity{}) {
11
+ std::array<int , maxc> cnt = {};
10
12
for (auto &x: a) {
11
13
cnt[proj (x)]++;
12
14
}
@@ -22,13 +24,15 @@ namespace cp_algo {
22
24
if (empty (a)) {
23
25
return ;
24
26
}
25
- int base = std::bit_floor (size (a));
26
27
auto mx = std::ranges::max (a);
27
- for (int64_t i = 1 ; i <= mx; i *= base) {
28
- count_sort (a, base, [&](auto x) {
29
- return x / i % base;
30
- });
31
- }
28
+ with_bit_floor (size (a), [&]<size_t floor >() {
29
+ constexpr int base = std::min<size_t >(floor , 1 << 16 );
30
+ for (int64_t i = 1 ; i <= mx; i *= base) {
31
+ count_sort<base>(a, [&](auto x) {
32
+ return x / i % base;
33
+ });
34
+ }
35
+ });
32
36
}
33
37
}
34
38
#endif // CP_ALGO_UTIL_SORT_HPP
You can’t perform that action at this time.
0 commit comments