Define a macro called getcx so that it does not use locks when reading. This is not thread safe, but faster if you are not worried about thread safety:
#define getcx getchar_unlocked
Define inp as inline to make it faster:
inline void inp( int &n )//fast input function { n=0; int ch=getcx();int sign=1; while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}
Multiply n by 10 (using the shift to compute 8 * n + 2 * n, which could be faster):
while( ch >= '0' && ch <= '9' ) n = (n<<3)+(n<<1) + ch-'0', ch=getcx(); n=n*sign; }
You can use putchar_unlocked to have a faster output function if thread safety is not an issue.
source share