Three Subarrays

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 3.0s
Memory limit: 512M

Problem type

Given a sequence A with n positive integers, a1, a2, , an, and q queries. Each query specifies three subarrays [L1,R1], [L2,R2], and [L3,R3]. Bob wants to find the value

len(L1,R1)+len(L2,R2)+len(L3,R3)3×|a[L1:R1]a[L2:R2]a[L3:R3]|

where len(Li,Ri) is the length of the subarray [Li,Ri] and |a[L1:R1]a[L2:R2]a[L3:R3]| represents the number of shared common integers in all three subarrays.

For example, if the 1st subarray is [1,2,2,3,3], the 2nd subarray is [3,2,3,1,1], and the 3rd subarray is [1,3,2,2,3], the shared common integers are (1,2,3,3), and thus the value Bob wants to find is 5+5+53×4=3.

Input Specification

The first line of input contains two integers n and q (1n,q105), indicating the length of the sequence and the number of queries.

The second line of input contains n integers ai (1ai109).

Each of the following q lines contains six integers, L1, R1, L2, R2, L3, R3 (1LiRin).

Output Specification

Output one integer per line for each query.

Constraints

Subtask Points Additional constraints
1 30 n,q2000.
2 70 No additional constraints.

Sample Input

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

Sample Output

Copy
3
0

Comments

There are no comments at the moment.