A16076 | Exotic Queries
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
AquaMoon gives RiverHamster a sequence of integers $a_1,a_2,\dots,a_n$ , and RiverHamster gives you $q$ queries. Each query is expressed by two integers $l$ and $r$ .
For each query independently, you can take any continuous segment of the sequence and subtract an identical non-negative value from all the numbers of this segment. You can do so multiple (possibly, zero) times. However, you may not choose two intersecting segments which are not included in one another. Your goal is to convert to $0$ all numbers whose initial value was within the range $[l, r]$ . You must do so in the minimum number of operations.
Please note that the queries are independent, the numbers in the array are restored to their initial values between the queries.
Formally, for each query, you are to find the smallest $m$ such that there exists a sequence $\{(x_j,y_j,z_j)\}_{j=1}^{m}$ satisfying the following conditions:
- for any $1 \le j \leq m$ , $z_j \ge 0$ and $1 \le x_j \le y_j \leq n$ (here $[x_j, y_j]$ correspond to the segment of the sequence);
- for any $1 \le j < k \le m$ , it is true that $[x_j,y_j]\subseteq[x_{k},y_{k}]$ , or $[x_k,y_k]\subseteq[x_{j},y_{j}]$ , or $[x_j,y_j]\cap[x_{k},y_{k}]=\varnothing$ ;
- for any $1 \le i \le n$ , such that $l \le a_i \leq r$ , it is true that $$$${\large a_i = \sum\limits_{\substack {1 \le j \le m \\ x_j \le i \le y_j}} z_j. } $$$$
For each query independently, you can take any continuous segment of the sequence and subtract an identical non-negative value from all the numbers of this segment. You can do so multiple (possibly, zero) times. However, you may not choose two intersecting segments which are not included in one another. Your goal is to convert to $0$ all numbers whose initial value was within the range $[l, r]$ . You must do so in the minimum number of operations.
Please note that the queries are independent, the numbers in the array are restored to their initial values between the queries.
Formally, for each query, you are to find the smallest $m$ such that there exists a sequence $\{(x_j,y_j,z_j)\}_{j=1}^{m}$ satisfying the following conditions:
- for any $1 \le j \leq m$ , $z_j \ge 0$ and $1 \le x_j \le y_j \leq n$ (here $[x_j, y_j]$ correspond to the segment of the sequence);
- for any $1 \le j < k \le m$ , it is true that $[x_j,y_j]\subseteq[x_{k},y_{k}]$ , or $[x_k,y_k]\subseteq[x_{j},y_{j}]$ , or $[x_j,y_j]\cap[x_{k},y_{k}]=\varnothing$ ;
- for any $1 \le i \le n$ , such that $l \le a_i \leq r$ , it is true that $$$${\large a_i = \sum\limits_{\substack {1 \le j \le m \\ x_j \le i \le y_j}} z_j. } $$$$
输入格式
The first line contains two integers $n$ and $q$ ( $1\le n,q\le 10^6$ ).
The second line contains $n$ integers integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le n$ ).
Each of the next $q$ lines contains two integers $l$ and $r$ ( $1\le l\le r\le n$ ), representing a query.
The second line contains $n$ integers integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le n$ ).
Each of the next $q$ lines contains two integers $l$ and $r$ ( $1\le l\le r\le n$ ), representing a query.
输出格式
For each query, output the answer for this query on a separate line.
输入输出样例
输入 #1
10 8 1 6 2 3 2 6 3 10 1 2 1 10 2 2 3 3 2 3 1 3 3 6 4 6 5 5
输出 #1
8 1 1 3 5 3 1 0
输入 #2
3 1 1 3 2 1 3
输出 #2
3
In the first test case, consider the second query, when $l = 2$ , $r = 2$ . The elements to be manipulated are $[a_3, a_5, a_{10}] = [2, 2, 2]$ . It is sufficient to apply the operation sequence $\{(2, 10, 2)\}$ .
Consider the fourth query, when $l = 2$ , $r = 3$ . The elements to be manipulated are $[a_3, a_4, a_5, a_7, a_{10}] = [2, 3, 2, 3, 2]$ . It is sufficient to apply the operation sequence $\{(1, 10, 2), (4, 4, 1), (7, 7, 1)\}$ .
In the second test case, note that the operation sequence $\{(1, 2, 1), (2, 3, 2)\}$ is invalid because the two segments intersect but neither is contained inside the other.
Consider the fourth query, when $l = 2$ , $r = 3$ . The elements to be manipulated are $[a_3, a_4, a_5, a_7, a_{10}] = [2, 3, 2, 3, 2]$ . It is sufficient to apply the operation sequence $\{(1, 10, 2), (4, 4, 1), (7, 7, 1)\}$ .
In the second test case, note that the operation sequence $\{(1, 2, 1), (2, 3, 2)\}$ is invalid because the two segments intersect but neither is contained inside the other.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted