/
fastio.cpp
98 lines (93 loc) · 2.39 KB
/
fastio.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
#include <cstdint>
#include <cstdio>
#include <type_traits>
#include <iostream>
#ifdef _WIN32
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#define fputs_unlocked fputs
#endif
using std::is_integral; using std::is_unsigned; using std::is_signed;
using std::make_unsigned;
using std::enable_if;
struct unlocked_cin
{
char _whitespace_skip()
{
char lchar;
while(isspace(lchar = getchar_unlocked())) {}
return lchar;
}
unlocked_cin& operator>> (char& x)
{
x = _whitespace_skip();
return *this;
}
template<typename T>
typename enable_if<is_integral<T>::value && is_unsigned<T>::value, unlocked_cin&>::type
operator>> (T& x)
{
x = 0;
char lchar = _whitespace_skip();
do {
x *= 10, x += lchar - '0';
} while(isdigit(lchar = getchar_unlocked()));
return *this;
}
template<typename T>
typename enable_if<is_integral<T>::value && is_signed<T>::value, unlocked_cin&>::type
operator>> (T& x)
{
bool s = false;
x = 0;
char lchar = _whitespace_skip();
if(lchar == '-')
s = true;
else
x += lchar - '0';
while(isdigit(lchar = getchar_unlocked()))
x *= 10, x += lchar - '0';
if(s) x = -x;
return *this;
}
template<typename... Args>
void tie(Args...) {}
} ucin;
struct unlocked_cout
{
unlocked_cout& operator<< (const char* c)
{
fputs_unlocked(c, stdout);
return *this;
}
unlocked_cout& operator<< (char x)
{
putchar_unlocked(x);
return *this;
}
template<typename T>
typename enable_if<is_integral<T>::value && is_unsigned<T>::value, unlocked_cout&>::type
operator<< (T x)
{
static char buffer[64];
size_t i = 0;
if(not x)
buffer[i++] = '0';
while(x)
buffer[i++] = (x % 10) + '0', x /= 10;
for(; i --> 0; )
putchar_unlocked(buffer[i]);
return *this;
}
template<typename T>
typename enable_if<is_integral<T>::value && is_signed<T>::value, unlocked_cout&>::type
operator<< (T x)
{
if(x < 0)
putchar_unlocked('-'), x = -x;
return *this << static_cast<typename make_unsigned<T>::type>(x);
}
template<typename... Args>
void tie(Args...) {}
} ucout;