#P8522. [IOI 2021] 地牢游戏

[IOI 2021] 地牢游戏

Background

Abusing the judging system for this problem will result in your account being banned.

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

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

Robert is designing a new computer game. In the game, there is one hero, nn enemies, and n+1n + 1 dungeons. The enemies are numbered from 00 to n1n - 1, and the dungeons are numbered from 00 to nn. Enemy ii (0in10 \le i \le n - 1) is located in dungeon ii, and has strength s[i]s[i]. Dungeon nn has no enemy.

The hero initially enters dungeon xx with initial strength zz. Each time the hero enters dungeon ii (0in10 \le i \le n - 1), they must face enemy ii, and one of the following happens:

  • If the hero's strength is greater than or equal to the enemy ii's strength s[i]s[i], then the hero wins. This increases the hero's strength by s[i]s[i] (s[i]1s[i] \ge 1). In this case, the hero will next enter dungeon w[i]w[i] (w[i]>iw[i] > i).
  • Otherwise, the hero loses. This increases the hero's strength by p[i]p[i] (p[i]1p[i] \ge 1). In this case, the hero will next enter dungeon l[i]l[i].

Note that p[i]p[i] may be less than, equal to, or greater than s[i]s[i], and l[i]l[i] may be less than, equal to, or greater than ii. Regardless of the battle result, enemy ii always stays in dungeon ii and its strength remains s[i]s[i].

When the hero enters dungeon nn, the game ends. It can be seen that no matter what the hero's starting dungeon and initial strength are, the game will always end after a finite number of battles.

Robert wants you to test the game using qq simulations. For each simulation, Robert provides the hero's starting dungeon xx and initial strength zz. You need to output the hero's strength when the game ends for each simulation.

Input Format

You need to implement the following functions:

void init(int n, int[] s, int[] p, int[] w, int[] l)
  • nn: the number of enemies.
  • ss, pp, ww, ll: sequences of length nn. For each ii (0in10 \le i \le n - 1):
    • s[i]s[i] is the strength of enemy ii, and also the amount by which the hero's strength increases after defeating enemy ii.
    • p[i]p[i] is the amount by which the hero's strength increases after being defeated by enemy ii.
    • w[i]w[i] is the index of the next dungeon the hero enters after defeating enemy ii.
    • l[i]l[i] is the index of the next dungeon the hero enters after being defeated by enemy ii.
  • This function is called exactly once, and it is called before any calls to the following simulate function.
int64 simulate(int x, int z)
  • xx: the index of the hero's starting dungeon.
  • zz: the hero's initial strength.
  • Assuming the hero starts in dungeon xx with initial strength zz, the return value is the hero's strength when the game ends in that case.
  • This function is called exactly qq times.
3 2
2 6 9
3 1 2
2 2 3
1 0 1
0 1
2 3
24
25

Hint

For all testdata:

  • 1n4000001 \le n \le 400 \, 000
  • 1q500001 \le q \le 50 \, 000
  • 1s[i],p[i]1071 \le s[i], p[i] \le {10}^7(for all 0in10 \le i \le n - 1
  • 0l[i],w[i]n0 \le l[i], w[i] \le n(for all 0in10 \le i \le n - 1
  • w[i]>iw[i] > i(for all 0in10 \le i \le n - 1
  • 0xn10 \le x \le n - 1
  • 1z1071 \le z \le {10}^7
Subtasks Score Special Constraints
00 Sample.
11 1111 n50000n \le 50 \, 000, q100q \le 100, s[i],p[i]10000s[i], p[i] \le 10 \, 000 (for all 0in10 \le i \le n - 1).
22 2626 s[i]=p[i]s[i] = p[i] (for all 0in10 \le i \le n - 1).
33 1313 n50000n \le 50 \, 000, all enemies have the same strength, i.e., s[i]=s[j]s[i] = s[j] for all 0i,jn10 \le i, j \le n - 1.
44 1212 n50000n \le 50 \, 000, there are at most 55 distinct values among all s[i]s[i].
55 2727 n50000n \le 50 \, 000.
66 1111 No additional constraints.

Translated by ChatGPT 5