#P1824D. LuoTianyi and the Function

LuoTianyi and the Function

No submission language available for this problem.

Description

LuoTianyi gives you an array aa of nn integers and the index begins from 11.

Define g(i,j)g(i,j) as follows:

  • g(i,j)g(i,j) is the largest integer xx that satisfies {ap:ipj}{aq:xqj}\{a_p:i\le p\le j\}\subseteq\{a_q:x\le q\le j\} while iji \le j;
  • and g(i,j)=0g(i,j)=0 while i>ji>j.

There are qq queries. For each query you are given four integers l,r,x,yl,r,x,y, you need to calculate i=lrj=xyg(i,j)\sum\limits_{i=l}^{r}\sum\limits_{j=x}^{y}g(i,j).

The first line contains two integers nn and qq (1n,q1061\le n,q\le 10^6) — the length of the array aa and the number of queries.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ain1\le a_i\le n) — the elements of the array aa.

Next qq lines describe a query. The ii-th line contains four integers l,r,x,yl,r,x,y (1lrn,1xyn1\le l\le r\le n, 1\le x\le y\le n) — the integers in the ii-th query.

Print qq lines where ii-th line contains one integer — the answer for the ii-th query.

Input

The first line contains two integers nn and qq (1n,q1061\le n,q\le 10^6) — the length of the array aa and the number of queries.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ain1\le a_i\le n) — the elements of the array aa.

Next qq lines describe a query. The ii-th line contains four integers l,r,x,yl,r,x,y (1lrn,1xyn1\le l\le r\le n, 1\le x\le y\le n) — the integers in the ii-th query.

Output

Print qq lines where ii-th line contains one integer — the answer for the ii-th query.

Sample Input 1

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

Sample Output 1

6
6
0
6

Sample Input 2

10 5
10 2 8 10 9 8 2 1 1 8
1 1 10 10
2 2 3 3
6 6 6 6
1 1 4 5
4 8 4 8

Sample Output 2

4
2
6
4
80

Note

In the first example:

In the first query, the answer is g(1,4)+g(1,5)=3+3=6g(1,4)+g(1,5)=3+3=6.

x=1,2,3x=1,2,3 can satisfies {ap:1p4}{aq:xq4}\{a_p:1\le p\le 4\}\subseteq\{a_q:x\le q\le 4\}, 33 is the largest integer so g(1,4)=3g(1,4)=3.

In the second query, the answer is g(2,3)+g(3,3)=3+3=6g(2,3)+g(3,3)=3+3=6.

In the third query, the answer is 00, because all i>ji > j and g(i,j)=0g(i,j)=0.

In the fourth query, the answer is g(6,6)=6g(6,6)=6.

In the second example:

In the second query, the answer is g(2,3)=2g(2,3)=2.

In the fourth query, the answer is g(1,4)+g(1,5)=2+2=4g(1,4)+g(1,5)=2+2=4.