#P8492. [IOI 2022] 无线电信号塔

[IOI 2022] 无线电信号塔

Background

Due to technical limitations, please do not use C++ 14 (GCC 9) to submit this problem.

This is an interactive problem. You only need to implement the functions required in the code.

Your code does not need to include any additional header files, and you do not need to implement the main function.

Problem Description

There are NN radio signal towers in Jakarta. These towers are arranged in a straight line and are numbered from 00 to N1N - 1 from left to right.
For each ii satisfying 0iN10 \le i \le N - 1, tower ii has height HiH_i meters.
All tower heights are distinct.

For a positive signal interference parameter δ\delta, a pair of towers ii and jj (0i<jN10 \le i \lt j \le N - 1) can communicate with each other if and only if there exists an intermediate tower kk such that:

  • Tower ii is to the left of tower kk, and tower jj is to the right of tower kk, i.e., i<k<ji \lt k \lt j;
  • The heights of towers ii and jj are both at most H[k]δH[k] - \delta meters.

Pak Dengklek wants to rent some towers to build his new radio network.
Your task is to answer QQ queries from Pak Dengklek. Each query is as follows:
Given LL, RR, and DD (0LRN10 \le L \le R \le N - 1 and D>0D > 0), determine the maximum number of towers Pak Dengklek can rent, subject to all of the following conditions:

  • Pak Dengklek can only rent towers with indices between LL and RR (inclusive);
  • The signal interference parameter δ\delta equals DD;
  • Every pair of rented towers must be able to communicate with each other.

Note that regardless of whether the intermediate tower kk is rented, two rented towers can still use tower kk to communicate.

Input Format

You need to implement the following functions:

void init(int N, int[] H)
  • NN: the number of towers.
  • HH: an array of length NN giving the heights of the towers.
  • This function is called exactly once, and before any calls to max_towers.
int max_towers(int L, int R, int D)
  • LL, RR: the boundaries of the tower index range.
  • DD: the value of the signal interference parameter δ\delta.
  • This function should return the maximum number of towers Pak Dengklek can rent (to build the radio network), where Pak Dengklek can only rent towers between LL and RR (inclusive), and the signal interference parameter δ\delta equals DD.
  • This function will be called exactly QQ times.

Output Format

Consider the following sequence of function calls:

init(7, [10, 20, 60, 40, 50, 30, 70])
max_towers(1, 5, 10)

Pak Dengklek can rent towers 11, 33, and 55.
The diagram below illustrates this example, where the gray trapezoids represent the rented towers.

Towers 33 and 55 can communicate using tower 44, because 40501040 \le 50 - 10 and 30501030 \le 50 - 10.
Towers 11 and 33 can communicate using tower 22.
Towers 11 and 55 can communicate using tower 33.
It is impossible to rent more than 33 towers, so the function should return 33.

max_towers(2, 2, 100)

There is only 11 tower in this range, so Pak Dengklek can only rent 11 tower.
Therefore, the function should return 11.

max_towers(0, 6, 17)

Pak Dengklek can rent towers 11 and 33.
Towers 11 and 33 can communicate using tower 22, because 20601720 \le 60 - 17 and 40601740 \le 60 - 17.
It is impossible to rent more than 22 towers, so the function should return 22.

Hint

Constraints

  • 1N100  0001 \le N \le 100\;000
  • 1Q100  0001 \le Q \le 100\;000
  • 1Hi1091 \le H_i \le 10^9 (for all ii satisfying 0iN10 \le i \le N - 1)
  • HiHjH_i \ne H_j (for all ii and jj satisfying 0i<jN10 \le i \lt j \le N - 1)
  • 0LRN10 \le L \le R \le N - 1
  • 1D1091 \le D \le 10^9

Subtasks

  1. (4 points) There exists a tower kk (0kN10 \le k \le N - 1) satisfying all of the following:
    • For each ii with 0ik10 \le i \le k - 1: Hi<Hi+1H_i \lt H_{i + 1}
    • For each ii with kiN2k \le i \le N - 2: Hi>Hi+1H_i \gt H_{i + 1}
  2. (11 points) Q=1Q = 1, N2000N \le 2000
  3. (12 points) Q=1Q = 1
  4. (14 points) D=1D = 1
  5. (17 points) L=0L = 0, R=N1R = N - 1
  6. (19 points) The value of DD is the same in all calls to max_towers
  7. (23 points) No additional constraints

Sample grader

The sample grader reads input in the following format:

  • Line 11: N  QN \; Q
  • Line 22: H0  H1    HN1H_{0} \; H_{1} \; \ldots \; H_{N - 1}
  • Line 3+j3 + j (0jQ10 \le j \le Q - 1): L  R  DL \; R \; D (corresponding to the jj-th query)

The sample grader prints your answers in the following format:

  • Line 1+j1 + j (0jQ10 \le j \le Q - 1): the return value of max_towers for the jj-th query

Notes

In the statement, the function interfaces use general type names void, int, int64, int[] (array), and int[][] (array of arrays).

In C++, the grader will use appropriate data types or implementations, as shown in the tables below:

void int int64 int[]
void int long long std::vector<int>
int[][] The length of array a
std::vector<std::vector<int>> a.size()

Translated by ChatGPT 5