#FT0000139. Tổng trị tuyệt đối hai tiền tố (Abs Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 and , each of length , and independent queries. For a query compute
That is, pair every element among the first elements of with every element among the first elements of , and sum the absolute differences. Exactly pairs contribute; equal values also form pairs, contributing .
The indices are the lengths of prefixes, both starting from , 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 . The second line contains values , and the third contains values . The next line contains . Each of the following lines contains .
Output
Print exactly lines in query order; line contains as a decimal integer, without taking a modulus.
Subtasks
- Subtask 1 (20 points): ; for all ; ; for every query.
- Subtask 2 (30 points): ; for all ; ; for every query.
- Subtask 3 (50 points): ; for all ; ; 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 and , sum all pairs from the requested prefixes:
- : .
- : .
- : .
- : .
Hence the four output lines are .
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 , the respective contributions of the five values of against are , , , , and , totaling . For , the answer is $|1163686-1332157|+|1163686-1202905|=168471+39219=207690$. The other queries yield , , and in their respective output positions.