While working on a problem on UVa Online Judge, I received “Accepted” for my program which started like so:
int main() {
int i, j;
while(cin >> i >> j) {
However, when I replaced that with this:
void read(int &n) {
n = 0;
int sign = 1;
char c = getchar_unlocked();
while(c<'0' || c>'9') {
if(c == '-') sign = -1;
c = getchar_unlocked();
}
while(c>='0' && c<='9') {
n = n*10+c-'0';
c = getchar_unlocked();
}
n*=sign;
}
int main() {
int i, j;
while(1) {
read(i); read(j);
I get TLE. The function read() is something I found here on codechef, and I believe it is used by many for fast input. However since I am new to C++ I have simply copied it without understanding the difference between cin and getchar_unlocked. I also do not understand how UVa inputs to my program. I know that my second piece of code contains an infinite loop, but it seems that the first example also loops indefinitely, so I assumed that UVa did not care. What is the difference?