CF1864F.Exotic Queries
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
AquaMoon gives RiverHamster a sequence of integers a1,a2,…,an , and RiverHamster gives you q queries. Each query is expressed by two integers l and r .
For each query independently, you can take any continuous segment of the sequence and subtract an identical non-negative value from all the numbers of this segment. You can do so multiple (possibly, zero) times. However, you may not choose two intersecting segments which are not included in one another. Your goal is to convert to 0 all numbers whose initial value was within the range [l,r] . You must do so in the minimum number of operations.
Please note that the queries are independent, the numbers in the array are restored to their initial values between the queries.
Formally, for each query, you are to find the smallest m such that there exists a sequence {(xj,yj,zj)}j=1m satisfying the following conditions:
- for any 1≤j≤m , zj≥0 and 1≤xj≤yj≤n (here [xj,yj] correspond to the segment of the sequence);
- for any 1≤j<k≤m , it is true that [xj,yj]⊆[xk,yk] , or [xk,yk]⊆[xj,yj] , or [xj,yj]∩[xk,yk]=∅ ;
- for any 1≤i≤n , such that l≤ai≤r , it is true that $$$${\large a_i = \sum\limits_{\substack {1 \le j \le m \ x_j \le i \le y_j}} z_j. } $$$$
输入格式
The first line contains two integers n and q ( 1≤n,q≤106 ).
The second line contains n integers integers a1,a2,…,an ( 1≤ai≤n ).
Each of the next q lines contains two integers l and r ( 1≤l≤r≤n ), representing a query.
输出格式
For each query, output the answer for this query on a separate line.
输入输出样例
输入#1
10 8 1 6 2 3 2 6 3 10 1 2 1 10 2 2 3 3 2 3 1 3 3 6 4 6 5 5
输出#1
8 1 1 3 5 3 1 0
输入#2
3 1 1 3 2 1 3
输出#2
3
说明/提示
In the first test case, consider the second query, when l=2 , r=2 . The elements to be manipulated are [a3,a5,a10]=[2,2,2] . It is sufficient to apply the operation sequence {(2,10,2)} .
Consider the fourth query, when l=2 , r=3 . The elements to be manipulated are [a3,a4,a5,a7,a10]=[2,3,2,3,2] . It is sufficient to apply the operation sequence {(1,10,2),(4,4,1),(7,7,1)} .
In the second test case, note that the operation sequence {(1,2,1),(2,3,2)} is invalid because the two segments intersect but neither is contained inside the other.