快读

firstlight Lv2

getchar_unlocked()快读

getchar_unlocked() 的速度比 fread() 略逊一筹,但是有代码短小精悍的优点,可以在比赛中直接使用

Code:

1
2
3
4
5
6
7
8
9
10
namespace getchar_unlocked_fastread {
const void read(int &x)
{
x = 0; int f = 1; char c = getchar_unlocked();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar_unlocked();}
while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar_unlocked();
//(x << 3) + (x << 1) 相当于x * 10
x *= f;
}
}using getchar_unlocked_fastread::read;

fread()快读

fread()快读是目前最快的快读,但是有读入的缓存区,占内存

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace fread_fastread {
const int S = 1 << 20;
char buf[S], *H, *T;
char getchar()
{
if(H == T) T = (H = buf) + fread(buf, 1, S, stdin);
return (H == T) ? EOF : *H ++ ;
}

const void read(int &x)
{
x = 0; int f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= f;
}
} using fread_fastread::read;
  • 标题: 快读
  • 作者: firstlight
  • 创建于 : 2023-12-27 18:23:00
  • 更新于 : 2025-01-22 22:41:04
  • 链接: https://blog.firstlightport.top/posts/kuai-du/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论