Zip-line
Time limit3sMemory limit1024 MB
For each assumption that one tree has a new height, find the longest strictly increasing subsequence of the trees after that single change.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Binary search, Sorting
- Solved
- No attempts yet
Problem
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long as possible, but he does not remember the heights of all trees in the forest exactly. He is sure that he remembers the correct heights of all trees except, possibly, one of them.
The forest has trees in a row, numbered from left to right with integers from to . According to Vasya, the height of the -th tree is . A zip-line of length () hangs over trees () whose heights form an increasing sequence, that is, . The length of a zip-line is the number of trees that form it.
Petya was in the forest with Vasya and has assumptions about the mistake in Vasya's sequence . His -th assumption consists of two integers and , meaning that according to Petya, the height of tree is actually . Petya's assumptions are independent of each other.
For each of the assumptions, find the maximum length of a zip-line that can be built over the trees under that assumption.
Input
The first line contains two integers and (), the number of trees and the number of Petya's assumptions. The next line contains integers (), the heights of the trees according to Vasya. Each of the next lines contains two integers and (, ).
Output
For each of Petya's assumptions, output one integer: the maximum length of a zip-line that can be built under that assumption.
Hint
Consider the first sample. The first assumption actually coincides with the height remembered by Vasya. In the second assumption the heights of the trees are , in the third one they are and in the fourth one they are .