I have talk about the method and the optimization by segment tree of LIS before. Monotone queue is an easier method to optimize the LIS from to .

NYIST 17 is a naked problem: