跳到正文

算法·理论

FastIO

lateworker约 8 分钟
原文

这是 2025年9月5日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年8月9日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

FastIO

该快读模板会有约 0.32MiB0.32\text{MiB} 的内存占用。

C++
#define gc() (eof||(p==SIZE&&(buf[fread(buf,1,SIZE,stdin)]=EOF,(p=0)))||~buf[p]?buf[p++]:(eof=true,EOF))
#define ug(x) (eof=false,buf[--p]=(x))
#define pc(x) (buf[p++]=(x),p==SIZE?fl():p)
#define fl() (fwrite(buf,1,p,stdout),p=0)
namespace FastIn { constexpr size_t SIZE = 1 << 16; size_t p = SIZE; char buf[SIZE | 1], c; bool f, eof; inline void rd(char& x) { while(isspace(x=gc())); } inline void rd(char* s) { while(isspace(*s=gc()));if(eof){return;}for(;!isspace(*s)&&!eof;*++s=gc());ug(*s),*s='\0'; } template<typename T> inline std::enable_if_t<std::is_integral<T>::value || std::is_same_v<T, __int128>, void> rd(T& x) { x=f=0;while(isspace(c=gc()));if(eof){return;}if(c=='-'){f=true,c=gc();}for(;isdigit(c);c=gc()){x=(x<<1)+(x<<3)+(c^'0');}ug(c);if(f){x=-x;} } }
namespace FastOut { constexpr size_t SIZE = 1 << 18; char buf[SIZE], c[40]; size_t f, p; struct __{~__(){fl();}}__; inline void wt(char x) { pc(x); } inline void wt(const char* s) { while(*s)pc(*s++); } template<typename T> inline std::enable_if_t<std::is_integral<T>::value || std::is_same_v<T, __int128>, void> wt(T x) { if(!x){pc('0');return;}f=0;if(x<0)x=-x,pc('-');while(x){c[f++]=x%10^'0',x/=10;}while(f--){pc(c[f]);} } }
template<typename... Args> inline void rd(Args&&... args) { using namespace FastIn; (rd(args), ...); }
template<typename... Args> inline void wtsp(Args&&... args) { using namespace FastOut; ((wt(args), wt(' ')), ...); if constexpr (!sizeof...(args)) wt(' '); }
template<typename... Args> inline void wtln(Args&&... args) { using namespace FastOut; ((wt(args), wt('\n')), ...); if constexpr (!sizeof...(args)) wt('\n'); }

更快的 FastIO(有点问题)

C++
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <cctype>
#include <cstddef>
#include <cstdlib>
#undef stdin
#undef stdout
#define stdin FastIO::in_fd
#define stdout FastIO::out_fd
#define IN_BUFFER_SIZE (1 << 16)
#define OUT_BUFFER_SIZE (1 << 18)
namespace FastIO {
	int in_fd = STDIN_FILENO, out_fd = STDOUT_FILENO;
	void freopen(const char* filename, const char* mode, int& file) {
		if (mode[0] == 'r') file = open(filename, O_RDONLY);
		if (mode[0] == 'w') file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
		if (file == -1) perror("Failed to open file"), exit(1);
	}
	size_t in_idx, in_end, out_idx;
	char in_buffer[IN_BUFFER_SIZE], out_buffer[OUT_BUFFER_SIZE];
	inline char getchar() {
		if (in_idx >= in_end) {
			in_idx = 0, in_end = read(in_fd, in_buffer, IN_BUFFER_SIZE);
			if (in_end <= 0) return EOF;
		} return in_buffer[in_idx++];
	}
	inline void putchar(const char &c) {
		out_buffer[out_idx++] = c;
		if (out_idx >= OUT_BUFFER_SIZE) write(out_fd, out_buffer, out_idx), out_idx = 0;
	}
	inline void flush() { if (out_idx > 0) write(out_fd, out_buffer, out_idx), out_idx = 0; }
	struct CIO { ~CIO() { flush(); } } cio;
	template<typename T_>
	CIO& operator>> (CIO& cio, T_& x) {
		char c; bool f;
		for (c = getchar(), f = false; isspace(c); c = getchar());
		if (c == '-') f = true, c = getchar();
		for (x = 0; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ '0');
		if (f) { x = -x; } if (!isspace(c)) { --in_idx; } return cio;
	}
	CIO& operator>> (CIO& cio, char& c) { for (c = getchar(); isspace(c); c = getchar()); return cio; }
	CIO& operator>> (CIO& cio, char* str) {
		char c; size_t len;
		for (c = getchar(); isspace(c); c = getchar());
		for (len = 0; !isspace(c); c = getchar()) str[len++] = c;
		str[len] = '\0'; return cio;
	}
	template<typename T_>
	CIO& operator<< (CIO& cio, T_ x) {
		if (!x) { putchar('0'); return cio; }
		if (x < 0) putchar('-'), x = -x;
		static char stk[21]; size_t top;
		for (top = 0; x; x /= 10) stk[top++] = x % 10;
		for (size_t i = top - 1; ~i; --i) putchar(stk[i] ^ '0');
		return cio;
	}
	CIO& operator<< (CIO& cio, char c) { putchar(c); return cio; }
	CIO& operator<< (CIO& cio, char* str) { for (size_t i = 0; str[i]; ++i) putchar(str[i]); return cio; }
	CIO& operator<< (CIO& cio, const char* str) { for (size_t i = 0; str[i]; ++i) putchar(str[i]); return cio; }
} using FastIO::cio; using FastIO::freopen;