Skip to content

Commit 659c118

Browse files
committed
fix: fixed some utils.
1 parent 1cf9b85 commit 659c118

4 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/alfred/algorithm/utils.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
#include <vector>
88

99
template <class T>
10-
inline void chkmax(T &x, T y) {
11-
if (x < y) x = y;
10+
inline bool chkmax(T &x, T y) {
11+
if (x < y) {
12+
x = y;
13+
return true;
14+
}
15+
return false;
1216
}
1317

1418
template <class T>
15-
inline void chkmin(T &x, T y) {
16-
if (x > y) x = y;
19+
inline bool chkmin(T &x, T y) {
20+
if (x > y) {
21+
x = y;
22+
return true;
23+
}
24+
return false;
1725
}
1826

1927
template <class T>

src/alfred/all

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ inline void Yes_No(bool cond) { cond ? Yes() : No(); }
6262

6363
std::ostream &operator<<(std::ostream &os, i128 n) {
6464
std::string s;
65+
if (n == 0) {
66+
return os << "0";
67+
}
6568
if (n < 0) {
6669
os << '-', n = -n;
6770
}

src/alfred/math/utils.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ inline T isqrt(T x) {
3232
return res;
3333
}
3434

35+
using i128 = __int128;
36+
37+
template <>
38+
inline i128 isqrt<i128>(i128 x) {
39+
i128 L = 0, R = 13043817825332782ll, mid;
40+
while (L < R) {
41+
mid = (L + R + 1) >> 1;
42+
if (mid * mid > x) {
43+
R = mid - 1;
44+
} else {
45+
L = mid;
46+
}
47+
}
48+
return L;
49+
}
50+
51+
inline i128 abs128(i128 x) {
52+
return x < 0 ? -x : x;
53+
}
54+
3555
template <class T, class V>
3656
inline bool in_range(V v, T l, T r) {
3757
return l <= v && v <= r;

src/alfred/string/hashed-string.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ struct SingleHash {
5555
};
5656

5757
struct HashedString {
58-
using SH1 = SingleHash<
58+
SingleHash<
5959
ctrandom::MOD_POOL[ctrandom::pick_idx1()],
60-
ctrandom::SEED_POOL[ctrandom::pick_idx1()]>;
61-
using SH2 = SingleHash<
60+
ctrandom::SEED_POOL[ctrandom::pick_idx1()]>
61+
H1;
62+
SingleHash<
6263
ctrandom::MOD_POOL[ctrandom::pick_idx2()],
63-
ctrandom::SEED_POOL[ctrandom::pick_idx2()]>;
64-
SH1 H1;
65-
SH2 H2;
64+
ctrandom::SEED_POOL[ctrandom::pick_idx2()]>
65+
H2;
6666
HashedString(void) = default;
6767
HashedString(std::string s) : H1(s), H2(s) {}
6868
inline void init(std::string s) {

0 commit comments

Comments
 (0)