#P6829. [IOI 2020] 植物比较

[IOI 2020] 植物比较

Description

Botanist Hazel visited a special exhibition at the Singapore Botanic Gardens. In this exhibition, there are nn plants with pairwise distinct heights, arranged in a circle. The plants are numbered clockwise from 00 to n1n-1, and plant n1n-1 is adjacent to plant 00.

For each plant i (0in1)i\ (0 \le i \le n-1), Hazel compares it with the next k1k-1 plants in the clockwise direction, and records a value r[i]r[i], which indicates how many of those k1k-1 plants are taller than plant ii. Therefore, each value r[i]r[i] is determined by the relative heights within some consecutive block of kk plants.

For example, suppose n=5n=5, k=3k=3, and i=3i=3. The next k1=2k-1=2 plants clockwise from plant 33 are plants 44 and 00. If plant 44 is taller than plant 33, and plant 00 is shorter than plant 33, then Hazel records r[3]=1r[3]=1.

You may assume that all the values r[i]r[i] recorded by Hazel are correct. That is, there exists at least one set of pairwise distinct heights that matches Hazel’s recorded values.

In this problem, you need to compare the heights of qq pairs of plants. Since you did not get to visit the exhibition, the only information you have is Hazel’s notebook, which contains kk and the sequence r[0],,r[n1]r[0],\ldots, r[n-1].

For each pair of plants xx and yy to be compared (with xyx \ne y), determine which of the following cases applies:

  • Plant xx is definitely taller than plant yy: for any set of pairwise distinct heights h[0],,h[n1]h[0],\ldots,h[n-1] that matches array rr, we have h[x]>h[y]h[x] > h[y].
  • Plant xx is definitely shorter than plant yy: for any set of pairwise distinct heights h[0],,h[n1]h[0],\ldots,h[n-1] that matches array rr, we have h[x]<h[y]h[x] < h[y].
  • The comparison is inconclusive: neither of the above two cases holds.

Implementation Details

You are required to implement the following functions:

void init(int k, std::vector<int> r)
  • kk: the number of consecutive plants that determine each value r[i]r[i].
  • rr: an array of size nn, where r[i]r[i] is the number of plants among the next k1k-1 plants clockwise from plant ii that are taller than it.
  • This function is called exactly once, before any call to compare_plants.
int compare_plants(int x, int y)
  • x,yx,y: the indices of the plants to be compared.
  • This function should return:
    • 11, if plant xx is definitely taller than plant yy,
    • 1-1, if plant xx is definitely shorter than plant yy,
    • 00, if the comparison is inconclusive.
  • This function is called exactly qq times.

Hint

Sample Explanation

Example 1

Consider the following call:

init(3, [0, 1, 1, 2])

Suppose the grader calls compare_plants(0, 2). From r[0]=0r[0]=0, we can conclude that plant 22 is not taller than plant 00, so this call should return 11.

Suppose the grader then calls compare_plants(1, 2). Since for every set of plant heights satisfying the above conditions, plant 11 is shorter than plant 22, this call should return 1-1.

Example 2

Consider the following call:

init(2, [0, 1, 0, 1])

Suppose the grader calls compare_plants(0, 3). From r[3]=1r[3]=1, we can conclude that plant 00 is taller than plant 33, so this call should return 11.

Suppose the grader then calls compare_plants(1, 3). Two height assignments [3,1,4,2][3,1,4,2] and [3,2,4,1][3,2,4,1] both match Hazel’s observations. Since in the first case plant 11 is shorter than plant 33, while in the second case it is taller than plant 33, this call should return 00.

Constraints

  • 2kn200 0002\le k\le n\le 200\ 000.
  • 1q200 0001\le q\le 200\ 000.
  • 0r[i]k10 \le r[i]\le k-1 (for all 0in10 \le i \le n-1).
  • 0x<yn10\le x<y\le n-1.
  • There exists one or more sets of pairwise distinct heights that match the array rr.

Subtasks

  1. (5 points) k=2k=2.
  2. (14 points) n5000,2k>nn \le 5000, 2 \cdot k > n.
  3. (13 points) 2k>n2 \cdot k > n.
  4. (17 points) The correct answer to each compare_plants call is 11 or 1-1.
  5. (11 points) n300,qn(n1)2n\le 300, q\le \frac{n\cdot (n-1)}{2}.
  6. (15 points) In each compare_plants call, x=0x=0.
  7. (25 points) No additional constraints.

Example Grader

The example grader reads the input in the following format:

Line 11: n k qn\ k\ q
Line 22: r[0] r[1]  r[n1]r[0]\ r[1]\ \cdots\ r[n-1]
Line 3+i (0iq1)3+i\ (0\le i\le q-1): x yx\ y, representing the parameters of the ii-th call to compare_plants

The example grader prints your answers in the following format:

Line 1+i (0iq1)1+i\ (0\le i\le q-1): the return value of the ii-th call to compare_plants

Translated by ChatGPT 5