GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Thursday 19 May 2022

[Solution] Diverse Segments Codeforces Solution | Codeforces Problem Solution 2022

F. Diverse Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a of n integers. Also you are given m subsegments of that array. The left and the right endpoints of the j-th segment are lj and rj respectively.

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

Solution Click Below:-  CLICK HERE

You have to apply this operation so that for the given m segments, the elements on each segment are distinct. More formally, for each 1jm all elements alj,alj+1,,arj1,arj 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 0.

Input

The input consists of multiple test cases. The first line contains a single integer t (1t100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n and m (1n,m2105) — the size of the array and the number of segments respectively.

The next line contains n integers a1,a2,,an (1ai109) — the elements of a.

Each of the next m lines contains two integers ljrj (1ljrjn) — the left and the right endpoints of the j-th segment.

It's guaranteed that the sum of n and the sum of m over all test cases does not exceed 2105.

Output

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 0.

No comments:

Post a Comment