CF1404C.Fixed Point Removal
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let a1,…,an be an array of n positive integers. In one operation, you can choose an index i such that ai=i, and remove ai from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x,y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
设 a1,…,an 是一个由 n 个正整数组成的数组。一次操作中,你可以选择一个下标 i,满足 ai=i,然后将 ai 从数组中删除(删除后,剩余部分会拼接在一起)。
数组 a 的权重定义为:你能删除的元素的最大数量。
你需要回答 q 个独立的查询 (x,y):将 a 的前 x 个元素和后 y 个元素均替换为 n+1(使其无法被删除)后,a 的权重是多少?
输入格式
The first line contains two integers n and q (1≤n,q≤3⋅105) — the length of the array and the number of queries.
The second line contains n integers a1, a2, ..., an (1≤ai≤n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x,y≥0 and x+y<n).
第一行包含两个整数 n 和 q(1≤n,q≤3⋅105)—— 分别表示数组的长度和查询次数。
第二行包含 n 个整数 a1, a2, ..., an(1≤ai≤n)—— 数组的元素。
接下来的 q 行中,第 i 行包含两个整数 x 和 y(x,y≥0 且 x+y<n)。
输出格式
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
输出 q 行,第 i 行应包含一个整数——即第 i 个查询的答案。
输入输出样例
输入#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=3 and last y=1 elements impossible to remove, a becomes [×,×,×,9,5,4,6,5,7,8,3,11,×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
- [×,×,×,9,5,4,6,5,7,8,3,11,×]
- [×,×,×,9,4,6,5,7,8,3,11,×]
- [×,×,×,9,4,6,5,7,8,3,×]
- [×,×,×,9,4,5,7,8,3,×]
- [×,×,×,9,4,5,7,3,×]
- [×,×,×,9,4,5,3,×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
第一次查询的解释:
在使最前面 x=3 个元素和最后面 y=1 个元素不可删除之后,数组 a 变为 [×,×,×,9,5,4,6,5,7,8,3,11,×](为清晰起见,我们用 × 表示不可删除的元素)。
以下是一种可删除 5 个元素的策略(被删除的元素以红色标出):
- [×,×,×,9,5,4,6,5,7,8,3,11,×]
- [×,×,×,9,4,6,5,7,8,3,11,×]
- [×,×,×,9,4,6,5,7,8,3,×]
- [×,×,×,9,4,5,7,8,3,×]
- [×,×,×,9,4,5,7,3,×]
- [×,×,×,9,4,5,3,×](最终状态)
无法删除超过 5 个元素,因此该查询的权值为 5。
输入解题思路,AI测评打分。不知道怎么写?