[SPOJ] LQDNUMS

Does anyone know how to solve this problem?

Or at least please give me the references for it. Thanks a lot in advance.