School Pairing
Time limit2sMemory limit512 MB
For each query range, count pairs of positions whose skill grades sum to K.
- Level
Medium6 of 10
- Topics
- Hash map, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
The school of Auckbury admits a very large class every year. In one year the intake came close to a million students. The school cares about how well its students do in programming contests, so every student receives a skill grade on admission. A grade is an integer between and .
Each year the new students are numbered from to , where is that year's intake. The numbers identify students in the freshman contests. Those contests are held often during the year and are open only to the newest class.
When a contest starts, the staff computer picks two numbers between and . The students standing between those two numbers, both ends included, pair up into teams of two and compete against other teams. The principal cares about fairness, so he allows a team to compete only when the two skill grades add up to , the value he fixes at the start of the day.
The contests held in one year are given. For each contest, find how many teams can be formed inside the range the computer picked. A team is an unordered pair of two students, two teams are different when their pairs of student numbers differ, and a student who belongs to several teams is counted once for each of them.
Input
The input holds several test cases.
The first line of each test case has three integers , and (, , ).
The second line has the skill grades of the students, listed in order from student . Each grade is an integer between and .
Each of the next lines has two numbers and picked by the computer (). The numbers appear in the order they were picked, so may be larger than . The range runs from the smaller of the two numbers to the larger one.
A line whose , and are all ends the input. That line is not a test case.
There are at most test cases. The sum of over all test cases and the sum of over all test cases are each at most .
Output
For each test case print one line per contest, in the order the contests are given, holding the number of teams that can be formed in that range. Print one blank line after each test case.
Hint
In the first example four students stand in a line and the principal fixes the sum at . In the first contest the computer picks and , so all four students fall inside the range. Only one team in that range has a combined skill of , the one made of the first two students. In the second contest the computer picks and , and those two grades do not add up to , so no team can be formed.