读入优化

卡常必备,平时要多写。万一复杂度不对卡常卡过去了呢。
劣质版:

#include<cstdio>
char gcbuf[1<<28];
inline char fgc() {
	static int cur = 0, lim = 0;
	cur++;
	if(cur >= lim) {
		lim = fread(gcbuf, 1, 1<<28, stdin);
		cur = 0;
	}
	return gcbuf[cur];
}
inline int read() {
	int a = 0, c = fgc(), w = 1;
	for(; c < '0' || c > '9'; c = fgc()) if(c == '-') w = -1;
	for(; c >= '0' && c <= '9'; c = fgc()) a = a * 10 + c - '0';
	return a * w;
}

优质版:

#include<cstdio>
#include <sys/mman.h>
struct IObuf{
	char *buf;
	IObuf() : buf((char*)mmap(0, 1<<28, PROT_READ, MAP_PRIVATE, fileno(stdin), 0)) {}
	operator int(){
		int ret = 0, flag = 0;
		while (*buf < 48)	if (*buf++ == 45)	flag = 1;
		while (*buf > 32)	ret = ret*10+*buf++-48;
		return flag ? -ret : ret;
	}
} reader;
posted @ 2018-09-22 23:14  Utoрia  阅读(130)  评论(0编辑  收藏  举报