#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 nn trees along the road. The ii-th tree grows at coordinate xix_i and has height hih_i. The information about the trees is sorted, satisfying x1<x2<<xn1<xnx_1 < x_2 < \dots < x_{n-1} < x_n.

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 xix_i with height hih_i falls to the right, then there must be no uncut tree at coordinate xjx_j such that xi<xj<xi+hix_i < x_j < x_i + h_i. If the same tree falls to the left, then there must be no uncut tree at coordinate xjx_j such that xihi<xj<xix_i − h_i < x_j < x_i.

Next to the tree at the left boundary x1x_1 and the tree at the right boundary xnx_n, there are important buildings, so a fallen tree is not allowed to lie outside the interval [x1,xn][x_1, x_n]. In other words, if xihi<x1x_i − h_i < x_1, then the tree at coordinate xix_i with height hih_i cannot fall to the left; if xi+hi>xnx_i + h_i > x_n, then the tree at coordinate xix_i with height hih_i cannot fall to the right.

The organization received qq logging requests. Each request consists of two numbers lil_i and rir_i, meaning the applicant wants to cut trees from the lil_i-th to the rir_i-th (inclusive).

While processing a request, it is only allowed to cut trees with indices from lil_i to rir_i. The cut trees may fall into the area to the left of lil_i or to the right of rir_i, but they must not go beyond [x1,xn][x_1, x_n], and they must not touch any other trees outside the range from lil_i to rir_i.

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 nn and qq.

The next nn lines describe the trees. Each line contains two integers xix_i and hih_i. It is guaranteed that x1<x2<<xnx_1 < x_2 < \dots < x_n.

Then follow the descriptions of qq logging requests, one per line. The ii-th line contains two integers lil_i and rir_i.

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 100%100\% 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 n,qn,q\le
11 1515 100100
22 500500
33 50005000
44 55 1000010000
55 1010 100000100000
66 200000200000
77 3030 500000500000

Translated by ChatGPT 5