PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000216. Loại bỏ phần tử bằng vị trí (Fixed Point Removal)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 a1,a2,…,ana_1,a_2,\ldots,a_n of positive integers. In its current state, you may choose an index ii whose value equals its current index, meaning ai=ia_i=i, 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 qq independent queries. For a query (x,y)(x,y), replace the first xx and last yy elements of the original array with n+1n+1. 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 n,qn,q: the array length and query count.
  • The second line contains exactly nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, in order.
  • Each of the next qq lines contains integers x,yx,y: the numbers of first and last elements to replace with n+1n+1.

Output

Print exactly qq lines, one integer per line. Line jj gives the maximum number of removable elements for query jj, in input order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; 1≤q≤151\le q\le15; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n; each query has 0≤x,y0\le x,y and x+y<nx+y<n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤q≤20001\le q\le2000; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n; each query has 0≤x,y0\le x,y and x+y<nx+y<n.
  • Subtask 3 (50 points): 1≤n≤3⋅1051\le n\le3\cdot10^5; 1≤q≤3⋅1051\le q\le3\cdot10^5; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n; each query has 0≤x,y0\le x,y and x+y<nx+y<n.

Examples

Input

5 2
1 4 1 2 4
0 0
1 0

Output

2
0

Explanation

The original array is [1,4,1,2,4][1,4,1,2,4].

  • Query (0,0)(0,0) changes nothing. Remove the first value 11 because it equals its current index 11, obtaining [4,1,2,4][4,1,2,4]. Then remove the last value 44 because its current index is 44, obtaining [4,1,2][4,1,2]. No remaining value equals its index. The maximum number of removals is 22.
  • Query (1,0)(1,0) replaces the first value with 6=n+16=n+1, resulting in [6,4,1,2,4][6,4,1,2,4]. No value equals its index: 6≠16\ne1, 4≠24\ne2, 1≠31\ne3, 2≠42\ne4, and 4≠54\ne5. Thus no first removal is possible, and the answer is 00.

The two output lines are 2 and 0.