-
Notifications
You must be signed in to change notification settings - Fork 46
/
fast_io.cpp
72 lines (63 loc) · 1.37 KB
/
fast_io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
// some damn fast I/O
#define GETCHAR getchar_unlocked
#define FWRITE fwrite_unlocked
const int BUFSIZE = 1<<16;
static char outbuf[BUFSIZE];
int outbuf_size = 0;
void init_io() {
ios_base::sync_with_stdio(false);
}
void flush() {
FWRITE(outbuf, outbuf_size, 1, stdout);
outbuf_size = 0;
}
template <typename T>
bool read_num(T &x) {
register int c = GETCHAR();
x = 0;
bool neg = false;
while (!isdigit(c) && c != '-' && c != EOF)
c = GETCHAR();
if (c == EOF) return false;
if (c == '-') { neg = true; c = GETCHAR(); }
while (isdigit(c)) {
x = x * 10 + c - '0';
c = GETCHAR();
}
if (neg) x = -x;
return true;
}
void write_char(char c) {
if (outbuf_size == BUFSIZE)
flush();
outbuf[outbuf_size++] = c;
}
void write_str(const char *c) {
while (*c) write_char(*(c++));
}
// because we only flush when trying to write, we always have
// at least one byte in the buffer after every write operation
bool unwrite() {
if (!outbuf_size)
return false;
outbuf_size--;
return true;
}
template <typename T>
void write_num(T x) {
if (x < 0) { write_char('-'); x = -x; }
static int digs[25];
register int i = 0;
while (x) {
digs[i++] = x % 10; x /= 10;
}
if (i) {
while (i) {
write_char(digs[i-1] + '0'); --i;
}
} else write_char('0');
}