luogu#P16330. Just Because!

    ID: 16153 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>贪心倍增洛谷原创O2优化洛谷月赛

Just Because!

Problem Description

You are given nn trees. The ii-th tree is located at position pip_i and has height hih_i. It is guaranteed that the sequence pp is strictly increasing.

There are qq queries. For the ii-th query, only the subarray [li,ri][l_i, r_i] is kept. You need to choose as many trees as possible such that there exists a way to cut them down so that no tree trunk hits another trunk.

Formally, let $S=\{s_1,s_2,\dots,s_k\}\subseteq\{l_i,l_i+1,\dots,r_i\}$ with s1<s2<<sks_1<s_2<\cdots<s_k.

The requirement is that for every 1<j<k1<j<k, we have $p_{s_j}+h_{s_j}<p_{s_{j+1}} \lor p_{s_j}-h_{s_j}>p_{s_{j-1}}.$

Find the maximum possible value of kk.

Note that you don't actually cut any trees, and when you compute the answer, the trees' positions and heights remain unchanged.

Input Format

The first line contains two positive integers n,qn,q.

The second line contains nn positive integers, denoting hih_i.

The third line contains a strictly increasing sequence of nn positive integers pip_i.

The next qq lines each contain two positive integers li,ril_i,r_i, describing the query interval.

Output Format

Output qq lines. Each line should contain one integer, the answer for the corresponding query.

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

2
2
2

17 16
7 8 15 20 24 27 30 37 40 44 48 52 56 60 64 68 72 
5 1 1 4 1 2 1 2 3 2 2 5 7 4 7 6 7 
2 12
10 12
4 14
4 8
3 3
3 16
3 13
1 16
3 15
15 16
1 15
3 16
2 14
5 16
4 17
4 14

11
3
10
5
1
12
10
14
12
2
14
12
12
10
12
10

Hint

Constraints

For all test cases, 1n,q5×1051\le n,q\le 5\times 10^5, 1pi,hi1091\le p_i,h_i\le 10^9, and liril_i\le r_i. For all 1i<n1\le i<n, it is guaranteed that pi<pi+1p_i<p_{i+1}.

Subtask nn\le qq\le Special Property Score
11 1818 None 1010
22 300300 2020
33 30003000
44 10510^5 Yes 1010
55 5×1055\times 10^5 None 4040
  • Special property: For all 1iq1\le i\le q, it is guaranteed that li=1l_i=1.