PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000139. Tổng trị tuyệt đối hai tiền tố (Abs Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Tổng trị tuyệt đối hai tiền tố (Abs Sum)

Abs Sum

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two nonnegative integer arrays A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N) and B=(B1,B2,…,BN)B=(B_1,B_2,\ldots,B_N), each of length NN, and KK independent queries. For a query (X,Y)(X,Y) compute

F(X,Y)=∑i=1X∑j=1Y∣Ai−Bj∣.F(X,Y)=\sum_{i=1}^{X}\sum_{j=1}^{Y}|A_i-B_j|.

That is, pair every element among the first XX elements of AA with every element among the first YY elements of BB, and sum the absolute differences. Exactly X⋅YX\cdot Y pairs contribute; equal values also form pairs, contributing 00.

The indices X,YX,Y are the lengths of prefixes, both starting from 11, not arbitrary subarray endpoints. Queries do not modify the arrays and do not affect one another. Answers can exceed the 32-bit signed integer range; use 64-bit arithmetic. No modulus is applied.

Input

The first line contains NN. The second line contains NN values A1,…,ANA_1,\ldots,A_N, and the third contains NN values B1,…,BNB_1,\ldots,B_N. The next line contains KK. Each of the following KK lines contains Xk,YkX_k,Y_k.

Output

Print exactly KK lines in query order; line kk contains F(Xk,Yk)F(X_k,Y_k) as a decimal integer, without taking a modulus.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le 30; 0≤Ai,Bi≤2⋅1080\le A_i,B_i\le 2\cdot10^8 for all 1≤i≤N1\le i\le N; 1≤K≤501\le K\le 50; 1≤Xk,Yk≤N1\le X_k,Y_k\le N for every query.
  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 0≤Ai,Bi≤2⋅1080\le A_i,B_i\le 2\cdot10^8 for all 1≤i≤N1\le i\le N; 1≤K≤10001\le K\le 1000; 1≤Xk,Yk≤N1\le X_k,Y_k\le N for every query.
  • Subtask 3 (50 points): 1≤N≤1000001\le N\le 100000; 0≤Ai,Bi≤2⋅1080\le A_i,B_i\le 2\cdot10^8 for all 1≤i≤N1\le i\le N; 1≤K≤100001\le K\le 10000; 1≤Xk,Yk≤N1\le X_k,Y_k\le N for every query.

Examples

Example 1

Input

2
2 4
3 5
4
1 1
1 2
2 1
2 2

Output

1
4
2
6

Explanation

For A=[2,4]A=[2,4] and B=[3,5]B=[3,5], sum all pairs from the requested prefixes:

  • (1,1)(1,1): ∣2−3∣=1|2-3|=1.
  • (1,2)(1,2): ∣2−3∣+∣2−5∣=1+3=4|2-3|+|2-5|=1+3=4.
  • (2,1)(2,1): ∣2−3∣+∣4−3∣=1+1=2|2-3|+|4-3|=1+1=2.
  • (2,2)(2,2): ∣2−3∣+∣2−5∣+∣4−3∣+∣4−5∣=1+3+1+1=6|2-3|+|2-5|+|4-3|+|4-5|=1+3+1+1=6.

Hence the four output lines are 1,4,2,61,4,2,6.

Example 2

Input

5
1163686 28892 1263085 2347878 520306
1332157 1202905 2437161 1291976 563395
5
5 3
1 5
2 3
1 2
5 5

Output

13331322
2209746
6366712
207690
20241215

Explanation

For (5,3)(5,3), the respective contributions of the five values of AA against B1,B2,B3B_1,B_2,B_3 are 14811651481165, 48855474885547, 13033281303328, 22499772249977, and 34113053411305, totaling 1333132213331322. For (1,2)(1,2), the answer is $|1163686-1332157|+|1163686-1202905|=168471+39219=207690$. The other queries yield 22097462209746, 63667126366712, and 2024121520241215 in their respective output positions.