整形超级快读模板
namespace io
{
char *p1,*p2,buf[100001];
#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
template<typename T>
inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value,T>::__type read()
{
T sum=0;
char ch;
do ch=getchar(); while(!isdigit(ch));
do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
return sum;
}
template<typename T>
inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value,void>::__type read(T& sum)
{
sum=0;
char ch;
do ch=getchar(); while(!isdigit(ch));
do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
}
#undef getchar
}上面的那个板子对调用参数进行了判断,确保你不会读入一个 double 之类的玩意儿。在正式考试中,建议使用常规写法:
namespace io
{
char *p1,*p2,buf[100001];
#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
template<typename T>
inline T read()
{
T sum=0;
char ch;
do ch=getchar(); while(!isdigit(ch));
do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
return sum;
}
template<typename T>
inline void read(T& sum)
{
sum=0;
char ch;
do ch=getchar(); while(!isdigit(ch));
do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
}
#undef getchar
}用法
int n;
io::read(n);int n=read<int>();
long long m=read<long long>();超级快写模板
#include<cstdio>
#include<cctype>
#include<cstdlib>
using namespace std;
namespace io_out
{
char buf[100001];
char *p1=buf,*p2=buf+100000;
#define putchar(ch) ((p1==p2?(fwrite(buf,1,100000,stdout),p1=buf):0),*p1++=ch)
void quit(int ret_val=0){fwrite(buf,1,p1-buf,stdout);exit(ret_val);}
void print(char ch){putchar(ch);}
template<class T> void print(T x)
{
static char tmp[15];
int top=0;
while(x) tmp[++top]=(x%10)+'0',x/=10;
if(top==0) tmp[++top]=0;
for(int i=top;i>=1;i--) putchar(tmp[i]);
}
void print(const char* str)
{
while(*str!=0) putchar(*str++);
}
#undef putchar
}使用方式:
int main()
{
srand(time(0));
if(rand()%2==0)
print("NO"),quit(0);
print(123);
quit();//请在每个程序出口处都调用quit(); 而不是 return 0; 或者 exit(0);
}时间对比
结果(不开 O2)
cin | cin.tie(0) | ifstream | fastin | Sfastin | |
|---|---|---|---|---|---|
cout | 4.784s | / | 4.667s | 5.421s | 4.554s |
cout.tie(0) | / | 2.104s | 2.108s | 2.850s | 1.964s |
ofstream | 2.141s | 2.060s | 2.041s | 2.864s | 1.980s |
fastout | 1.970s | 2.023s | 1.982s | 2.609s | 1.751s |
Sfastout | 1.693s | 1.600s | 1.604s | 2.374s | 1.522s |
结果(开 O2)
cin | cin.tie(0) | ifstream | fastin | Sfastin | |
|---|---|---|---|---|---|
cout | 4.621s | / | 4.467s | 4.271s | 4.038s |
cout.tie(0) | / | 2.046s | 1.950s | 1.707s | 1.554s |
ofstream | 2.013s | 1.979s | 2.022s | 1.701s | 1.503s |
fastout | 1.567s | 1.437s | 1.484s | 1.229s | 1.011s |
Sfastout | 1.361s | 1.343s | 1.260s | 1.029s | 0.837s |
结果(旧版)
scanf/printf 8.903
fastin+printf 6.438
cin/cout 4.992
fastin+fastout 2.958
cin/cout+freopen+sync 1.798
fstream 1.788
Sfastin+printf 1.761
cin/cout+sync+freopen 1.742
Sfastin+Sfastout 0.5535