luogu#P16391. [IATI 2024] Game

[IATI 2024] Game

Problem Description

Klimi and Nikol have an integer array AA with NN cells numbered from 00 to N1N-1 and containing different\textbf{different} integer values. The girls are playing a game with a single piece that is moved around the array. Initially, the piece is located in some cell. One move of the game proceeds as follows:

  • The player whose turn it is takes the piece and moves it to another cell that is at distance at most DD from the current one. Formally, if the piece is currently in cell xx (0x<N0\leq x< N), then the player is allowed to move the piece to cell yy (0y<N0\leq y<N) if yxD|y-x|\leq D and xyx\neq y. Note that this means the player must\textbf{must} move the piece.
  • After moving the piece to some cell yy, the player adds AyA_y to her score. Note that the score is added after\textbf{after} moving the piece.

The moves alternate between the two girls and Klimi plays first. The game ends in exactly 1010010^{100} turns, at which point the girl whose total score is higher wins. If their score is equal, then Klimi wins.

The girls aren't quite happy with the time it takes to finish even a single game, so they ask you to just figure out the optimal play results in different scenarios.

Formally, you will be given the initial array AA and the value DD, and then you will have to process QQ queries of two types:

  • Updates: A single value in the array is changed
  • Questions: You are asked whether Klimi (the one to play first) wins the game, given some initial position of the piece and assuming optimal play.

The updates persist across queries, so each question must be answered considering all updates that have happened so far.

The implementation details section describes the interfaces you should support in more detail.

Implementation details

You must implement three functions. Your first function init\verb|init| must have the following prototype:

void init(std::vector<int> A, int D)

It will be called once in the beginning of the test and will supply you with the array AA and the value DD.

Your other two functions updateValue\verb|updateValue| and isWinning\verb|isWinning| must have the following prototypes:

void updateValue(int index, int newValue)
bool isWinning(int startIndex)

The total number of calls to either of the two functions will be QQ (refer to the constraints section) and all calls will be done after the call to init\verb|init|.

The updateValue\verb|updateValue| function must process an update query and set AindexA_{index} to newValue\verb|newValue|. It is guaranteed that after each update all values in AA are still different.

The isWinning\verb|isWinning| function must process a question query and return true\verb|true| if Klimi can win the game on the current array, given that the piece starts in cell startIndex\verb|startIndex|. It must return false\verb|false| otherwise.

Your program must implement the three functions, but should not contain a function main\verb|main|. Also, it must not read from the standard input or print to the standard output. Your program must also include the header file game.h\verb|game.h| by an instruction to the preprocessor: #include "game.h"\verb|#include "game.h"|

As long as it respects these conditions, your program can contain any helper functions, variables, constants, and so on.

Local testing

You are given the files Lgrader.cpp\verb|Lgrader.cpp| and game.h\verb|game.h|, which you can compile together with your code to test it.

Input Format

The input format of the grader is:

  • NN DD
  • A0A_0 A1A_1 ... AN1A_{N-1}
  • QQ
  • <QQ lines of queries>

Where the format of the queries is:

  • 11 ind\verb|ind| val\verb|val| --- Update query setting Aind=valA_{\mathrm{ind}}=\mathrm{val}
  • 22 ind\verb|ind| --- Question query for the piece starting in ind\verb|ind|

Output Format

For each query of type 22, the grader will print 00 if your function returned false\verb|false| and 11 if your function returned true\verb|true|

6 2
1 7 4 9 30 2
5
2 0
2 1
1 4 8
2 0
2 1
1
0
0
1

Hint

Example

Suppose A=[1,7,4,9,30,2]A = [1, 7, 4, 9, 30, 2], D=2D = 2, and Q=5Q = 5. An example sequence of calls is:

  • Call to init({1, 7, 4, 9, 30, 2}, 2)\verb|init({1, 7, 4, 9, 30, 2}, 2)|
  • Call to isWinning(0)\verb|isWinning(0)| which returns true\verb|true|
  • Call to isWinning(1)\verb|isWinning(1)| which returns false\verb|false|
  • Call to updateValue(4, 8)\verb|updateValue(4, 8)|
  • Call to isWinning(0)\verb|isWinning(0)| which returns false\verb|false|
  • Call to isWinning(1)\verb|isWinning(1)| which returns true\verb|true|

The array after the update call is [1,7,4,9,8,2][1, 7, 4, 9, 8, 2]. It can be shown that those are the correct answers to the questions if both girls play optimally.

Constraints

  • 1N,Q21051 \leq N, Q \leq 2\cdot 10^5
  • 1D251 \leq D \leq 25
  • 1Ai1091 \leq A_i \leq 10^9
  • 0index,startIndex<N0 \leq \mathrm{index}, \mathrm{startIndex} < N
  • 1newValue1091 \leq \mathrm{newValue} \leq 10^9
  • The values in AA are all different at all times, including after updates.

Subtasks

Subtask Points N,QN, Q DD Extra Constraints
11 88 N,Q10N, Q \leq 10 D25D \leq 25
22 1818 N,Q2103N, Q \leq 2\cdot 10^3 There are no update queries
33 1616 N,Q2105N, Q \leq 2\cdot 10^5
44 2727 N,Q105N, Q \leq 10^5 D10D \leq 10
55 3131 N,Q2105N, Q \leq 2\cdot 10^5 D25D \leq 25

You must pass all tests in a subtask to get the points.