|
6 | 6 |
|
7 | 7 | class FastIO { |
8 | 8 | private: |
9 | | - int siz = 100000; |
| 9 | + int chunk = 1 << 18; |
10 | 10 | char *buf, *p1, *p2; |
| 11 | + char *obuf, *op; |
| 12 | + |
| 13 | + inline void flush_output(void) { |
| 14 | + fwrite(obuf, 1, len, stdout), op = obuf; |
| 15 | + } |
11 | 16 |
|
12 | 17 | public: |
13 | 18 | inline char nc(void) { |
14 | | - return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, siz, stdin), p1 == p2) ? EOF : *p1++; |
| 19 | + if (p1 == p2) { |
| 20 | + p2 = (p1 = buf) + fread(buf, 1, chunk, stdin); |
| 21 | + if (p1 == p2) return EOF; |
| 22 | + } else return *p1++; |
| 23 | + } |
| 24 | + inline void pc(char c) { |
| 25 | + if (op == obuf + chunk) flush_output(); |
| 26 | + *op++ = c; |
| 27 | + } |
| 28 | + FastIO(void) : chunk(1 << 18) { |
| 29 | + p1 = p2 = buf = (char *)(malloc(chunk)); |
| 30 | + op = obuf = (char *)(malloc(chunk)); |
| 31 | + } |
| 32 | + FastIO(int _chunk) : chunk(_chunk) { |
| 33 | + p1 = p2 = buf = (char *)(malloc(chunk)); |
| 34 | + op = obuf = (char *)(malloc(chunk)); |
15 | 35 | } |
16 | | - inline void pc(char c) { putchar(c); } |
17 | | - FastIO(void) { buf = (char *)malloc(siz); } |
18 | | - FastIO(int n) : siz(n) { buf = (char *)malloc(siz); } // n characters. |
19 | | - ~FastIO(void) { free(buf); } |
20 | | - inline void resize(int n) { |
21 | | - siz = n; |
22 | | - buf = (char *)realloc(buf, siz); |
| 36 | + ~FastIO(void) { |
| 37 | + flush_output(); |
| 38 | + free(buf), free(obuf); |
23 | 39 | } |
| 40 | + inline void flush(void) { flush_output(); } |
24 | 41 | } __buf; |
25 | 42 |
|
26 | | -inline void reset_size(int siz) { |
27 | | - __buf.resize(siz); |
28 | | -} |
| 43 | +inline void pc(char c) { __buf.pc(c); } |
29 | 44 |
|
30 | 45 | template <class T> |
31 | 46 | inline void fast_read(T &x) { |
32 | 47 | x = 0; |
| 48 | + static bool neg = false; |
33 | 49 | static char c = __buf.nc(); |
34 | | - while (c < '0' || c > '9') c = __buf.nc(); |
| 50 | + while (c < '0' || c > '9') { |
| 51 | + neg = (c == '-'), c = __buf.nc(); |
| 52 | + } |
35 | 53 | while (c >= '0' && c <= '9') { |
36 | 54 | x = (x << 1) + (x << 3) + (c ^ 48), c = __buf.nc(); |
37 | 55 | } |
| 56 | + if (neg) x = -x; |
38 | 57 | } |
39 | 58 | template <class T> |
40 | 59 | inline void write(T x) { |
41 | | - if (x < 0) putchar('-'), x = -x; |
| 60 | + if (x < 0) { |
| 61 | + __buf.pc('-'), x = -x; |
| 62 | + } |
42 | 63 | if (x > 9) write(x / 10); |
43 | | - putchar((x % 10) ^ 48); |
| 64 | + __buf.pc((x % 10) ^ 48); |
44 | 65 | } |
45 | 66 | template <int mod> |
46 | 67 | inline void write(ModInt<mod> x) { |
47 | 68 | write(x.x); |
48 | 69 | } |
| 70 | +template <typename T, typename... V> |
| 71 | +inline void write(T x, V... v) { |
| 72 | + write(x), __buf.pc(' '), write(v...); |
| 73 | +} |
49 | 74 | template <class T> |
50 | 75 | inline void writeln(T x) { |
51 | | - write(x), puts(""); |
| 76 | + write(x), __buf.pc('\n'); |
52 | 77 | } |
53 | 78 | template <typename T, typename... V> |
54 | 79 | inline void writeln(T x, V... v) { |
55 | | - write(x), putchar(' '), writeln(v...); |
| 80 | + write(x), __buf.pc(' '), writeln(v...); |
56 | 81 | } |
57 | 82 | template <typename T, typename... V> |
58 | 83 | inline void fast_read(T &t, V &...v) { |
|
0 commit comments