快读快写 模板
Aaron_Romeo · · 算法·理论
C
namespace IO
{
#ifdef LOCAL
const int N = 1;
#else
const int N = 1 << 23;
#endif
template<typename T> struct flt {T num; int pcs; flt() {num = 0, pcs = 6;} flt(const T &_num, const int &_pcs) {num = _num, pcs = _pcs;}};
struct I {FILE *in; char ibuf[N], *p1, *p2; I() {*this = stdin;} I(FILE *f) {*this = f;} I & operator = (FILE *f) {return p1 = p2 = ibuf, in = f, *this;} bool flush() {return p1 != p2 || (p2 = (p1 = ibuf) + fread(ibuf, 1, N, in), p1 != p2);} char gc() {return !flush() ? EOF : *p1 ++ ;} int getline(std :: string &s) {s.clear(); char c; while (c = gc(), c != 10 && c != EOF) if (c != 13) s += c; return s.length();} int getline(char *s) {char *t = s; char c; while (c = gc(), c != 10 && c != EOF) if (c != 13) *t ++ = c; *t = 0; return t - s;} int getdigit() {char c; while (!isdigit(c = gc()) && c != EOF); return c == EOF ? 0 : c - '0';} template<typename T> T readnum() {bool f = 0; T x = 0; char c; while (!isdigit(c = gc()) && c != EOF) f = c == '-'; while (isdigit(c)) x = x * 10 + c - '0', c = gc(); if (c == '.') for (T v = 0.1; isdigit(c = gc()); v *= 0.1) x += v * (c - '0'); return f ? -x : x;} template<typename T, typename = std :: enable_if_t<std :: is_arithmetic<T> :: value || std :: is_same<T, __int128> :: value || std :: is_same<T, unsigned __int128> :: value || std :: is_same<T, bool> :: value>> operator T() {return readnum<T>();} operator char() {char c = gc(); return c == EOF ? 0 : c;} operator std :: string() {std :: string s; char c; while (c = gc(), c == ' ' || c == 10 || c == 13 || c == 9); while (c != ' ' && c != 10 && c != 9 && c != EOF) {if (c != 13) s += c; c = gc();} return s;} I & operator () () {return *this;} template<typename T> int operator () (T &x) {return x = operator T(), 0;} int operator () (char *s) {char *t = s, c; while (c = gc(), c == ' ' || c == 10 || c == 13 || c == 9); while (c != ' ' && c != 10 && c != 9 && c != EOF) {if (c != 13) *t ++ = c; c = gc();} *t = 0; return t - s;} template<typename T, typename ...Args> typename std :: enable_if<std :: is_pointer<typename std :: remove_reference<T> :: type> :: value || std::is_array<T> :: value, int> :: type operator () (T x, Args &&...args) {return operator () (x) + operator () (std :: forward<Args> (args)...);} template<typename T, typename ...Args> typename std :: enable_if<!std :: is_pointer<typename std :: remove_reference<T> :: type> :: value && !std::is_array<T> :: value, int> :: type operator () (T &x, Args &&...args) {return operator () (x) + operator () (std :: forward<Args> (args)...);}};
struct O {FILE *out; char obuf[N], *p3; O() {p3 = obuf, out = stdout;} O(FILE *f) {p3 = obuf, out = f;} ~O() {flush();} O & operator = (FILE *f) {return flush(), out = f, *this;} void flush() {fwrite(obuf, p3 - obuf, 1, out), p3 = obuf;} int close() {return flush(), fclose(out);} void pc(char x) {*p3 ++ = x; if (p3 - obuf == N) flush();} template<typename ...Args> int printf(const char *format, Args ...args) {char s[1 << 23], *t = s; sprintf(s, format, args...); while (*t) pc(*t ++ ); return t - s;} template<typename T> void writeint(const T &x) {T y = x; if (!y) return pc('0'); if (y < 0) pc('-'); char c[1 << 6]; int len = 0; if (y < 0) while (y) c[ ++ len] = -(y % 10) + '0', y /= 10; else while (y) c[ ++ len] = y % 10 + '0', y /= 10; while (len) pc(c[len -- ]);} template<typename T> void operator () (const T &x) {writeint<T>(x);} void operator () (const char &x) {pc(x);} void operator () (const std :: string &s) {for (char i : s) pc(i);} void operator () (char *s) {while (*s) pc(*s ++ );} void operator () (const char *s) {while (*s) pc(*s ++ );} template<typename T> void writefloat(const flt<T> &x) {T y = x.num; int r = x.pcs; if (y < 0) pc('-'), y = -y; T f = 5; for (int i = 1; i <= r + 1; i ++ ) f *= 0.1; y += f; char c[1 << 8]; int len = 0; if (y < 1) pc('0'); for (T s = std :: floor(y), t; s >= 1; s = t) t = std :: floor(s * 0.1), c[ ++ len] = std :: max((int)(s - t * 10), 0) + '0'; while (len) pc(c[len -- ]); if (!r) return ; pc('.'); for (T s = (y - std :: floor(y)) * 10; r; s = (s - std :: floor(s)) * 10, r -- ) pc((int)s + '0');} template<typename T> void operator () (const flt<T> &x) {writefloat<T>(x);} void operator () (const float &x) {writefloat(flt<float>(x, 6));} void operator () (const double &x) {writefloat(flt<double>(x, 6));} void operator () (const long double &x) {writefloat(flt<long double>(x, 6));} template<typename T, typename ...Args> void operator () (const T &x, const Args &...args) {operator () (x), operator () (args...);}};
} IO :: I read; IO :: O write, debug = stderr; using IO :: flt;
C++
%%%@recollect_i
struct fast_IO{
/***read***/
char buf[1000000], *s = buf, *t = buf;
inline char gc()
{
#ifdef LOCAL
return getchar();
#endif
return s == t && (t = (s = buf) + fread(buf, 1, 1000000, stdin), s == t) ? EOF : *s ++ ;
}
// read a character
int chtype = 0;
inline void chnormal() {chtype = 0;}
inline void chall() {chtype = 1;}
inline void chdigit() {chtype = 2;}
inline void chletter() {chtype = 3;}
inline char chread()
{
char ch = gc();
while(ch != EOF && ch <= 32) ch = gc();
return ch;
}
inline char digitread()
{
char ch = gc();
while(ch != EOF && (ch < '0' || ch > '9')) ch = gc();
return ch;
}
inline char letterread()
{
char ch = gc();
while(ch != EOF && !('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z')) ch = gc();
return ch;
}
// read a string
int strtype = 0;
inline void strnormal() {strtype = 0;}
inline void strline() {strtype = 1;}
inline void strread(char *s)
{
char ch = gc();
while(ch <= 32) ch = gc();
while(ch > 32) *s ++ = ch, ch = gc();
*s = 0;
}
inline void lineread(char *s)
{
char ch = gc();
while(ch < 32) ch = gc();
while(ch >= 32) *s ++ = ch, ch = gc();
*s = 0;
}
// read an integer
int inttype = 0;
inline void intnormal() {inttype = 0;}
inline void intunsigned() {inttype = 1;}
int lltype = 0;
inline void llnormal() {lltype = 0;}
inline void llunsigned() {lltype = 1;}
template <typename T>
inline void uread(T &x)
{
char ch = gc();
x = 0;
while(ch < '0' || ch > '9') ch = gc();
while('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
}
template <typename T>
inline void read(T &x)
{
char ch = gc();
x = 0;
bool f = 0;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = 1;
ch = gc();
}
if(f) while('0' <= ch && ch <= '9') x = x * 10 - (ch ^ 48), ch = gc();
else while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
}
// read a real number
template <typename T>
inline void dread(T &x)
{
char ch = gc();
x = 0;
int f = 1;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = gc();
}
while('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
if(ch == '.')
{
ch = gc();
double p = 0.1;
while('0' <= ch && ch <= '9') x += p * (ch ^ 48), ch = gc(), p *= 0.1;
}
x *= f;
}
inline fast_IO & operator >> (char &x)
{
switch(chtype)
{
case 0 : x = chread(); break ;
case 1 : x = gc(); break ;
case 2 : x = digitread(); break ;
case 4 : x = letterread(); break ;
default : break ;
}
return *this;
}
inline fast_IO & operator >> (char *x)
{
strtype ? lineread(x) : strread(x);
return *this;
}
inline fast_IO & operator >> (string &x)
{
static char buf[1000005];
strtype ? lineread(buf) : strread(buf);
return x = buf, *this;
}
inline fast_IO & operator >> (int &x)
{
inttype ? uread(x) : read(x);
return *this;
}
inline fast_IO & operator >> (unsigned &x)
{
return uread(x), *this;
}
inline fast_IO & operator >> (long long &x)
{
lltype ? uread(x) : read(x);
return *this;
}
inline fast_IO & operator >> (unsigned long long &x)
{
return read(x), *this;
}
inline fast_IO & operator >> (__int128 &x)
{
lltype ? read(x) : read(x);
return *this;
}
inline fast_IO & operator >> (unsigned __int128 &x)
{
return read(x), *this;
}
inline fast_IO & operator >> (float &x)
{
return dread(x), *this;
}
inline fast_IO & operator >> (double &x)
{
return dread(x), *this;
}
inline fast_IO & operator >> (long double &x)
{
return dread(x), *this;
}
inline fast_IO & operator >> (__float128 &x)
{
return dread(x), *this;
}
/***write***/
char obuf[1000000], *p = obuf;
const char endl = '\n';
inline void pc(char x)
{
p - obuf < 1000000 ? (*p ++ = x) : (fwrite(obuf, p - obuf, 1, stdout), p = obuf, *p ++ = x);
}
inline ~fast_IO() {fwrite(obuf, p - obuf, 1, stdout);}
template <typename T>
inline void write(T x)
{
if(!x) return pc(48);
int len = 0;
static char c[40];
if(x < 0) pc('-'), x = -x;
while(x) c[ ++ len] = (x % 10) ^ 48, x /= 10;
while(len) pc(c[len -- ]);
}
inline fast_IO & operator << (int x)
{
return write(x), *this;
}
inline fast_IO & operator << (unsigned x)
{
return write(x), *this;
}
inline fast_IO & operator << (long long x)
{
return write(x), *this;
}
inline fast_IO & operator << (unsigned long long x)
{
return write(x), *this;
}
inline fast_IO & operator << (__int128 x)
{
return write(x), *this;
}
inline fast_IO & operator << (unsigned __int128 x)
{
return write(x), *this;
}
inline fast_IO & operator << (char x)
{
return pc(x), *this;
}
inline fast_IO & operator << (char *x)
{
while(*x) pc(*x ++ );
return *this;
}
inline fast_IO & operator << (string x)
{
for(char i : x) pc(i);
return *this;
}
}IO;