#P8797. [蓝桥杯 2022 国 A] 三角序列
[蓝桥杯 2022 国 A] 三角序列
Background
Thanks to Lotuses for providing the testdata.
Problem Description
Given pairs of numbers , each pair represents a triangle with rows and columns as shown in the figure:

When , the left side is lower; when , the right side is lower.
Concatenate the triangles corresponding to each pair from left to right in the given order.
Now there are queries . For each query, find the minimum height such that, between columns and , the number of whose height is within is at least .
Input Format
The first line contains two integers , separated by a space.
The next lines each contain two integers , separated by a space.
The next lines each contain three integers , where adjacent integers are separated by a space.
Output Format
Output one line containing a string representing the answer.
6 6
3 0
4 0
2 1
3 1
5 0
1 1
3 9 12
3 9 13
3 4 4
14 16 7
9 15 12
1 18 42
2
3
3
3
3
-1
Hint
Sample Explanation
The range corresponding to the first query is shown in the figure:

Constraints
- For of the test cases, .
- For of the test cases, .
- For all test cases, , , , , .
Lanqiao Cup 2022 National Contest A Group, Problem I.
Translated by ChatGPT 5