#SRD0000143. Khoảng cách giữa hai đoạn con (Subarray Distance)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Khoảng cách giữa hai đoạn con (Subarray Distance)
Subarray Distance
Source: MarisaOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a static array of positive integers. Each of the independent queries specifies two index intervals and ; the intervals may overlap or coincide.
Their distance is . Ordered pairs are counted: if the intervals coincide, and are separate contributions for , whereas pairs with contribute zero.
Calculate the distance for each query. The array does not change.
Input
- The first line contains .
- The second line contains integers .
- Each of the next lines contains , giving two one-based inclusive intervals.
Output
Print lines. The -th line contains for the -th query. The answer may exceed a 32-bit integer.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; ; .
Examples
Example 1
Input
5 5
1 2 3 4 5
1 1 2 2
1 1 2 3
1 1 2 4
1 2 2 3
1 5 1 5
Output
1
3
6
4
40
Explanation
The first three queries yield respectively , , and . The fourth has the four terms . For the entire array against itself, every pair of distinct indices contributes twice, yielding .