Codeforces Round 872 (Div. 1)
D
LuoTianyi and the Function
Codeforces
3000
原题
← CE →
翻译中…

LuoTianyi 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 \gt 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.

翻译中…

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 \gt 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$.

翻译中…

样例

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
Output #1
6
6
0
6
InputCopy OutputCopy
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
Output #2
4
2
6
4
80
InputCopy OutputCopy
题目信息
题号
1824D
时间限制
7 s
内存限制
1024 MB
标签
数据结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。