School Pairing

Time limit2sMemory limit512 MB

Summary
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 −109-10^9 and 10910^9.

Each year the new students are numbered from 11 to NN, where NN 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 11 and NN. 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 KK, the value he fixes at the start of the day.

The MM 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 NN, MM and KK (1≤N≤1051 \le N \le 10^5, 1≤M≤1051 \le M \le 10^5, 1≤K≤1061 \le K \le 10^6).

The second line has the skill grades of the NN students, listed in order from student 11. Each grade is an integer between −109-10^9 and 10910^9.

Each of the next MM lines has two numbers ii and jj picked by the computer (1≤i,j≤N1 \le i, j \le N). The numbers appear in the order they were picked, so ii may be larger than jj. The range runs from the smaller of the two numbers to the larger one.

A line whose NN, MM and KK are all 00 ends the input. That line is not a test case.

There are at most 2020 test cases. The sum of NN over all test cases and the sum of MM over all test cases are each at most 2×1052 \times 10^5.

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 55. In the first contest the computer picks 11 and 44, so all four students fall inside the range. Only one team in that range has a combined skill of 55, the one made of the first two students. In the second contest the computer picks 33 and 44, and those two grades do not add up to 55, so no team can be formed.

Examples3

  1. Example 1

    Input
    4 2 5
    2 3 1 5
    1 4
    3 4
    0 0 0
    
    Expected output
    1
    0
    
    
  2. Example 2

    Input
    5 4 7
    3 4 3 4 3
    5 1
    2 2
    4 3
    1 3
    0 0 0
    
    Expected output
    6
    0
    1
    2
    
    
  3. Example 3

    Input
    3 1 4
    2 2 2
    1 3
    4 2 3
    1 2 1 2
    1 4
    2 3
    2 1 10
    5 5
    1 2
    0 0 0
    
    Expected output
    3
    
    4
    1
    
    1