-
Notifications
You must be signed in to change notification settings - Fork 2
/
fast input.cpp
55 lines (51 loc) · 938 Bytes
/
fast input.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
/**
* Description: Fast Input & Output
* Usage: readInt, writeInt
* Source: https://github.com/dragonslayerx
*/
#ifdef ONLINE_JUDGE
#define GETCHAR getchar_unlocked
#define PUTCHAR putchar_unlocked
#endif
#ifndef ONLINE_JUDGE
#define GETCHAR getchar
#define PUTCHAR putchar
#endif
template <typename T>
inline void readInt(T &n){
n = 0;
bool flag=1;
char c;
int sign=1;
while (1){
c = GETCHAR();
if(c=='-') sign=-1;
else if(c>='0'&&c<='9') {n = n * 10 + c - '0';flag=0;}
else if(flag!=1) break;
}
n *= sign;
}
template <typename T>
inline void writeInt(T x) {
bool sign = false;
if (x < 0) {
x = -x;
}
bool flag = false;
char buf[20];
int i = 0;
while (x > 0) {
flag = true;
buf[i++] = x % 10 + '0';
x /= 10;
}
if (flag) {
if (sign) PUTCHAR('-');
i--;
for (; i >=0; i--) {
PUTCHAR(buf[i]);
}
} else {
PUTCHAR('0');
}
}