Skip to content

Commit a4cff83

Browse files
committed
feat: Implement 128-bit integer read/write functions in FastIO and add corresponding test
1 parent 3b3faf7 commit a4cff83

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

test/many_aplusb_128bit.test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/many_aplusb_128bit"
2+
3+
#include "../weilycoder/fast-io.hpp"
4+
using namespace weilycoder;
5+
6+
static FastIOFile<> io;
7+
8+
int main() {
9+
size_t t = io.read_u64();
10+
while (t--) {
11+
auto a = io.read_i128();
12+
auto b = io.read_i128();
13+
io.write_i128_line(a + b);
14+
}
15+
return 0;
16+
}

weilycoder/fast-io.hpp

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,36 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
116116

117117
inline void flush() { writer.flush(); }
118118

119-
inline uint64_t read_u64() {
119+
int32_t abs(int32_t x) { return x >= 0 ? x : -x; }
120+
121+
template <typename utype> inline utype _read_u() {
120122
char c;
121123
do
122124
c = getchar();
123125
while (c < '0' || c > '9');
124-
uint64_t x = 0;
126+
utype x = 0;
125127
do
126128
x = x * 10 + (c - '0'), c = getchar();
127129
while (c >= '0' && c <= '9');
128130
return x;
129131
}
130132

131-
inline void write_u64(uint64_t x) {
132-
static char buf[20];
133+
template <typename itype, typename utype> inline itype _read_i() {
134+
char c;
135+
bool neg = false;
136+
do
137+
if ((c = getchar()) == '-')
138+
neg = true;
139+
while ((c < '0' || c > '9'));
140+
utype x = 0;
141+
do
142+
x = x * 10 + (c - '0'), c = getchar();
143+
while (c >= '0' && c <= '9');
144+
return static_cast<itype>(neg ? -x : x);
145+
}
146+
147+
template <typename utype, size_t bufsize> inline void _write_u(utype x) {
148+
static char buf[bufsize];
133149
size_t len = 0;
134150
do
135151
buf[len++] = '0' + (x % 10), x /= 10;
@@ -138,7 +154,47 @@ template <typename Reader, typename Writer, bool debug = false> struct FastIO {
138154
putchar(buf[i]);
139155
}
140156

157+
template <typename itype, size_t bufsize> inline void _write_i(itype x) {
158+
bool neg = x < 0;
159+
static char buf[bufsize];
160+
size_t len = 0;
161+
do
162+
buf[len++] = '0' + this->abs(x % 10), x /= 10;
163+
while (x);
164+
if (neg)
165+
putchar('-');
166+
for (size_t i = len - 1; ~i; --i)
167+
putchar(buf[i]);
168+
}
169+
170+
inline int32_t read_i32() { return _read_i<int32_t, uint32_t>(); }
171+
inline uint32_t read_u32() { return _read_u<uint32_t>(); }
172+
173+
inline void write_i32(int32_t x) { _write_i<int32_t, 10>(x); }
174+
inline void write_u32(uint32_t x) { _write_u<uint32_t, 10>(x); }
175+
176+
inline int64_t read_i64() { return _read_i<int64_t, uint64_t>(); }
177+
inline uint64_t read_u64() { return _read_u<uint64_t>(); }
178+
179+
inline void write_i64(int64_t x) { _write_i<int64_t, 20>(x); }
180+
inline void write_u64(uint64_t x) { _write_u<uint64_t, 20>(x); }
181+
182+
inline __int128 read_i128() { return _read_i<__int128, unsigned __int128>(); }
183+
inline unsigned __int128 read_u128() { return _read_u<unsigned __int128>(); }
184+
185+
inline void write_i128(__int128 x) { _write_i<__int128, 40>(x); }
186+
inline void write_u128(unsigned __int128 x) {
187+
_write_u<unsigned __int128, 40>(x);
188+
}
189+
190+
inline void write_i32_line(int32_t x) { write_i32(x), putchar('\n'); }
191+
inline void write_u32_line(uint32_t x) { write_u32(x), putchar('\n'); }
192+
inline void write_i64_line(int64_t x) { write_i64(x), putchar('\n'); }
141193
inline void write_u64_line(uint64_t x) { write_u64(x), putchar('\n'); }
194+
inline void write_i128_line(__int128 x) { write_i128(x), putchar('\n'); }
195+
inline void write_u128_line(unsigned __int128 x) {
196+
write_u128(x), putchar('\n');
197+
}
142198
};
143199

144200
template <bool debug = false>

0 commit comments

Comments
 (0)