You can download the pdf of this problem from eeclass : https://eeclass.nthu.edu.tw/filedownload/2164163
Since the input size is large, it is recommended to use scanf/printf to process I/O.
If you want to use cin/cout, please add ios::sync_with_stdio(0); and cin.tie(0); at the begining of the main function, and avoid using endl to speed up I/O.
