One major improvement and relatively simple one you can use on your program is to replace the I/O methods cin/cout, with scanf()/printf().
Generally, the former ones are a lot of times faster than cin and cout and actually it is an encouraged practice to do so!!
Also, putting the code which “does the actual job” in an auxiliary function and inlining it, might also give you a relatively small performance boost
About the solutions you don’t understand, possibly they are using very fast I/O methods, which are based on reading the numbers from the standard input as a stream of characters and convert them to a number afterwards
How can putting some code in another function and then inlining it give performance boost? Inlining the code itself (without any functions) should be faster compared to any use of functions, right?