Hello, CodeForces!
The following is a fast input/output template suitable for C++11 or higher versions. It not only supports reading and writing of all integer types, but also supports strings and floating point types. It can also set output precision, flush write buffers, format output, and other functions.
NOTE: keyboard input will be ignored if the acceleration switch is enabled.
Original version
/* d0j1a_1701 FastIO full ver. 4.2 */
//#define FIO // Cache acceleration switch, DO NOT enable in interactive problems.
struct IO {
#ifdef FIO
const static int BUFSIZE = 1 << 20;
char buf[BUFSIZE], obuf[BUFSIZE], *p1, *p2, *pp;
inline char getchar() {
return (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUFSIZE, stdin), p1 == p2) ? EOF : *p1++);
}
inline void putchar(char x) {
((pp - obuf == BUFSIZE && (fwrite(obuf, 1, BUFSIZE, stdout), pp = obuf)), *pp = x, pp++);
}
inline IO &flush() {
fwrite(obuf, 1, pp - obuf, stdout);
fflush(stdout);
return *this;
}
IO() {
p1 = buf, p2 = buf, pp = obuf;
}
~IO() {
flush();
}
#else
int (*getchar)() = &::getchar;
int (*putchar)(int) = &::putchar;
inline IO &flush() {
fflush(stdout);
return *this;
};
#endif
string sep = " ";
int k = 2;
template < typename Tp, typename std::enable_if < std::is_integral<Tp>::value || std::is_same<Tp, __int128_t>::value >::type * = nullptr >
inline int read(Tp &s) {
int f = 1;
char ch = getchar();
s = 0;
while (!isdigit(ch) && ch != EOF) f = (ch == '-' ? -1 : 1), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
s *= f;
return ch != EOF;
}
template<typename Tp, typename enable_if<is_floating_point<Tp>::value>::type * = nullptr>
inline int read(Tp &s) {
int f = 1;
char ch = getchar();
s = 0;
while (!isdigit(ch) && ch != EOF && ch != '.') f = (ch == '-' ? -1 : 1), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
if(ch == EOF) return false;
if(ch == '.') {
Tp eps = 0.1;
ch = getchar();
while (isdigit(ch)) s = s + (ch ^ 48) * eps, ch = getchar(), eps /= 10;
}
s *= f;
return ch != EOF;
}
inline int read(char &c) {
char ch = getchar();
c = EOF;
while(isspace(ch) && ch != EOF) ch = getchar();
if(ch != EOF) c = ch;
return c != EOF;
}
inline int read(char *c) {
char ch = getchar(), *s = c;
while(isspace(ch) && ch != EOF) ch = getchar();
while(!isspace(ch) && ch != EOF) *(c++) = ch, ch = getchar();
*c = '\0';
return s != c;
}
inline int read(string &s) {
s.clear();
char ch = getchar();
while(isspace(ch) && ch != EOF) ch = getchar();
while(!isspace(ch) && ch != EOF) s += ch, ch = getchar();
return s.size() > 0;
}
inline int getline(char *c, const char &ed = '\n') {
char ch = getchar(), *s = c;
while(ch != ed && ch != EOF) *(c++) = ch, ch = getchar();
*c = '\0';
return s != c;
}
inline int getline(string &s, const char &ed = '\n') {
s.clear();
char ch = getchar();
while(ch != ed && ch != EOF) s += ch, ch = getchar();
return s.size() > 0;
}
template<typename Tp = int>
inline Tp read() {
Tp x;
read(x);
return x;
}
template<typename Tp, typename... Ts>
int read(Tp &x, Ts &...val) {
return read(x) && read(val...);
}
template<typename Tp, typename enable_if<is_integral<Tp>::value>::type * = nullptr>
IO & write(Tp x) {
if (x < 0) putchar('-'), x = -x;
static char sta[20];
int top = 0;
do sta[top++] = x % 10 + '0', x /= 10;
while (x);
while (top)
putchar(sta[--top]);
return *this;
}
inline IO &write(const string &str) {
for(char ch : str) putchar(ch);
return *this;
}
inline IO &write(const char *str) {
while(*str != '\0') putchar(*(str++));
return *this;
}
inline IO &write(char *str) {
return write((const char *)str);
}
inline IO &write(const char &ch) {
return putchar(ch), *this;
}
template<typename Tp, typename enable_if<is_floating_point<Tp>::value>::type * = nullptr>
inline IO & write(Tp x) {
if(x > 1e18 || x < -1e18) {
write("[Floating point overflow]");
throw;
}
if(x < 0) putchar('-'), x = -x;
const static long long pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 100000000000000000, 100000000000000000};
const auto &n = pow10[k];
long long whole = (int)x;
double tmp = (x - whole) * n;
long long frac = tmp;
double diff = tmp - frac;
if (diff > 0.5) {
++frac;
if (frac >= n) frac = 0, ++whole;
} else if (diff == 0.5 && ((frac == 0U) || (frac & 1U))) ++frac;
write(whole);
if (k == 0U) {
diff = x - (double)whole;
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1))
++whole;
} else {
putchar('.');
static char sta[20];
int count = k, top = 0;
while (frac) {
sta[top++] = frac % 10 + '0';
frac /= 10, count--;
}
while (count--) putchar('0');
while (top) putchar(sta[--top]);
}
return *this;
}
template<typename Tp, typename... Ts>
inline IO &write(Tp x, Ts... val) {
write(x);
write(sep);
write(val...);
return *this;
}
template<typename... Ts>
inline IO &writeln(Ts... val) {
write(val...);
putchar('\n');
return *this;
}
inline IO &writeln(void) {
putchar('\n');
return *this;
}
template<typename Tp>
inline IO &writeWith(Tp x, const string &s = " ") {
write(x), write(s);
return *this;
}
inline IO &setsep(const string &s) {
return sep = s, *this;
}
inline IO &setprec(const int &K) {
return k = K, *this;
}
} io;
Compressed version, suitable for Dev-C++ enthusiasts.
/* d0j1a_1701 FastIO full ver. 4.2 */
//#define FIO // Cache acceleration switch, DO NOT enable in interactive problems.
struct IO{
#ifdef FIO
const static int BUFSIZE=1<<20;char buf[BUFSIZE],obuf[BUFSIZE],*p1,*p2,*pp;inline char getchar(){return(p1==p2&&(p2=(p1=buf)+fread(buf,1,BUFSIZE,stdin),p1==p2)?EOF:*p1++);}inline void putchar(char x){((pp-obuf==BUFSIZE&&(fwrite(obuf,1,BUFSIZE,stdout),pp=obuf)),*pp=x,pp++);}inline IO&flush(){fwrite(obuf,1,pp-obuf,stdout);fflush(stdout);return*this;}IO(){p1=buf,p2=buf,pp=obuf;}~IO(){flush();}
#else
int(*getchar)()=&::getchar;int(*putchar)(int)=&::putchar;inline IO&flush(){fflush(stdout);return*this;};
#endif
string sep=" ";int k=2;template<typename Tp,typename std::enable_if<std::is_integral<Tp>::value||std::is_same<Tp,__int128_t>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF)f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();s*=f;return ch!=EOF;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF&&ch!='.')f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();if(ch==EOF)return false;if(ch=='.'){Tp eps=0.1;ch=getchar();while(isdigit(ch))s=s+(ch^48)*eps,ch=getchar(),eps/=10;}s*=f;return ch!=EOF;}inline int read(char&c){char ch=getchar();c=EOF;while(isspace(ch)&&ch!=EOF)ch=getchar();if(ch!=EOF)c=ch;return c!=EOF;}inline int read(char*c){char ch=getchar(),*s=c;while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)*(c++)=ch,ch=getchar();*c='\0';return s!=c;}inline int read(string&s){s.clear();char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)s+=ch,ch=getchar();return s.size()>0;}inline int getline(char*c,const char&ed='\n'){char ch=getchar(),*s=c;while(ch!=ed&&ch!=EOF)*(c++)=ch,ch=getchar();*c='\0';return s!=c;}inline int getline(string&s,const char&ed='\n'){s.clear();char ch=getchar();while(ch!=ed&&ch!=EOF)s+=ch,ch=getchar();return s.size()>0;}template<typename Tp=int>inline Tp read(){Tp x;read(x);return x;}template<typename Tp,typename...Ts>int read(Tp&x,Ts&...val){return read(x)&&read(val...);}template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>IO&write(Tp x){if(x<0)putchar('-'),x=-x;static char sta[20];int top=0;do sta[top++]=x%10+'0',x/=10;while(x);while(top)putchar(sta[--top]);return*this;}inline IO&write(const string&str){for(char ch:str)putchar(ch);return*this;}inline IO&write(const char*str){while(*str!='\0')putchar(*(str++));return*this;}inline IO&write(char*str){return write((const char*)str);}inline IO&write(const char&ch){return putchar(ch),*this;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline IO&write(Tp x){if(x>1e18||x<-1e18){write("[Floating point overflow]");throw;}if(x<0)putchar('-'),x=-x;const static long long pow10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,100000000000000000,100000000000000000};const auto&n=pow10[k];long long whole=(int)x;double tmp=(x-whole)*n;long long frac=tmp;double diff=tmp-frac;if(diff>0.5){++frac;if(frac>=n)frac=0,++whole;}else if(diff==0.5&&((frac==0U)||(frac&1U)))++frac;write(whole);if(k==0U){diff=x-(double)whole;if((!(diff<0.5)||(diff>0.5))&&(whole&1))++whole;}else{putchar('.');static char sta[20];int count=k,top=0;while(frac){sta[top++]=frac%10+'0';frac/=10,count--;}while(count--)putchar('0');while(top)putchar(sta[--top]);}return*this;}template<typename Tp,typename...Ts>inline IO&write(Tp x,Ts...val){write(x);write(sep);write(val...);return*this;}template<typename...Ts>inline IO&writeln(Ts...val){write(val...);putchar('\n');return*this;}inline IO&writeln(void){putchar('\n');return*this;}template<typename Tp>inline IO&writeWith(Tp x,const string&s=" "){write(x),write(s);return*this;}inline IO&setsep(const string&s){return sep=s,*this;}inline IO&setprec(const int&K){return k=K,*this;}}io;
Usage:
/* Input */
int x = io.read(); // Return value method
io.read(x); // Parameter passing method
long long i64 = io.read<long long>(); // You can specify the data type for return value method using templates, default is int
io.read(i64); // If using parameter passing method, the type will be automatically inferred
double db;
string str;
io.read(x, i64, db, str); // Parameter passing method can read multiple data of any type at once
// The return value in parameter passing method is used to check for EOF. A common use case is for problems with multiple test cases.
// If reading is successful, it returns 1. If EOF is reached, it returns 0.
// If multiple variables are read at once, if any variable reaches EOF, it immediately terminates and returns 0. If all variables are successfully read, it returns 1.
while (io.read(x))
io.writeln(x);
/* Output */
io.write(x); // cout << x;
io.writeln(x); // cout << x << endl;
// cout << x << ' ' << i64 << ' ' << str << endl;
io.writeln(x, i64, str);
io.writeWith(x); // cout << x << " ";
// The second argument can be a string (only a string! Don't use ',' or similar things! Use ","!)
io.writeWith(x, "qwq!"); // cout << x << "qwq!";
// setprec(int) is used to set the floating-point output precision, default is to keep two decimal places
db = 3.1415926;
io.writeln(db); // 3.14
io.setprec(5).writeln(db); // 3.14159
io.setprec(0).writeln(db); // 3
// setsep(string) is used to set the output separator, default is a space
// Similarly, the argument can only be a string!
x = 1, i64 = 2;
io.writeln(x, i64); // 1 2
io.setsep(","); // 1,2
// A common use case is to implement formatted string output
int T = io.read(), a, b;
for (int t = 1; t <= T; t++) {
io.read(a, b);
io.setsep("").writeln("Test case #", t, ": ", a + b);
}
/*
Input:
2
1 2
5 5
Output:
Test case #1: 3
Test case #2: 10
*/
// Custom IO functions
// putchar -> io.putchar
// getchar -> io.getchar
// You should place this outside the struct IO{}
void writeString(string str) {
for (char ch : str) io.putchar(ch);
}
writeString("zxl is very cute");
// Flush output buffer and IO interaction problem
/*
By default, when the cache mode is enabled, the output is saved in the cache. The cache will be automatically flushed to stdout when it is full or when the program exits (reading works similarly, fread reads a large chunk from stdin to the cache, parses it, then reads the second chunk, so you cannot use keyboard input when the cache mode is enabled, and you cannot use the cache mode in interactive problems).
However, you can manually flush the output buffer using io.flush(). If the cache mode is enabled, this will flush all the contents in the output buffer to stdout. Otherwise, it will flush the built-in cache of stdout.
Therefore, in IO interaction problems, disable the cache mode and call io.flush() after each output, just like using cout/printf.
*/
io.writeln("Hello World!").flush();
// All functions support chained calls
io.setprec(3).writeln(114.514).setsep("").writeln("My name is Tadokoro Koji, my telephone number is ", 1919810).setsep(" ").writeln("I am a 24-year-old student.");
Benchmark
Read 5 million long long integers, repeat the test five times.
Without-acceleration: 288ms(-O2) 479ms
With-acceleration: 175ms(-O2) 429ms




