# ATcode E - Takahashi / Tak and Hotels

## Title Description:

E - Gaoqiao Junとホテル / Tak and Hotels

### Problem Statement

NN hotels are located on a straight line. The coordinate of the

ii-th hotel (1≤i≤N)(1≤i≤N) is xixi.

Tak the traveler has the following two personal principles:

• He never travels a distance of more than LL in a single day.
• He never sleeps in the open. That is, he must stay at a hotel at the end of a day.

You are given QQ queries. The jj-th (1≤j≤Q)(1≤j≤Q) query is described by two

distinct integers ajaj and bjbj. For each query, find the minimum number of days

that Tak needs to travel from the ajaj-th hotel to the bjbj-th hotel following his

principles. It is guaranteed that he can always travel from the ajaj-th hotel to

the bjbj-th hotel, in any given input.

### Constraints

• 2≤N≤1052≤N≤105
• 1≤L≤1091≤L≤109
• 1≤Q≤1051≤Q≤105
• 1≤xi<x2<...<xN≤1091≤xi<x2<...<xN≤109
• xi+1−xi≤Lxi+1−xi≤L
• 1≤aj,bj≤N1≤aj,bj≤N
• aj≠bjaj≠bj
• N,L,Q,xi,aj,bjN,L,Q,xi,aj,bj are integers.

### Partial Score

• 200200 points will be awarded for passing the test set satisfying
• N≤103N≤103 and Q≤103Q≤103.

### Input

The input is given from Standard Input in the following format:

```NN
x1x1 x2x2 ...... xNxN
LL
QQ
a1a1 b1b1
a2a2 b2b2
:
aQaQ bQbQ
```

### Output

Print QQ lines. The jj-th line (1≤j≤Q)(1≤j≤Q) should contain the minimum

number of days that Tak needs to travel from the ajaj-th hotel to the bjbj-th hotel.

### Sample Input 1 Copy

Copy

```9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5
```

### Sample Output 1 Copy

Copy

```4
2
1
2
```

For the 11-st query, he can travel from the 11-st hotel to the 88-th hotel in 44 days,

as follows:

• Day 11: Travel from the 11-st hotel to the 22-nd hotel. The distance traveled is 22.
• Day 22: Travel from the 22-nd hotel to the 44-th hotel. The distance traveled is 1010.
• Day 33: Travel from the 44-th hotel to the 77-th hotel. The distance traveled is 66.
• Day 44: Travel from the 77-th hotel to the 88-th hotel. The distance traveled is 1010.

## Core topic:

Give the coordinates of N hotels on the X-axis. A passenger can walk L steps every day. At the end of each day, he can only stay in a hotel,

Q times of inquiry, each time from X -- > y, it will take at least a few days.

## Train of thought:

At the beginning, I used the most violent method, O (Q*N) TLE, and then binary optimization, resulting in the worst case of binary optimization

O(Q*N*logN)，TLE.

If the positive solution is the multiplication processing state dp [i] [j]: the farthest position that can be reached after (1 < < j) days starting from I.

(know about multiplication: ST algorithm and multiplication LCA, but they are only used as a routine, without in-depth understanding).

Then you can O (N*logN preprocessing), and then O (q*logN) query.

## Code implementation:

```#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int N=2e5+100;
using namespace std;
int a[N];
int dp[N][50];
int main()
{
int n;
io;
while(cin>>n)
{
for(int i = 1; i <= n; i++)cin>>a[i];
int L,Q;
cin>>L>>Q;
for(int i = 1; i <= n; i++)
{
int id = upper_bound(a+1,a+1+n,a[i]+L)-a-1;
if(a[i] + L >= a[n])
dp[i][0] = n;
else
dp[i][0] = id;
}

for(int i = 1; i <= 30; i++)
{
for(int j = 1; j <= n; j++)
dp[j][i] = dp[dp[j][i-1]][i-1];
}
while(Q--)
{
int L,R;
cin>>L>>R;
if(R<L)
swap(L,R);
long long ans=0;
for(int i = 30; i >= 0; i--)
{
if(dp[L][i] < R)
{
ans=ans+(1ll<<i);
L = dp[L][i];
}
}
cout<<ans+1<<endl;
}
}
return 0;
}```

The end；

Tags: iOS

Posted on Fri, 08 Nov 2019 12:40:03 -0800 by davidjmorin