1 fread 和 fwrite 实现的超级读入输出挂(速度超快,但不支持终端交互)
2 getchar 和 putchar 实现的读入输出挂(比1慢一些,但支持终端交互)
3 整合1和2两个版本,通过本地宏定义,实现本地运行版本2,提交时运行1.(保留了两个版本的优势,又避免了两个版本的缺陷)
#include<bits/stdc++.h>
namespace fastIO
{
#define BUF_SIZE 100000
#define DECIMAL 6
#define LL long long
bool IOerror
= 0;
inline char nc() { static char buf
[BUF_SIZE
], * p1
= buf
+ BUF_SIZE
, * pend
= buf
+ BUF_SIZE
; if (p1
== pend
) { p1
= buf
; pend
= buf
+ fread(buf
, 1, BUF_SIZE
, stdin); if (pend
== p1
) { IOerror
= 1; return -1; } }return *p1
++; }
inline bool blank(char ch
) { return ch
== ' ' || ch
== '\n' || ch
== '\r' || ch
== '\t'; }
template<class T> inline bool read(T
& x
) { bool sign
= 0; char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; if (ch
== '-')sign
= 1, ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; if (sign
)x
= -x
; return true; }
inline bool read(unsigned long long& x
) { char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; return true; }
inline bool read(unsigned int& x
) { char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; return true; }
inline bool read(double& x
) { bool sign
= 0; char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; if (ch
== '-')sign
= 1, ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; if (ch
== '.') { double tmp
= 1; ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())tmp
/= 10.0, x
+= tmp
* (ch
- '0'); }if (sign
)x
= -x
; return true; }
inline bool read(char* s
) { char ch
= nc(); for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; !blank(ch
) && !IOerror
; ch
= nc())*s
++ = ch
; *s
= 0; return true; }
inline bool read(char& c
) { for (c
= nc(); blank(c
); c
= nc()); if (IOerror
) { c
= -1; return false; }return true; }
inline bool read(std
::string
& s
) { s
.clear(); char ch
= nc(); for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; !blank(ch
) && !IOerror
; ch
= nc())s
+= ch
; return true; }
template<class T, class... U
>bool read(T
& h
, U
&... t
) { return read(h
) && read(t
...); }
struct Ostream_fwrite
{
char* buf
, * p1
, * pend
;
Ostream_fwrite() { buf
= new char[BUF_SIZE
]; p1
= buf
; pend
= buf
+ BUF_SIZE
; }
inline void out(char ch
) { if (p1
== pend
) { fwrite(buf
, 1, BUF_SIZE
, stdout); p1
= buf
; }*p1
++ = ch
; }
template<class T>inline void print(T x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; if (x
< 0)out('-'), x
= -x
; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(char ch
) { out(ch
); }
inline void print(unsigned long long x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(unsigned int x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(double x
, int y
= DECIMAL
) {
static LL mul
[] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000,
1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL };
if (x
< -1e-12)out('-'), x
= -x
; x
*= mul
[y
];
LL x1
= (LL
)floor(x
); if (x
- floor(x
) >= 0.5)++x1
;
LL x2
= x1
/ mul
[y
], x3
= x1
- x2
* mul
[y
]; print(x2
);
if (y
> 0) { out('.'); for (size_t i
= 1; i
< y
&& x3
* mul
[i
] < mul
[y
]; out('0'), ++i
); print(x3
); }
}
inline void print(char* s
) { while (*s
)out(*s
++); }
inline void print(const char* s
) { while (*s
)out(*s
++); }
inline void print(std
::string s
) { print(s
.c_str()); }
inline void flush() { if (p1
!= buf
) { fwrite(buf
, 1, p1
- buf
, stdout); p1
= buf
; } }
~Ostream_fwrite() { flush(); }
}Ostream
;
template<class T>inline void print(T x
) { Ostream
.print(x
); }
template<class T>inline void println(T x
) { Ostream
.print(x
); Ostream
.out('\n'); }
inline void print(double x
, int y
= DECIMAL
) { Ostream
.print(x
, y
); }
inline void println(double x
, int y
= DECIMAL
) { Ostream
.print(x
, y
); Ostream
.out('\n');; }
template<class T, class... U
>void print(const T
& h
, const U
&... t
) { print(h
); Ostream
.out(' ');; print(t
...); }
template<class T, class... U
>void println(const T
& h
, const U
&... t
) { print(h
); Ostream
.out(' ');; println(t
...); }
inline void flush() { Ostream
.flush(); }
#undef LL
#undef DECIMAL
#undef BUF_SIZE
};
using namespace fastIO
;
int main()
{
#ifdef DEBUG
freopen("..\\DEBUG\\IO\\1.in", "r", stdin);
#endif
int a
;
long long b
;
unsigned long long c
;
unsigned int d
;
double e
;
char f
[10];
char g
;
std
::string h
;
read(a
, b
, c
, d
, e
, f
, g
, h
);
print(a
, b
, c
, d
, e
, f
, g
, h
);
println(a
, b
, c
, d
, e
, f
, g
, h
);
println("hello", 3.14, 3);
println(3.1415926);
println(3.1415926, 4);
println(3.1415926, 9);
println(3.1415926, 4, "hello");
fastIO
::flush();
return 0;
}
支持终端交互的版本
#include<bits/stdc++.h>
namespace fastIO
{
#define DECIMAL 6
#define LL long long
bool IOerror
= 0;
inline char nc() {
char ch
= getchar();
if (ch
== -1)IOerror
= 1;
return ch
;
}
inline bool blank(char ch
) { return ch
== ' ' || ch
== '\n' || ch
== '\r' || ch
== '\t'; }
template<class T> inline bool read(T
& x
) { bool sign
= 0; char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; if (ch
== '-')sign
= 1, ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; if (sign
)x
= -x
; return true; }
inline bool read(unsigned long long& x
) { char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; return true; }
inline bool read(unsigned int& x
) { char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; return true; }
inline bool read(double& x
) { bool sign
= 0; char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; if (ch
== '-')sign
= 1, ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; if (ch
== '.') { double tmp
= 1; ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())tmp
/= 10.0, x
+= tmp
* (ch
- '0'); }if (sign
)x
= -x
; return true; }
inline bool read(char* s
) { char ch
= nc(); for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; !blank(ch
) && !IOerror
; ch
= nc())*s
++ = ch
; *s
= 0; return true; }
inline bool read(char& c
) { for (c
= nc(); blank(c
); c
= nc()); if (IOerror
) { c
= -1; return false; }return true; }
inline bool read(std
::string
& s
) { s
.clear(); char ch
= nc(); for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; !blank(ch
) && !IOerror
; ch
= nc())s
+= ch
; return true; }
template<class T, class... U
>bool read(T
& h
, U
&... t
) { return read(h
) && read(t
...); }
struct OStream
{
inline void out(char ch
) { putchar(ch
); }
template<class T>inline void print(T x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; if (x
< 0)out('-'), x
= -x
; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(char ch
) { out(ch
); }
inline void print(unsigned long long x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(unsigned int x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(double x
, int y
= DECIMAL
) {
static LL mul
[] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000,
1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL };
if (x
< -1e-12)out('-'), x
= -x
; x
*= mul
[y
];
LL x1
= (LL
)floor(x
); if (x
- floor(x
) >= 0.5)++x1
;
LL x2
= x1
/ mul
[y
], x3
= x1
- x2
* mul
[y
]; print(x2
);
if (y
> 0) { out('.'); for (size_t i
= 1; i
< y
&& x3
* mul
[i
] < mul
[y
]; out('0'), ++i
); print(x3
); }
}
inline void print(char* s
) { while (*s
)out(*s
++); }
inline void print(const char* s
) { while (*s
)out(*s
++); }
inline void print(std
::string s
) { print(s
.c_str()); }
}Ostream
;
template<class T>inline void print(T x
) { Ostream
.print(x
); }
template<class T>inline void println(T x
) { Ostream
.print(x
); Ostream
.out('\n'); }
inline void print(double x
, int y
= DECIMAL
) { Ostream
.print(x
, y
); }
inline void println(double x
, int y
= DECIMAL
) { Ostream
.print(x
, y
); Ostream
.out('\n');; }
template<class T, class... U
>void print(const T
& h
, const U
&... t
) { print(h
); Ostream
.out(' ');; print(t
...); }
template<class T, class... U
>void println(const T
& h
, const U
&... t
) { print(h
); Ostream
.out(' ');; println(t
...); }
#undef LL
#undef DECIMAL
#undef BUF_SIZE
};
using namespace fastIO
;
int main()
{
int a
;
long long b
;
unsigned long long c
;
unsigned int d
;
double e
;
char f
[10];
char g
;
std
::string h
;
read(a
, b
, c
, d
, e
, f
, g
, h
);
print(a
, b
, c
, d
, e
, f
, g
, h
);
println(a
, b
, c
, d
, e
, f
, g
, h
);
println("hello", 3.14, 3);
println(3.1415926);
println(3.1415926, 4);
println(3.1415926, 9);
println(3.1415926, 4, "hello");
return 0;
}
整合后的版本3
(我将宏LOCAL 定义在了 头文件 stdc++.h 里面,这样引用stdc++就有LOCAL的定义)
#include<bits/stdc++.h>
namespace fastIO
{
#ifdef LOCAL
#define DEBUG
#endif
#define BUF_SIZE 100000
#define DECIMAL 6
#define LL long long
static bool IOerror
= 0;
#ifdef DEBUG
inline char nc() {char ch
= getchar();if (ch
== -1)IOerror
= 1;return ch
;}
#else
inline char nc() { static char buf
[BUF_SIZE
], * p1
= buf
+ BUF_SIZE
, * pend
= buf
+ BUF_SIZE
; if (p1
== pend
) { p1
= buf
; pend
= buf
+ fread(buf
, 1, BUF_SIZE
, stdin); if (pend
== p1
) { IOerror
= 1; return -1; } }return *p1
++; }
#endif
inline bool blank(char ch
) { return ch
== ' ' || ch
== '\n' || ch
== '\r' || ch
== '\t'; }
template<class T> inline bool read(T
& x
) { bool sign
= 0; char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; if (ch
== '-')sign
= 1, ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; if (sign
)x
= -x
; return true; }
inline bool read(unsigned long long& x
) { char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; return true; }
inline bool read(unsigned int& x
) { char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; return true; }
inline bool read(double& x
) { bool sign
= 0; char ch
= nc(); x
= 0; for (; blank(ch
); ch
= nc()); if (IOerror
)return false; if (ch
== '-')sign
= 1, ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())x
= x
* 10 + ch
- '0'; if (ch
== '.') { double tmp
= 1; ch
= nc(); for (; ch
>= '0' && ch
<= '9'; ch
= nc())tmp
/= 10.0, x
+= tmp
* (ch
- '0'); }if (sign
)x
= -x
; return true; }
inline bool read(char* s
) { char ch
= nc(); for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; !blank(ch
) && !IOerror
; ch
= nc())*s
++ = ch
; *s
= 0; return true; }
inline bool read(char& c
) { for (c
= nc(); blank(c
); c
= nc()); if (IOerror
) { c
= -1; return false; }return true; }
inline bool read(std
::string
& s
) { s
.clear(); char ch
= nc(); for (; blank(ch
); ch
= nc()); if (IOerror
)return false; for (; !blank(ch
) && !IOerror
; ch
= nc())s
+= ch
; return true; }
template<class T, class... U
>bool read(T
& h
, U
&... t
) { return read(h
) && read(t
...); }
struct Ostream_fwrite
{
#ifdef DEBUG
inline void out(char ch
) { putchar(ch
); }
#else
char* buf
, * p1
, * pend
;
Ostream_fwrite() { buf
= new char[BUF_SIZE
]; p1
= buf
; pend
= buf
+ BUF_SIZE
; }
inline void out(char ch
) { if (p1
== pend
) { fwrite(buf
, 1, BUF_SIZE
, stdout); p1
= buf
; }*p1
++ = ch
; }
#endif
template<class T>inline void print(T x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; if (x
< 0)out('-'), x
= -x
; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(char ch
) { out(ch
); }
inline void print(unsigned long long x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(unsigned int x
) { static char s
[41], * s1
; s1
= s
; if (!x
)*s1
++ = '0'; while (x
)*s1
++ = x
% 10 + '0', x
/= 10; while (s1
-- != s
)out(*s1
); }
inline void print(double x
, int y
= DECIMAL
) {
static LL mul
[] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000,
1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL };
if (x
< -1e-12)out('-'), x
= -x
; x
*= mul
[y
];
LL x1
= (LL
)floor(x
); if (x
- floor(x
) >= 0.5)++x1
;
LL x2
= x1
/ mul
[y
], x3
= x1
- x2
* mul
[y
]; print(x2
);
if (y
> 0) { out('.'); for (size_t i
= 1; i
< y
&& x3
* mul
[i
] < mul
[y
]; out('0'), ++i
); print(x3
); }
}
inline void print(char* s
) { while (*s
)out(*s
++); }
inline void print(const char* s
) { while (*s
)out(*s
++); }
inline void print(std
::string s
) { print(s
.c_str()); }
#ifndef DEBUG
inline void flush() { if (p1
!= buf
) { fwrite(buf
, 1, p1
- buf
, stdout); p1
= buf
; } }
~Ostream_fwrite() { flush(); }
#endif
}Ostream
;
template<class T>inline void print(T x
) { Ostream
.print(x
); }
template<class T>inline void println(T x
) { Ostream
.print(x
); Ostream
.out('\n'); }
inline void print(double x
, int y
= DECIMAL
) { Ostream
.print(x
, y
); }
inline void println(double x
, int y
= DECIMAL
) { Ostream
.print(x
, y
); Ostream
.out('\n');; }
template<class T, class... U
>void print(const T
& h
, const U
&... t
) { print(h
); Ostream
.out(' ');; print(t
...); }
template<class T, class... U
>void println(const T
& h
, const U
&... t
) { print(h
); Ostream
.out(' ');; println(t
...); }
#ifndef DEBUG
inline void flush() { Ostream
.flush(); }
#endif
#undef LL
#undef DECIMAL
#undef BUF_SIZE
};
using namespace fastIO
;
typedef long long LL
;
using namespace std
;
const int32_t MAXN
= 1e5 + 5;
int main()
{
#ifdef LOCAL
freopen("..\\DEBUG\\IO\\1.in", "r", stdin);
clock_t c1
= clock();
#endif
#ifdef LOCAL
std
::cerr
<< "Time:" << clock() - c1
<< "ms" << std
::endl
;
#endif
#ifndef DEBUG
fastIO
::flush();
#endif
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-26513.html