CF BUDDY
← Problems·

1991F · Triangle Formation

2200 · brute force, greedy, implementation

Problem: You are given nn sticks, numbered from 11 to nn. The length of the ii-th stick is aia_i.

You need to answer qq queries. In each query, you are given two integers ll and rr (1l<rn1 \le l < r \le n, rl+16r - l + 1 \ge 6). Determine whether it is possible to choose 66 distinct sticks from the sticks numbered ll to rr, to form 22 non-degenerate triangles*^{\text{*}}.

Input Format: The first line contains two integers nn and qq (6n1056 \le n \le 10^5, 1q1051 \le q \le 10^5) — the number of sticks and the number of queries respectively.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — aia_i denotes the length of the ii-th stick.

Each of the following qq lines contains two integers ll and rr (1l<rn1 \le l < r \le n, rl+16r - l + 1 \ge 6) — the parameters of each query.

Output Format: For each query, output "YES" (without quotes) if it is possible to form 22 triangles, and "NO" (without quotes) otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Note: In the first query, the lengths of the sticks are [5,2,2,10,4,10][5, 2, 2, 10, 4, 10]. Two sets of sticks [2,4,5][2, 4, 5] and [2,10,10][2, 10, 10] can be selected to form 22 non-degenerate triangles.

In the second query, the lengths of the sticks are [2,2,10,4,10,6][2, 2, 10, 4, 10, 6]. It can be shown that it is impossible to form 22 non-degenerate triangles.

In the third query, the lengths of the sticks are [2,2,10,4,10,6,1][2, 2, 10, 4, 10, 6, 1]. Two sets of sticks [1,2,2][1, 2, 2] and [4,10,10][4, 10, 10] can be selected to form 22 non-degenerate triangles.

In the fourth query, the lengths of the sticks are [4,10,6,1,5,3][4, 10, 6, 1, 5, 3]. It can be shown that it is impossible to form 22 non-degenerate triangles.

In the fifth query, the lengths of the sticks are [10,4,10,6,1,5,3][10, 4, 10, 6, 1, 5, 3]. Two sets of sticks [1,10,10][1, 10, 10] and [3,4,5][3, 4, 5] can be selected to form 22 non-degenerate triangles.

Sample Cases

Case 1

Input

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

Output

YES
NO
YES
NO
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback