读入/输出优化模板

读入/输出优化模板


今天调一道题调了好久,最后发现输出优化挂了。。

于是在这里写一下读入/输出优化模板。


读入优化:

#define nc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int rd() {
	int x=0; char s=nc();
	while(s<'0'||s>'9') s=nc();
	while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+s-'0',s=nc();
	return x;
}
char rc() {
	char s=nc();
	while(s<'a'||s>'z') s=nc();
	return s;
}

 输出优化:

char pbuf[100000],*pp=pbuf;
void push(const char c) {
	if(pp-pbuf==100000) fwrite(pbuf,1,100000,stdout),pp=pbuf;
	*pp++=c;
}
void write(int x) {
	static int sta[35];
	int top=0;
	do{sta[top++]=x%10,x/=10;}while(x);
	while(top) push(sta[--top]+'0');
}

 输出优化在文件末写上

fwrite(pbuf,1,pp-pbuf,stdout);
原文地址:https://www.cnblogs.com/suika/p/9211986.html