CF1684F.Diverse Segments

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of nn integers. Also you are given mm subsegments of that array. The left and the right endpoints of the jj-th segment are ljl_j and rjr_j respectively.

You are allowed to make no more than one operation. In that operation you choose any subsegment of the array aa and replace each value on this segment with any integer (you are also allowed to keep elements the same).

You have to apply this operation so that for the given mm segments, the elements on each segment are distinct. More formally, for each 1≤j≤m1 \le j \le m all elements alj,alj+1,…,arj−1,arja_{l_{j}}, a_{l_{j}+1}, \ldots, a_{r_{j}-1}, a_{r_{j}} should be distinct.

You don't want to use the operation on a big segment, so you have to find the smallest length of a segment, so that you can apply the operation to this segment and meet the above-mentioned conditions. If it is not needed to use this operation, the answer is 00.

给你一个包含 nn 个整数的数组 aa,以及该数组的 mm 个子区间。第 jj 个区间的左右端点分别为 ljl_j 和 rjr_j。

你最多可以执行一次操作:在该操作中,任选数组 aa 的一个子区间,并将该子区间内的每个元素替换为任意整数(你也可以选择保持某些元素不变)。

你需要执行该操作,使得给定的 mm 个区间各自内部的元素互不相同。更准确地说,对每个 1≤j≤m1 \le j \le m,子数组 alj,alj+1,…,arj−1,arja_{l_{j}}, a_{l_{j}+1}, \ldots, a_{r_{j}-1}, a_{r_{j}} 中的所有元素必须互异。

你希望避免在过长的区间上执行该操作,因此你需要找出满足上述条件所需的最短操作区间长度。若无需执行该操作,则答案为 00。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5) — the size of the array and the number of segments respectively.

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of aa.

Each of the next mm lines contains two integers ljl_j, rjr_j (1≤lj≤rj≤n1 \le l_j \le r_j \le n) — the left and the right endpoints of the jj-th segment.

It's guaranteed that the sum of nn and the sum of mm over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5),分别表示数组的大小和区间的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9),表示数组 aa 的元素。

接下来的 mm 行中,每行包含两个整数 ljl_j、rjr_j(1≤lj≤rj≤n1 \le l_j \le r_j \le n),表示第 jj 个区间的左右端点。

保证所有测试用例的 nn 之和与 mm 之和均不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case output a single integer — the smallest length of a segment you can apply an operation on making the elements on all given segments distinct. If it is not needed to use the operation, output 00.

对于每个测试用例,输出一个整数——即能够通过执行一次操作使得所有给定区间内的元素互不相同的最短区间的长度。如果无需执行该操作,则输出 00。

输入输出样例

  • 输入#1

    5
    7 3
    1 1 2 1 3 3 5
    1 4
    4 5
    2 4
    5 2
    10 1 6 14 1
    4 5
    2 4
    4 5
    5 7 5 6
    2 2
    1 3
    2 4
    3 3
    3 4
    7 3
    2 2 2 7 8 2 2
    4 4
    4 4
    5 5
    1 1
    123
    1 1

    输出#1

    2
    0
    1
    0
    0

说明/提示

In the first test case you can perform the operation on the segment [1,2][1, 2] and make a=[5,6,2,1,3,3,5]a = [5, 6, 2, 1, 3, 3, 5]. Then the elements on each segment are distinct.

  • On the segment [1,4][1, 4] there are [5,6,2,1][5, 6, 2, 1].
  • On the segment [4,5][4, 5] there are [1,3][1, 3].
  • On the segment [2,4][2, 4] there are [6,2,1,3][6, 2, 1, 3].

This way on each of the given segments all elements are distinct. Also, it is impossible to change any single integer to make elements distinct on each segment. That is why the answer is 22.

In the second test case the elements on each segment are already distinct, so we should not perform the operation.

In the third test case we can replace the first 55 by 11. This way we will get [1,7,5,6][1, 7, 5, 6] where all elements are distinct so on each given segment all elements are distinct.

在第一个测试用例中,你可以对区间 [1,2][1, 2] 执行操作,使数组变为 a=[5,6,2,1,3,3,5]a = [5, 6, 2, 1, 3, 3, 5]。此时,每个给定区间内的元素均互不相同。

  • 在区间 [1,4][1, 4] 上,元素为 [5,6,2,1][5, 6, 2, 1]。
  • 在区间 [4,5][4, 5] 上,元素为 [1,3][1, 3]。
  • 在区间 [2,4][2, 4] 上,元素为 [6,2,1,3][6, 2, 1, 3]。

这样,所有给定区间内的元素均互不相同。此外,仅修改单个整数无法使所有给定区间内的元素均互不相同。因此答案为 22。

在第二个测试用例中,每个给定区间内的元素已经互不相同,因此无需执行任何操作。

在第三个测试用例中,我们可以将第一个 55 替换为 11。这样得到数组 [1,7,5,6][1, 7, 5, 6],其中所有元素互不相同,故每个给定区间内的元素也均互不相同。

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

首页