cin的优化--关闭同步

cin在关闭缓冲与输入绑定之后,速度与scanf不相上下。
注意关闭后只能使用cin与cout等输入输出流,无法与c的输入输出混用。
关闭方法如下:

1
2
ios::sync_with_stdio(false);//关闭c输入方式兼容
cin.tie(0);//解绑输入流

输入量实在超级巨大的时候,可以考虑使用快读

1
2
3
4
5
6
7
void read(int &x)
{
int f=1,x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}

另附快速输出模板

1
2
3
4
5
6
7
8
9
10
11
void write(int x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
write(x/10);
putchar(x%10+'0');
}

0%