#FT0000216. Loại bỏ phần tử bằng vị trí (Fixed Point Removal)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Loại bỏ phần tử bằng vị trí (Fixed Point Removal)
Fixed Point Removal
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers. In its current state, you may choose an index whose value equals its current index, meaning , and remove that element. Elements to its right shift one position left, so their indices change.
The weight of an array is the maximum number of elements that can be removed by performing such operations in any valid order.
There are independent queries. For a query , replace the first and last elements of the original array with . Such elements cannot be removed because their values exceed every possible current index. All other values stay unchanged.
For each query, find the weight of the modified array. Every query starts from the original array, without retaining changes or deletions from previous queries.
Input
- The first line contains two integers : the array length and query count.
- The second line contains exactly positive integers , in order.
- Each of the next lines contains integers : the numbers of first and last elements to replace with .
Output
Print exactly lines, one integer per line. Line gives the maximum number of removable elements for query , in input order.
Subtasks
- Subtask 1 (20 points): ; ; for every ; each query has and .
- Subtask 2 (30 points): ; ; for every ; each query has and .
- Subtask 3 (50 points): ; ; for every ; each query has and .
Examples
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Explanation
The original array is .
- Query changes nothing. Remove the first value because it equals its current index , obtaining . Then remove the last value because its current index is , obtaining . No remaining value equals its index. The maximum number of removals is .
- Query replaces the first value with , resulting in . No value equals its index: , , , , and . Thus no first removal is possible, and the answer is .
The two output lines are 2 and 0.