Codeforces Round 872 (Div. 1) |
---|

Finished |

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

data structures

*3000

No tag edit access

The problem statement has recently been changed. View the changes.

×
D. LuoTianyi and the Function

time limit per test

7 secondsmemory limit per test

1024 megabytesinput

standard inputoutput

standard outputLuoTianyi gives you an array $$$a$$$ of $$$n$$$ integers and the index begins from $$$1$$$.

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

- $$$g(i,j)$$$ is the largest integer $$$x$$$ that satisfies $$$\{a_p:i\le p\le j\}\subseteq\{a_q:x\le q\le j\}$$$ while $$$i \le j$$$;
- and $$$g(i,j)=0$$$ while $$$i>j$$$.

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

Input

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

The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\le a_i\le n$$$) — the elements of the array $$$a$$$.

Next $$$q$$$ lines describe a query. The $$$i$$$-th line contains four integers $$$l,r,x,y$$$ ($$$1\le l\le r\le n, 1\le x\le y\le n$$$) — the integers in the $$$i$$$-th query.

Output

Print $$$q$$$ lines where $$$i$$$-th line contains one integer — the answer for the $$$i$$$-th query.

Examples

Input

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

Output

6 6 0 6

Input

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

Output

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=6$$$.

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

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

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

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

In the second example:

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

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

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/30/2023 19:42:38 (j2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|