I got these 2 codes for fast input and output respectively, from codechef itself.
inline int readInt(){
int number = 0;
char c = getchar_unlocked();
while(c<'0')
c = getchar_unlocked();
while(c>='0' && c<='9'){
number = (number<<3)+(number<<1)+c-'0';
c = getchar_unlocked();
}
return number;
}
inline void writeInt (int n)
{
int N = n, rev, count = 0;
rev = N;
if (N == 0) { putchar_unlocked('0'); return ;}
while ((rev % 10) == 0) { count++; rev /= 10;}
rev = 0;
while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
while (rev != 0) { putchar_unlocked(rev % 10 + '0'); rev /= 10;}
while (count--) putchar_unlocked('0');
putchar_unlocked('\n');
}
Can someone please explain how do these work?? Further as far as I can understand it is a faster method for reading integers, can there be a more generalized method for all data types??
Thanks for any help in advance.