#P17197. [KOI 2026 #2] 删除局部最小值

[KOI 2026 #2] 删除局部最小值

Problem Description

You are given a sequence A=[A1,A2,⋯ ,AN]A=[A_1,A_2,\cdots,A_N] of length NN, consisting of pairwise distinct integers.

For a sequence BB, the following process is called one transformation:

  • Let B=[B1,B2,⋯ ,BK]B=[B_1,B_2,\cdots,B_K]. For every integer ii satisfying Bi−1>Bi<Bi+1B_{i-1}>B_i<B_{i+1} (2≤i≤K−12 \le i \le K-1), call BiB_i an element to be deleted. Delete all elements to be deleted from BB simultaneously, then keep the relative order of the remaining elements and concatenate them.

For example, after applying the transformation three times to the sequence [5,1,3,2,4][5,1,3,2,4], the sequence changes as follows:

[5,1,3,2,4]→[5,3,4]→[5,4]→[5,4][5,1,3,2,4]\to[5,3,4]\to[5,4]\to[5,4]

You are given QQ queries. Each query consists of three integers l,r,tl,r,t. For each query (l,r,t)(l,r,t), output the number of elements remaining in the sequence [Al,Al+1,⋯ ,Ar][A_l,A_{l+1},\cdots,A_r] after applying the transformation tt times.

Input Format

The first line contains two integers NN and QQ, separated by spaces.

The second line contains NN integers A1,A2,⋯ ,ANA_1,A_2,\cdots,A_N, separated by spaces.

The next QQ lines give the queries. Each line contains three integers l,r,tl,r,t, separated by spaces, representing one query.

Output Format

Output QQ lines of answers starting from the first line. Print each query’s answer on its own line, in the same order as the input.

5 5
5 1 3 2 4
1 5 1
1 5 2
1 4 1
2 5 1
1 5 5
3
2
3
3
2
15 7
14 5 2 7 11 13 3 12 9 4 10 8 1 6 15
1 15 1
1 15 2
1 15 3
1 15 4
1 15 5
1 15 6
1 15 7
11
8
6
4
3
2
2
10 10
9 6 4 1 8 2 3 5 7 10
1 10 1
1 10 2
1 10 5
1 9 3
2 10 2
2 10 4
3 8 1
3 8 2
1 5 4
4 8 3
8
6
2
3
5
3
4
3
2
3

Hint

Constraints

  • All given numbers are integers.
  • 1≤N≤200 0001 \le N \le 200\,000.
  • 1≤Q≤200 0001 \le Q \le 200\,000.
  • The sequence AA is a permutation of 1,2,⋯ ,N1,2,\cdots,N, i.e., {A1,A2,⋯ ,AN}={1,2,⋯ ,N}\{A_1,A_2,\cdots,A_N\}=\{1,2,\cdots,N\}.
  • For each query, 1≤l≤r≤N1 \le l \le r \le N.
  • For each query, 1≤t≤N1 \le t \le N.

Subtasks

  1. (66 points) N≤5 000N \le 5\,000; for each query, l=1l=1 and r=Nr=N.
  2. (1111 points) For each query, l=1l=1 and r=Nr=N.
  3. (66 points) For each query, t=1t=1.
  4. (1212 points) For each query, t=Nt=N.
  5. (77 points) There exists an integer pp (1≤p≤N1 \le p \le N) such that the following conditions both hold:
    • For every integer ii (1≤i≤p−11 \le i \le p-1), Ai>Ai+1A_i>A_{i+1}.
    • For every integer ii (p≤i≤N−1p \le i \le N-1), Ai<Ai+1A_i<A_{i+1}.
  6. (2626 points) After applying 2020 transformations to the sequence A=[A1,A2,⋯ ,AN]A=[A_1,A_2,\cdots,A_N], applying further transformations will not change the sequence anymore.
  7. (3232 points) No additional constraints.

Translated by ChatGPT-5.6.

Translated by ChatGPT 5