You can significantly reduce the execution time by entering with fread()
or fread_unlocked()
(if your program is single-threaded). Locking / unlocking the input stream takes only a short time once, so ignore it.
Here is the code:
#include <iostream> int maxio=1000000; char buf[maxio], *s = buf + maxio; inline char getc1(void) { if(s >= buf + maxio) { fread_unlocked(buf,sizeof(char),maxio,stdin); s = buf; } return *(s++); } inline int input() { char t = getc1(); int n=1,res=0; while(t!='-' && !isdigit(t)) t=getc1(); if(t=='-') { n=-1; t=getc1(); } while(isdigit(t)) { res = 10*res + (t&15); t=getc1(); } return res*n; }
This is implemented in C++
. In C
you do not need to enable iostream
, the isdigit()
function is implicitly available.
You can accept input as a stream of characters by calling getc1()
and accept integer input by calling input()
.
The whole idea of using fread()
is to immediately accept large input blocks. Calling scanf()/printf()
repeatedly takes up valuable time when locking and unlocking threads that are fully reserved in a single-threaded program.
Also, make sure that the maxio
value is such that all input can only be done in a few “circular transitions” (ideally, in this case). Adjust it if necessary. This method is very effective in programming competitions to gain an edge over your opponent in terms of lead time.
Hope this helps!
source share