#P11094. [ROI 2021] 砍树 (Day 2)
[ROI 2021] 砍树 (Day 2)
Problem Description
Translated from ROI 2021 D2T3.
The “2D” organization is responsible for issuing logging permits. They received logging requests along a road.
There are trees along the road. The -th tree grows at coordinate and has height . The information about the trees is sorted, satisfying .
Trees can be cut one by one as follows: cut a tree and make it fall to the left or to the right. To prevent trees from being damaged while falling, it must not touch any uncut tree within a distance less than its own height. In other words, if the tree at coordinate with height falls to the right, then there must be no uncut tree at coordinate such that . If the same tree falls to the left, then there must be no uncut tree at coordinate such that .
Next to the tree at the left boundary and the tree at the right boundary , there are important buildings, so a fallen tree is not allowed to lie outside the interval . In other words, if , then the tree at coordinate with height cannot fall to the left; if , then the tree at coordinate with height cannot fall to the right.
The organization received logging requests. Each request consists of two numbers and , meaning the applicant wants to cut trees from the -th to the -th (inclusive).
While processing a request, it is only allowed to cut trees with indices from to . The cut trees may fall into the area to the left of or to the right of , but they must not go beyond , and they must not touch any other trees outside the range from to .
For each request, compute the maximum number of trees that can be cut without damaging any tree. Each request should be processed independently (the trees are not actually cut).
Input Format
The first line contains two integers and .
The next lines describe the trees. Each line contains two integers and . It is guaranteed that .
Then follow the descriptions of logging requests, one per line. The -th line contains two integers and .
Output Format
For each request, output the maximum number of trees that can be cut.
3 3
1 3
3 1
4 2
1 1
2 3
1 3
0
2
3
5 3
1 5
3 1
4 2
5 3
6 1
1 5
5 5
1 1
5
1
0
1 1
100 100
1 1
0
Hint
The last query of Sample 1 can be cut as follows:

For of the testdata, $1 \le n, q \le 500000,1 \le x_i, h_i \le 10^9,1 \le l_i \le r_i \le n$.
| Subtask | Score | |
|---|---|---|
Translated by ChatGPT 5