CF1864F.Exotic Queries

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

AquaMoon gives RiverHamster a sequence of integers a1,a2,…,ana_1,a_2,\dots,a_n, and RiverHamster gives you qq queries. Each query is expressed by two integers ll and rr.

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 00 all numbers whose initial value was within the range [l,r][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 mm such that there exists a sequence (xj,yj,zj)j=1m{(x_j,y_j,z_j)}_{j=1}^{m} satisfying the following conditions:

  • for any 1≤j≤m1 \le j \leq m, zj≥0z_j \ge 0 and 1≤xj≤yj≤n1 \le x_j \le y_j \leq n (here [xj,yj][x_j, y_j] correspond to the segment of the sequence);
  • for any 1≤j<k≤m1 \le j \lt k \le m, it is true that [xj,yj]⊆[xk,yk][x_j,y_j]\subseteq[x_{k},y_{k}], or [xk,yk]⊆[xj,yj][x_k,y_k]\subseteq[x_{j},y_{j}], or [xj,yj]∩[xk,yk]=∅[x_j,y_j]\cap[x_{k},y_{k}]=\varnothing;
  • for any 1≤i≤n1 \le i \le n, such that l≤ai≤rl \le a_i \leq 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. }$$

AquaMoon 给 RiverHamster 一个整数序列 a1,a2,…,ana_1,a_2,\dots,a_n,RiverHamster 则向你提出 qq 个查询。每个查询由两个整数 ll 和 rr 表示。

对于每个查询(相互独立地),你可以任选序列的一个连续子段,并将该子段内所有数同时减去一个相同的非负值。该操作可执行任意多次(包括零次)。但禁止选择两个相交但互不包含的子段。你的目标是:将所有初始值落在区间 [l,r][l, r] 内的数全部变为 00。你需要用最少的操作次数达成此目标。

请注意,各查询相互独立;每次查询开始前,数组中的数值均恢复为初始值。

形式化地,对每个查询,你需要找出最小的 mm,使得存在一个三元组序列 (xj,yj,zj)j=1m{(x_j,y_j,z_j)}_{j=1}^{m},满足以下条件:

  • 对任意 1≤j≤m1 \le j \leq m,有 zj≥0z_j \ge 0 且 1≤xj≤yj≤n1 \le x_j \le y_j \leq n(其中 [xj,yj][x_j, y_j] 表示序列的一个子段);
  • 对任意 1≤j<k≤m1 \le j \lt k \le m,恒有 [xj,yj]⊆[xk,yk][x_j,y_j]\subseteq[x_{k},y_{k}],或 [xk,yk]⊆[xj,yj][x_k,y_k]\subseteq[x_{j},y_{j}],或 [xj,yj]∩[xk,yk]=∅[x_j,y_j]\cap[x_{k},y_{k}]=\varnothing;
  • 对任意 1≤i≤n1 \le i \le n,若 l≤ai≤rl \le a_i \leq r,则成立

    a_i=sumlimits_substack1lejlemx_jleiley_jz_j.{\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 nn and qq (1≤n,q≤1061\le n,q\le 10^6).

The second line contains nn integers integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n).

Each of the next qq lines contains two integers ll and rr (1≤l≤r≤n1\le l\le r\le n), representing a query.

第一行包含两个整数 nn 和 qq(1≤n,q≤1061\le n,q\le 10^6)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)。

接下来的 qq 行,每行包含两个整数 ll 和 rr(1≤l≤r≤n1\le l\le r\le n),表示一个查询。

输出格式

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=2l = 2, r=2r = 2. The elements to be manipulated are [a3,a5,a10]=[2,2,2][a_3, a_5, a_{10}] = [2, 2, 2]. It is sufficient to apply the operation sequence (2,10,2){(2, 10, 2)}.

Consider the fourth query, when l=2l = 2, r=3r = 3. The elements to be manipulated are [a3,a4,a5,a7,a10]=[2,3,2,3,2][a_3, a_4, a_5, a_7, a_{10}] = [2, 3, 2, 3, 2]. It is sufficient to apply the operation sequence (1,10,2),(4,4,1),(7,7,1){(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){(1, 2, 1), (2, 3, 2)} is invalid because the two segments intersect but neither is contained inside the other.

在第一个测试用例中,考虑第二个查询,此时 l=2l = 2,r=2r = 2。需要操作的元素为 [a3,a5,a10]=[2,2,2][a_3, a_5, a_{10}] = [2, 2, 2]。只需应用操作序列 (2,10,2){(2, 10, 2)} 即可。

考虑第四个查询,此时 l=2l = 2,r=3r = 3。需要操作的元素为 [a3,a4,a5,a7,a10]=[2,3,2,3,2][a_3, a_4, a_5, a_7, a_{10}] = [2, 3, 2, 3, 2]。只需应用操作序列 (1,10,2),(4,4,1),(7,7,1){(1, 10, 2), (4, 4, 1), (7, 7, 1)} 即可。

在第二个测试用例中,注意操作序列 (1,2,1),(2,3,2){(1, 2, 1), (2, 3, 2)} 是无效的,因为这两个区间相交,但彼此互不包含。

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

首页