A15917 | LuoTianyi and the Function
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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>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)$ .
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)$ .
输入格式
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.
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.
输出格式
Print $q$ lines where $i$ -th line contains one integer — the answer for the $i$ -th query.
输入输出样例
输入 #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
输出 #1
6 6 0 6
输入 #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
输出 #2
4 2 6 4 80
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted