#P17021. [ROI 2026 Day1] 体育训练

[ROI 2026 Day1] 体育训练

Problem Description

Several students are training in a sports club. At the start of the training, there are nn people in the gym, and later qq more people join during the class. All n+qn + q students have distinct heights, and we label them from 11 to n+qn + q in increasing order of height.

During training, the students do a ball-passing exercise. They stand in a line from left to right in some order. Depending on this order, some pairs of students form valid pairs.

For i<ji < j, the two students at positions ii and jj form a valid pair if and only if at least one of the following two conditions holds:

  • The student at position ii is the leftmost among the students to the left of position jj who are shorter than the student at position jj.
  • The student at position jj is the rightmost among the students to the right of position ii who are shorter than the student at position ii.

For example, if the students' labels from left to right are [6,7,3,5,1,2][6, 7, 3, 5, 1, 2], then the valid pairs include (6,2)(6, 2), (6,7)(6, 7), (7,2)(7, 2), (3,2)(3, 2), (3,5)(3, 5), (5,2)(5, 2), and (1,2)(1, 2).

This exercise has two difficulty levels, and each level allows its own set of valid passes. During an exercise session at any difficulty level, it is forbidden to pass the ball to a student who has already received the ball.

At the first difficulty level, a student may only pass the ball to the shorter person among the students that form a valid pair with them. For example, if the line is [6,7,3,5,1,2][6, 7, 3, 5, 1, 2], then student 33 can only pass to student 22; student 55 can pass to 33 and 22; student 11 cannot pass to anyone.

At the second difficulty level, a student may pass the ball to anyone who forms a valid pair with them. For example, in the arrangement [6,7,3,5,1,2][6, 7, 3, 5, 1, 2] above, student 33 can pass to 22 and 55; student 55 can pass to 33 and 22; student 11 can pass to 22.

The exercise proceeds as follows. The coach chooses the difficulty level tt. One student holds the ball and makes one valid pass. The student who receives the ball makes another valid pass, and so on. Passing continues until no further pass is possible. If there are multiple valid passes available, any one may be chosen, but it is forbidden to pass to a student who has already received the ball in this session. The participants will complete as many valid passes as possible under the chosen difficulty level.

Then there are qq times when new members join the training. Each newly joined student stands at the far left or the far right of the existing line. After that, the exercise is performed again under the same difficulty level.

For the initial group of trainees, and after each new student joins, you need to compute the maximum number of passes that the participants can complete.

Input Format

The first line contains an integer tt (1≤t≤21 \le t \le 2), which indicates the difficulty level of the exercise.

The second line contains two integers nn and qq (1≤n≤1051 \le n \le 10^5, 0≤q≤2⋅1050 \le q \le 2 \cdot 10^5), representing the initial number of participants and the number of people who join later.

The third line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n+q1 \le a_i \le n + q), representing the students' labels from left to right at the beginning. It is guaranteed that all labels are distinct.

The next qq lines describe the joining students. Each line contains a character L or R and an integer xx (1≤x≤n+q1 \le x \le n + q), separated by a space. L means the student with label xx stands at the far left of the line, and R means they stand at the far right.

It is guaranteed that after each insertion, all student labels are still distinct.

Output Format

Output an integer on the first line: the answer for the initial nn students at difficulty tt.

On the next qq lines, output one integer per line: the answer after each new student joins and the exercise is completed again at the same difficulty.

1
6 2
6 7 3 5 1 2
L 8
R 4
3
3
5
2
6 2
6 7 3 5 1 2
L 8
R 4

4
4
6
1
5 4
4 3 1 6 2
R 7
L 8
R 9
L 5
3
3
4
5
4
2
5 4
9 4 6 8 2
R 1
L 7
R 5
R 3
4
4
5
7
6

Hint

Explanation

In the first sample, an optimal exercise can start from student 55. The first pass can be to 33, the second to 22, and the third to 11. Adding student 88 on the left does not increase the maximum number of passes. After adding student 44 on the right, the exercise can start from 77 and pass in order to 66, 44, 33, 22, 11.

In the second sample, it can also start from 55 and complete four valid passes, in order to 33, 22, 77, 66. Adding student 88 on the left does not change the maximum number of passes. After adding student 44 on the right, for example starting from 77, it can pass in order to 66, 44, 55, 33, 22, 11.

Subtasks

Subtask Points tt nn and qq Additional constraints Depends on subtasks
1 6 t=1t = 1 n+q≤16n + q \le 16 -- --
2 4 ^ n,q≤100n, q \le 100 1
3 n≤1000n \le 1000,q=0q = 0 --
4 5 n,q≤1000n, q \le 1000 1–3
5 3 q=0q = 0 3
6 10 n=1n = 1 a1=1a_1 = 1;students join in increasing order of labels --
7 6 -- It is guaranteed that the initial participants, their order, the joining order of the remaining students, and the joining side are all random
8 5 n,q≤50 000n, q \le 50\,000 -- 1–4
9 8 -- 1–8
10 4 t=2t = 2 n+q≤16n + q \le 16 --
11 6 ^ n,q≤100n, q \le 100 10
12 5 n≤1000n \le 1000,q=0q = 0 --
13 9 n,q≤1000n, q \le 1000 10–12
14 3 q=0q = 0 12
15 6 n=1n = 1 a1=1a_1 = 1;students join in increasing order of labels --
16 -- It is guaranteed that the initial participants, their order, the joining order of the remaining students, and the joining side are all random
17 7 n,q≤50 000n, q \le 50\,000 -- 10–13
18 4 -- 10–17

Translated by DeepSeek V4 Pro.

Translated by ChatGPT 5