XCorr

Time limit2sMemory limit512 MB

Summary
Given two sparse nonnegative sequences, sum the cross-correlation XCorr(t) over all shifts t in a query range.
Level

Medium6 of 10

Topics
Prefix sum, Math, Sorting, Implementation
Solved
No attempts yet

Problem

There are two sequences of equal length, X=(x0,x1,⋯ ,xn−1)X=(x_0, x_1, \cdots, x_{n-1}) and Y=(y0,y1,⋯ ,yn−1)Y=(y_0, y_1, \cdots, y_{n-1}).

Each element of the two sequences is a nonnegative integer. The following is an example with n=5n=5.

X=(1,0,0,0,1)X=(1,0,0,0,1)

Y=(0,5,2,0,1)Y=(0,5,2,0,1)

For any integer tt, XCorr(t)XCorr(t) is defined as follows.

XCorr(t)=∑i=0n−1xiyi+tXCorr(t)=\displaystyle\sum_{i=0}^{n-1}{x_iy_{i+t}}

(If i<0i<0 or i≥ni\geq n, we take xi=yi=0x_i=y_i=0.)

For example, when tt is 0,1,−10, 1, -1, the value of XCorr(t)XCorr(t) is computed as follows.

XCorr(0)=x0y0+x1y1+⋯+xn−1yn−1XCorr(0) = x_0y_0 + x_1y_1 + \dots + x_{n-1}y_{n-1}

XCorr(1)=x0y1+x1y2+⋯+xn−1ynXCorr(1) = x_0y_1 + x_1y_2 + \dots + x_{n-1}y_n

Cells in gray do not affect the result. y0y_0 is not included in the expression, and xn−1x_{n-1} is multiplied by yn=0y_n=0, so it does not affect the result. Therefore, for the example sequences XX and YY, XCorr(1)XCorr(1) is computed as follows.

1×5+0×2+0×0+0×1=51\times5+0\times2+0\times0+0\times1=5

XCorr(−1)=x0y−1+x1y0+⋯+xn−1yn−2XCorr(-1) = x_0y_{-1} + x_1y_0 + \dots + x_{n-1}y_{n-2}

For any range of tt values (a≤t≤b)(a \le t \le b), the sum of all XCorr(t)XCorr(t), written S(a,b)S(a,b), is defined as follows.

S(a,b)=∑a≤t≤bXCorr(t)S(a,b)=\displaystyle\sum_{a \le t \le b}{XCorr(t)}

Given the sequences XX, YY and the range endpoints aa, bb for tt, write a program that computes S(a,b)S(a,b).

Input

The standard input gives the following information. The first line gives NN, the number of nonzero integers in sequence XX. (This is not the length nn of the sequence.) The next NN lines give, for each positive integer xix_i of sequence XX, its index ii and the value xix_i, in increasing order of index. From the next line onward, sequence YY is given in the same way as XX. (MM, the number of nonzero integers in YY, is given, and the next MM lines give, for each positive integer yiy_i of sequence YY, its index ii and the value yiy_i, in increasing order of index.) The next line gives the integer aa, the minimum of the range of tt, and the line after that gives the integer bb (a≤ba \le b), the maximum of the range of tt.

Output

Print the value of S(a,b)=∑a≤t≤bXCorr(t)S(a,b)=\displaystyle\sum_{a \leq t \leq b}{XCorr(t)} to standard output as an integer.

Constraints

In every subtask, the input values xi,yix_i, y_i satisfy 1≤xi,yi≤3,0001 \leq x_i, y_i \leq 3,000.

Examples2

  1. Example 1

    Input
    3
    0 1
    1 1
    2 1
    3
    0 1
    1 2
    2 3
    -1
    1
    
    Expected output
    14
    
  2. Example 2

    Input
    3
    0 1
    4 4
    9 5
    3
    1 3
    2 7
    10 7
    -2
    2
    
    Expected output
    73