CF1404C.Fixed Point Removal

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let a1,,ana_1, \ldots, a_n be an array of nn positive integers. In one operation, you can choose an index ii such that ai=ia_i = i , and remove aia_i from the array (after the removal, the remaining parts are concatenated).

The weight of aa is defined as the maximum number of elements you can remove.

You must answer qq independent queries (x,y)(x, y) : after replacing the xx first elements of aa and the yy last elements of aa by n+1n+1 (making them impossible to remove), what would be the weight of aa ?

输入格式

The first line contains two integers nn and qq ( 1n,q31051 \le n, q \le 3 \cdot 10^5 ) — the length of the array and the number of queries.

The second line contains nn integers a1a_1 , a2a_2 , ..., ana_n ( 1ain1 \leq a_i \leq n ) — elements of the array.

The ii -th of the next qq lines contains two integers xx and yy ( x,y0x, y \ge 0 and x+y<nx+y < n ).

输出格式

Print qq lines, ii -th line should contain a single integer — the answer to the ii -th query.

输入输出样例

  • 输入#1

    13 5
    2 2 3 9 5 4 6 5 7 8 3 11 13
    3 1
    0 0
    2 4
    5 0
    0 12

    输出#1

    5
    11
    6
    1
    0
  • 输入#2

    5 2
    1 4 1 2 4
    0 0
    1 0

    输出#2

    2
    0

说明/提示

Explanation of the first query:

After making first x=3x = 3 and last y=1y = 1 elements impossible to remove, aa becomes [×,×,×,9,5,4,6,5,7,8,3,11,×][\times, \times, \times, 9, 5, 4, 6, 5, 7, 8, 3, 11, \times] (we represent 1414 as ×\times for clarity).

Here is a strategy that removes 55 elements (the element removed is colored in red):

  • [×,×,×,9,5,4,6,5,7,8,3,11,×][\times, \times, \times, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, \times]
  • [×,×,×,9,4,6,5,7,8,3,11,×][\times, \times, \times, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, \times]
  • [×,×,×,9,4,6,5,7,8,3,×][\times, \times, \times, 9, 4, \color{red}{6}, 5, 7, 8, 3, \times]
  • [×,×,×,9,4,5,7,8,3,×][\times, \times, \times, 9, 4, 5, 7, \color{red}{8}, 3, \times]
  • [×,×,×,9,4,5,7,3,×][\times, \times, \times, 9, 4, 5, \color{red}{7}, 3, \times]
  • [×,×,×,9,4,5,3,×][\times, \times, \times, 9, 4, 5, 3, \times] (final state)

It is impossible to remove more than 55 elements, hence the weight is 55 .

首页