CF1404C.Fixed Point Removal

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

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?

设 a1,…,ana_1, \ldots, a_n 是一个由 nn 个正整数组成的数组。一次操作中,你可以选择一个下标 ii,满足 ai=ia_i = i,然后将 aia_i 从数组中删除(删除后,剩余部分会拼接在一起)。

数组 aa 的权重定义为:你能删除的元素的最大数量。

你需要回答 qq 个独立的查询 (x,y)(x, y):将 aa 的前 xx 个元素和后 yy 个元素均替换为 n+1n+1(使其无法被删除)后,aa 的权重是多少?

输入格式

The first line contains two integers nn and qq (1≤n,q≤3⋅1051 \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 (1≤ai≤n1 \leq a_i \leq n) — elements of the array.

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

第一行包含两个整数 nn 和 qq(1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5)—— 分别表示数组的长度和查询次数。

第二行包含 nn 个整数 a1a_1, a2a_2, ..., ana_n(1≤ai≤n1 \leq a_i \leq n)—— 数组的元素。

接下来的 qq 行中,第 ii 行包含两个整数 xx 和 yy(x,y≥0x, y \ge 0 且 x+y<nx+y \lt n)。

输出格式

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

输出 qq 行,第 ii 行应包含一个整数——即第 ii 个查询的答案。

输入输出样例

  • 输入#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.

第一次查询的解释:

在使最前面 x=3x = 3 个元素和最后面 y=1y = 1 个元素不可删除之后,数组 aa 变为 [×,×,×,9,5,4,6,5,7,8,3,11,×][\times, \times, \times, 9, 5, 4, 6, 5, 7, 8, 3, 11, \times](为清晰起见,我们用 ×\times 表示不可删除的元素)。

以下是一种可删除 55 个元素的策略(被删除的元素以红色标出):

  • [×,×,×,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](最终状态)

无法删除超过 55 个元素,因此该查询的权值为 55。

输入解题思路,AI测评打分。不知道怎么写?

首页