超级快读
实测 NKOJ 同样代码 TLE 40 -> AC 100
如果要在 Dev 里面调试的话,请在最开头加上 #define DEBUG 1。
不要忘记在交到 OJ 上之后删除 #define DEBUG 1!!!
支持负数,谨慎跟 stdio 混用。
超级快读提供了几个成员函数:
gc()。返回一个char。功能是读入一个char,并通过函数返回值返回。效果等同于getchar()。区别在于如果不在DEBUG模式,会使用更快的文件读写代替getchar()。blank(char ch)。返回一个bool值,如果ch为空格、回车、换行、制表符,返回true。否则返回false。read(T x)。读入x。x支持的类型有整数(支持__int128)、char、char数组。push(char ch)。输出一个char。效果等同于putchar()。区别在于如果不在DEBUG模式,会使用更快的文件读写代替putchar()。write(T x)。输出x。x支持的类型有整数(支持__int128)。write(T x,char ch)。输出x。x支持的类型有整数(支持__int128)。然后调用push()函数输出ch。
示例用法:
//...省略本体
io.read(n);//用于输入整数、char、char 数组
io.write(n);//用于输出整数
io.write(n,'\n');//输出整数 n 之后再输出一个换行符
io.push(' ');//用于输出一个 char本体:
放在头文件,namespace 之后。
//#define DEBUG 1
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x>='0'&&x<='9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG == 1
#else
IO():p1(buf),p2(buf),pp(pbuf){}
~IO(){
fwrite(pbuf,1,pp-pbuf,stdout);
}
#endif
bool fu;
char gc(){
#if DEBUG == 1
return getchar();
#endif
if(p1==p2){
p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
}
return p1==p2?' ':*p1++;
}
bool blank(char ch){
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template <class T>
void read(T &x){
x=0;
fu=0;
char ch=gc();
while(!isdigit(ch) && ch!='-'){
ch=gc();
}
if(ch=='-'){
fu=1;
while(!isdigit(ch)){
ch=gc();
}
}
while(isdigit(ch)){
x=(x<<3)+(x<<1)+(ch^48);
ch=gc();
}
if(fu){
x=(~x)+1;
}
}
void read(char *s){
char ch=gc();
while(blank(ch)){
ch=gc();
}
while(!blank(ch)){
*s++=ch;
ch=gc();
}
*s=0;
}
void read(char &ch){
ch=gc();
while(blank(ch)){
ch=gc();
}
}
void push(const char &c){
#if DEBUG == 1
putchar(c);
#else
if(pp-pbuf==MAXSIZE){
fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;
}
*pp++=c;
#endif
}
template<class T>
void write(T x){
static T stk[256];
T top=0;
if(x<0){
push('-');
x=(~x)+1;
}
do{
stk[top++]=x%10,x/=10;
}while(x);
while(top){
push(stk[--top]+'0');
}
}
template <class T>
void write(T x,char lastChar){
write(x);
push(lastChar);
}
}io;