我的板子

XiaoLe_MC發表於2024-08-16

讀寫最佳化

最佳化一
#ifdef _WIN32
	#define getchar _getchar_nolock
	#define putchar _putchar_nolock
#else
	#define getchar getchar_unlocked
	#define putchar putchar_unlocked
#endif
template <typename T> inline void rd(T &x){
	x = 0; int f = 1; char ch = getchar();
	while(!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)) x = (x<<1) + (x<<3) + (ch^48), ch = getchar();
	x *= f;
}
template <typename T> inline void wt(T x){
	if(x < 0) putchar('-'), x = -x;
	if(x / 10 > 0) wt(x / 10); putchar(x % 10 + '0');
}
最佳化二
#define B 1 << 23
char buf[B], *p1 = buf, *p2 = buf;
#define g() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, B, stdin), p1 == p2) ? EOF : *p1++)
template <typename T> inline void rd(T &x) {
    x = 0; int f = 0; char c = g();
    for (; c < '0' || c > '9'; c = g())
        f ^= c == '-';
    for (; c >= '0' && c <= '9'; c = g())
        x = (x << 3) + (x << 1) + (c ^ '0');
    x = f ? -x : x;
}
char obuf[B], *O = obuf;
#define p(c) (O - obuf == B && (fwrite(obuf, 1, B, stdout), O = obuf), *O++ = (c))
template <typename T> void wt(T x) {
    if (x < 0) x = -x, p('-');
    if (x >= 10) wt(x / 10);
    p(x % 10 ^ '0');
}

注意在程式碼末尾加上

fwrite(obuf, 1, O - obuf, stdout)

相關文章