#P10924. Battlefield Survival Optimization

    ID: 12971 Type: Default 1000ms 256MiB

Battlefield Survival Optimization

Battlefield Survival Optimization

You are given n military units. The i-th unit initially has ai soldiers and a consumption value mi. Every integer minute (starting at minute 1) each unit undergoes an update: its current number of soldiers becomes \(\lfloor\frac{x}{m_i}\rfloor\) where x is the number of soldiers allocated to that unit at that minute. If at any minute, after the update, at least one unit’s soldier count becomes 0, Happybob loses the war. We define the survival time as the last minute index for which every unit still had at least 1 soldier (i.e. the minute immediately before a unit drops to 0).

However, Happybob is clever. Between any two integer minutes (i.e. at non‐integer times) he is allowed to transfer soldiers between units. Formally, he may choose two distinct units i and j and an integer x satisfying 1 ≤ x ≤ ai, and then update the numbers to ai − x and aj + x respectively. There is no limit to how many such transfers may be performed between the integer minute updates.

With optimal transfers, Happybob wants to maximize the survival time of his army. As his trusted strategist, you are to answer q operations. Initially, at minute 0, the units have counts ai and consumption values mi (i = 1,2, …, n). Note: Throughout the problem, all indices i and j are subject to 1 ≤ i, j ≤ n and i ≠ j.

The operations are of three types:

  • Type 1: 1 i x — set the soldier count ai to x (1 ≤ x ≤ 109).
  • Type 2: 2 i x — set the consumption value mi to x (1 ≤ x ≤ 106).
  • Type 3: 3 — output the maximum survival time achievable by optimal transfers. This operation does not change any ai or mi.

How to compute the maximum survival time?

At each integer minute update, before the decay happens, you may redistribute the total number of soldiers arbitrarily among units. In order for every unit to survive the upcoming update, each unit i must be allocated at least mi soldiers (since \(\lfloor\frac{m_i}{m_i}\rfloor = 1\)). Let S denote the total number of soldiers and M = \(\sum_{i=1}^{n} m_i\) be the total minimum required soldiers. Moreover, to maximize the post‐update total, you would allocate extra soldiers in multiples of mi — and it turns out that the optimal strategy is to put all extra soldiers into the unit with the minimum consumption value r = \(\min_{i}(m_i)\) (since additional multiples of r yield extra survivors).

Thus if you have a total of S soldiers at the beginning of a minute, and S ≥ M, the best you can guarantee after the update is:

[ S_{next} = n + \left\lfloor\frac{S - M}{r}\right\rfloor. ]

You then repeat the process with the new total Snext. The survival time is defined as the maximum number T such that you can perform T updates (i.e. T minutes) before the total soldiers fall below M (at which point you cannot allocate enough soldiers to every unit).

Infinite Survival: If all consumption values are 1 (so that M = n and r = 1), the soldier count will never decrease (since \(S_{next} = n + (S - n) = S\)). In this case, output -1 to indicate infinite survival time.


Input Format

The first line contains two integers n and q — the number of units and the number of operations.

The second line contains n integers, the initial soldier counts a1, a2, …, an.

The third line contains n integers, the consumption values m1, m2, …, mn.

Each of the following q lines represents an operation in one of the following formats:

  • 1 i x
  • 2 i x
  • 3

Output Format

For each operation of type 3, output a single line with one integer — the maximum survival time (in minutes) achievable through optimal transfers. If the survival time is infinite, output -1.

inputFormat

The input begins with a line containing two integers n and q separated by a space.

The second line contains n integers representing the initial soldier counts for each unit.

The third line contains n integers representing the consumption values for each unit.

Then follow q lines, each describing an operation in one of the three forms:

  • 1 i x — update ai to x.
  • 2 i x — update mi to x.
  • 3 — query the maximum survival time.

outputFormat

For each operation of type 3, output the maximum survival time (in minutes) on a separate line. If the survival time is infinite, output -1.

sample

2 3
13 10
4 6
3
1 1 5
3
1

1

</p>