#P7769. Matching Contiguous Segments

    ID: 20955 Type: Default 1000ms 256MiB

Matching Contiguous Segments

Matching Contiguous Segments

There are n students standing in a line, each with an ugly value \(a_i\). Two teachers, ac and mc, will each choose a contiguous segment of students for a special lesson. They have to choose segments of the same length such that for every corresponding position, the sum of the two students' ugly values equals a fixed number \(s\). In other words, if mc chooses the segment with indices \(l, l+1, \dots, r\) (1-indexed) and ac chooses another segment starting at index \(b\) (also contiguous, of length \(m=r-l+1\)), then it must hold that

[ a_{l+k} + a_{b+k} = s, \quad \text{for all } k=0,1,\dots,m-1. ]

ac does not know which segment mc has chosen, or what \(s\) is. You are given several queries. For each query, you are given an integer \(s\) and two indices \(l\) and \(r\), representing the segment that mc has chosen. You need to determine whether there exists a starting position \(b\) (with \(1 \le b \le n-m+1\)) such that for every \(k=0,1,\dots, m-1\) we have

[ a_{l+k} + a_{b+k} = s. ]

Input Format: The first line contains a single integer \(n\) (the number of students). The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\). The third line contains a single integer \(q\) (the number of queries). Each of the next \(q\) lines contains three integers \(s, l, r\), describing a query.

Output Format: For each query, output a single line containing "Yes" if there is a contiguous segment for ac satisfying the condition, or "No" otherwise.

Example:

Input:
5
2 4 2 4 2
3
6 1 3
6 2 4
7 1 3

Output: Yes Yes No

</p>

Explanation:

  • Query 1: For \(s=6, l=1, r=3\): The segment chosen by mc is [2, 4, 2]. The condition requires ac’s segment \(b, b+1, b+2\) to satisfy \(2 + a_b = 6, 4 + a_{b+1} = 6, 2 + a_{b+2} = 6\), i.e. ac must choose the segment [4, 2, 4]. This segment exists starting at index 2.
  • Query 2: For \(s=6, l=2, r=4\): The segment for mc is [4, 2, 4]. Then ac should choose [2, 4, 2] which exists starting at index 1 (or index 3).
  • Query 3: For \(s=7, l=1, r=3\): The required segment is [5, 3, 5], which does not appear in the array.

inputFormat

The input begins with a line containing the integer n (the number of students).

The next line contains n space-separated integers \(a_1, a_2, \dots, a_n\), representing the ugly values of the students.

The following line contains an integer q, the number of queries. Each of the next q lines contains three integers: s, l, and r, representing a query as described above.

outputFormat

For each query, output a single line containing either "Yes" if there exists a contiguous segment for ac that satisfies the condition, or "No" if no such segment exists.

sample

5
2 4 2 4 2
3
6 1 3
6 2 4
7 1 3
Yes

Yes No

</p>