#P15449. [IOI 2011] Elephants

[IOI 2011] Elephants

Description

Dancing Elephants is a spectacular show in Pattaya that features NN elephants dancing on a line, known as the stage.

After years of training, elephants in this show are capable of many amazing dances. The show consists of a series of acts. In each act, exactly one elephant performs a cute dance while possibly moving to a different position.

The show producers want to produce a photo book that contains pictures of the entire show. After each act, they want to take pictures of all elephants as seen by the spectators.

At any time during the show, multiple elephants may share the same position. In that case, they simply stand behind one another at the same position.

A single camera can take a picture of a group of elephants if and only if all their positions lie on some segment of length LL (including both its endpoints). As the elephants can spread out across the stage, multiple cameras may be needed in order to take simultaneous snapshots of all the elephants.

As an example, suppose that L=10L=10 and that the elephants are at positions 1010, 1515, 1717, and 2020 on the stage. At this moment, a single camera can take their picture, as shown below. (Elephants are shown as triangles; cameras are shown as trapezoids.)

In the following act, the elephant at position 1515 dances to position 3232. After this act, we need at least two cameras to take the snapshot.

In the next act, the elephant at position 1010 moves to position 77. For the new arrangement of elephants, we need three cameras to photograph all of them.

In this interactive task, you have to determine the minimum number of cameras needed to take the pictures after each of the acts. Note that the number of cameras needed may increase, decrease, or stay the same between acts.

Write the following procedures:

  • Procedure init(N,L,X) that takes the following parameters:
    • NN - the number of elephants. The elephants are numbered 00 through N1N-1.
    • LL - the length of the segment captured by a single camera. You may assume that LL is an integer such that 0L1090 \le L \le 10^9.
    • XX - a one-dimensional array of integers representing the initial positions of the elephants. For 0i<N0 \le i < N, elephant ii starts at the position X[i]X[i]. The initial positions are in sorted order. More precisely, you may assume that 0X[0]X[N1]1090 \le X[0] \le \ldots \le X[N-1] \le 10^9. Note that during the dance the elephants may reorder themselves.
    • This procedure will be called only once, prior to all calls to update. It does not return any value.
  • Procedure update(i,y) that takes the following parameters:
    • ii - the number of the elephant that moves in the current act.
    • yy - the position where the elephant ii will stand after the current act. You may assume that yy is an integer such that 0y1090 \le y \le 10^9.
    • This procedure will be called multiple times. Each call corresponds to a single act (which follows on from all of the previous acts). Each call must return the minimum number of cameras needed to photograph all elephants after the corresponding act.

Consider the case where N=4N=4, L=10L=10, and the initial positions of the elephants are X={10,15,17,20}X=\{10,15,17,20\}.

First, your procedure init will be called with these parameters. Afterwards, your procedure update will be called once for each act. Here is an example sequence of calls and their correct return values:

act call parameters return value
11 update(2,16) 11
22 update(1,25) 22
33 update(3,35)
44 update(0,38)
55 update(2,0) 33

Input Format

The sample grader reads the input in the following format:

  • Line 11: NN, LL, and MM, where MM is the number of acts in the show.
  • Lines 22 to N+1N+1: the initial positions; i.e., line k+2k+2 contains X[k]X[k] for 0k<N0 \le k < N.
  • Lines N+2N+2 to N+M+1N+M+1: information on MM acts; i.e. line N+1+jN+1+j contains i[j]i[j], y[j]y[j], and s[j]s[j], separated by a space, denoting that in the jj-th act elephant i[j]i[j] moves to position y[j]y[j], and after that act, s[j]s[j] is the mininal number of cameras needed, for 1jM1 \le j \le M.

Output Format

Sample grader output: For this task, each one of these files should contain precisely the text Correct.

Hint

Subtask 1 (10 points)

  • There are exactly N=2N = 2 elephants.
  • Initially, and after each act, the positions of all elephants will be distinct.
  • Your procedure update will be called at most 100100 times.

Subtask 2 (16 points)

  • 1N1001 \le N \le 100.
  • Initially, and after each act, the positions of all elephants will be distinct.
  • Your procedure update will be called at most 100100 times.

Subtask 3 (24 points)

  • 1 \le N \le 5×1045\times10^4.
  • Initially, and after each act, the positions of all elephants will be distinct.
  • Your procedure update will be called at most 5×1045\times10^4 times.

Subtask 4 (47 points)

  • 1N7×1041 \le N \le 7\times10^4.
  • Elephants may share the same position.
  • Your procedure update will be called at most 7×1047\times10^4 times.

Subtask 5 (3 points)

  • 1N1.5×1051 \le N \le 1.5\times10^5.
  • Elephants may share the same position.
  • Your procedure update will be called at most 1.5×1051.5\times10^5 times.
  • Please see the note about the CPU time limit under the Implementation Details Section.

Hint

  • Note: The collection templates in the C++ Standard Library (STL) can be slow; in particular, it might not be possible to solve subtask 5 if you use them.