-
Notifications
You must be signed in to change notification settings - Fork 128
/
buffered_reader.h
38 lines (36 loc) · 916 Bytes
/
buffered_reader.h
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
// Buffered reader {{{
namespace IO {
const int BUFSIZE = 1<<14;
char buf[BUFSIZE + 1], *inp = buf;
bool reacheof;
char get_char() {
if (!*inp && !reacheof) {
memset(buf, 0, sizeof buf);
int tmp = fread(buf, 1, BUFSIZE, stdin);
if (tmp != BUFSIZE) reacheof = true;
inp = buf;
}
return *inp++;
}
template<typename T>
T get() {
int neg = 0;
T res = 0;
char c = get_char();
while (!std::isdigit(c) && c != '-' && c != '+') c = get_char();
if (c == '+') { neg = 0; }
else if (c == '-') { neg = 1; }
else res = c - '0';
c = get_char();
while (std::isdigit(c)) {
res = res * 10 + (c - '0');
c = get_char();
}
return neg ? -res : res;
}
};
// Helper methods
int ri() {
return IO::get<int>();
}
// }}}