Bob's Sequence Problem

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem types

Bob has a sequence A consisting of N integers, denoted as: a_1, a_2, \ldots, a_N.

Bob doesn't like sequences that stay the same, so he's planning to perform M update operations. Each update is described by a pair of integers (i, x), meaning Bob will change a_i to x during that operation.

However, all updates are hypothetical - they are applied independently and always refer to the original sequence A.

Bob is fascinated by non-decreasing subsequences. He wants to find a subsequence of the original sequence that satisfies the following condition. The subsequence must remain non-decreasing in the original sequence and after each of the M updates.

Write a program to compute the maximum possible length of such a persistent non-decreasing subsequence.

Input Specification

The first line of input contains two integers N and M (1 \le N, M \le 10^5), indicating the length of the sequence and the number of update operations, respectively.

The second line of input contains N integers, a_i (1 \le a_i \le 10^5), the initial sequence.

Each of the following M lines contains two integers i and x (1 \le i \le N and 1 \le x \le 10^5), indicating an update operation to change a_i to x.

Output Specification

Output one integer, the length of the longest subsequence which is non-decreasing in the initial sequence and also non-decreasing after each update operation.

Constraints

Subtask Points Additional constraints
1 20 N, M \le 300.
2 30 N, M \le 3000.
3 50 No additional constraints.

Sample Input

4 5
1 2 3 5
1 2
2 3
2 1
3 4
4 1

Sample Output

3

Explanation for Sample

The original sequence and the sequence after each update are listed as follows:

  • [1, 2, 3, 5]
  • [2, 2, 3, 5]
  • [1, 3, 3, 5]
  • [1, 1, 3, 5]
  • [1, 2, 4, 5]
  • [1, 2, 3, 1]

Bob can choose the first three integers in his subsequence, which is non-decreasing for the original sequence and also for the sequence after each update.


Comments

There are no comments at the moment.