快读
一般的,当要读入的东西常数过大时,就会使用快读,且时间复杂度比\(cin\)低。
代码:
void read(int &x)
{
x = 0;
int f = 1;
char c = getchar();
while (!('0' <= c && c <= '9'))
{
if (c == '-')
{
f = -1;
}
c = getchar();
}
while ('0' <= c && c <= '9')
{
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
使用:
int n;
read(n);
快读的改进
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21];
char *p1=buf;
char *p2=buf;
void read(int &x)
{
x = 0;
int f = 1;
char c = getchar();
while (!('0' <= c && c <= '9'))
{
if (c == '-')
{
f = -1;
}
c = getchar();
}
while ('0' <= c && c <= '9')
{
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
当然,读字符串还是用\(getchar\)吧……

浙公网安备 33010602011771号