[C++] Fast IO

Fast IO

해당 코드는 thread unsafe하기 때문에 주의해야합니다. 정확히는 unlocked 함수가 thread unsafe하기 때문입니다. thread safe한 기능을 원하신다면 단순 putchar getchar를 사용하시면 됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#define pc(x) putchar_unlocked(x)
#define gc() getchar_unlocked()
#define FIR(x) fastreadINT_byReference(x)
#define FIV() fastreadINT_byValue()

inline void fastreadINT_byReference(int &ret){
int N = gc(), flag = 1;
for(;N<'0'||N>'9';N=gc())
if(N=='-'){
flag=-1; N=gc(); break;
}
ret = 0;
do{
ret = (ret<<3) + (ret<<1) + (N & 0b1111); N=gc();
}while('0'<=N&&N<='9');
ret *= flag;
}

int fastreadINT_byValue(){
int N = gc(), ret = 0, flag = 1;
for(;N<'0'||N>'9';N=gc())
if(N=='-'){
flag=-1; N=gc(); break;
}
do{
ret = (ret<<3) + (ret<<1) + (N & 0b1111); N=gc();
}while('0'<=N&&N<='9');
return ret*flag;
}

inline void fastwriteInt (int n){
if (n == 0) { pc('0'); pc('\n'); return ;}
if (n<0) { n = -n; pc('-');}
int N = n, rev, count = 0;
rev = N;
while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
rev = 0;
while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;} //store reverse of N in rev
while (rev != 0) { pc(rev % 10 | 0b110000); rev /= 10;}
while (count--) pc('0');
pc('\n');
}
int main(int argc, char** argv){
int k;
FIR(k);
writeInt(k);
k=FIV();
writeInt(k);
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}

Digression

다이아를 달성했습니다.

BOJ

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/21/Languages/Cplusplus/fast-io/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.