#P9180. [COCI 2022/2023 #5] Slastičarnica

[COCI 2022/2023 #5] Slastičarnica

Problem Description

There is a sequence a1,,ana_1,\ldots,a_n and qq operations. Each operation is of the form: “Delete a prefix or suffix of length did_i, and you must ensure that all elements in this prefix or suffix are greater than or equal to sis_i.” Before each operation, you may choose a prefix and/or suffix of any length (they can be empty, and you may choose both ends at the same time) and delete them. If at some operation it cannot be performed, then this operation and all following operations stop. Ask for the maximum number of operations that can be performed.

Input Format

The first line contains two positive integers n,q (1n5 000,1q2105)n,q\ (1\le n\le 5\ 000,1\le q\le 2\cdot 10^5), representing the sequence length and the number of operations.

The second line contains nn positive integers ai (1ai109)a_i\ (1\le a_i\le 10^9), representing the sequence.

The next qq lines each contain two integers di,si (1din,1si109)d_i,s_i\ (1\le d_i\le n,1\le s_i\le 10^9), describing one operation.

Output Format

Output one line with one integer, representing the maximum number of operations that can be performed.

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

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

2
9 5
1 3 2 5 1 4 6 2 1
3 2
2 3
1 1
1 2
1 1

4

Hint

Explanation for sample 33:

First delete the prefix (1)(1), then perform the first operation and delete the prefix (3,2,5)(3,2,5). The sequence becomes (1,4,6,2,1)(1,4,6,2,1).

Then delete the prefix (1)(1), then perform the second operation and delete the prefix (4,6)(4,6). The sequence becomes (2,1)(2,1).

Then do not delete any prefix or suffix, perform the third operation, and delete the suffix (1)(1). The sequence becomes (2)(2).

Then do not delete any prefix or suffix, perform the fourth operation, and delete the only remaining (2)(2). The sequence becomes empty.

The last operation cannot be completed because the sequence is empty, so the operations stop.

Therefore, a total of four operations are performed.

Subtask ID Additional Constraints Score
00 Sample only 00
11 n,q100n,q\le 100 1919
22 d1=d2==dq=1d_1=d_2=\ldots=d_q=1 2828
33 No additional constraints 5353

Translated by ChatGPT 5