#P10924. Happybob's Game (UBC001D)

Happybob's Game (UBC001D)

Problem Description

Note: In this problem, all i,ji, j are assumed to satisfy 1i,jn1 \le i, j \le n and iji \not = j.

Happybob's game character is fighting a war.

He has nn armies. The ii-th army has aia_i soldiers and a consumption value mim_i, meaning that every 11 minute, the number of soldiers in the ii-th army becomes aimi⌊\frac{a_i}{m_i}⌋. Once the number of soldiers in any army becomes 00, Happybob loses. Let the beginning be minute 00. Define the survival time as the minute right before one of Happybob's armies becomes 00. You may refer to the sample explanation.

However, Happybob is not willing to be wiped out like this. He comes up with a good idea: at any time that is not an integer minute, he can transfer some soldiers from one army to another. Formally, choose two armies ai,aja_i, a_j and a transfer amount xx satisfying 1xai1 \le x \le a_i, and change ai,aja_i, a_j into aix,aj+xa_i - x, a_j + x respectively. Note that between two integer minutes, he can perform transfers any number of times.

Happybob finds that this can effectively increase the armies' survival time. Now, as Happybob's most trusted chief of staff, you need to help him deploy troops.

Next, you are given qq operations. Each operation is one of the following:

  • Operation 11, in the form 1 i x, means the number of soldiers aia_i of Happybob's ii-th army becomes xx (1x1091 \le x \le 10^9).
  • Operation 22, in the form 2 i x, means the consumption value mim_i of Happybob's ii-th army becomes xx (1x1061 \le x \le 10^6).
  • Operation 33, in the form 3, means you should output the maximum survival time that Happybob can theoretically achieve using the troop-transfer method above. Note that this operation does not change the values of aia_i or mim_i.

Input Format

The first line contains two positive integers n,qn, q.

The next two lines each contain nn positive integers, representing the array aa and the array mm.

The next qq lines each describe one of the three operations above.

Output Format

Suppose there are optopt operations of type 33. Then your output should contain optopt lines, each line giving the maximum possible survival time under the current state. If Happybob's armies can survive forever, output -1.

2 10
29 5
7 2
1 2 3
2 1 3
3
1 1 1
1 2 1
2 1 1
2 2 1
3
2 1 2
3
3
-1
0

Hint

Sample Explanation

For the first operation 33, Happybob can move troops like this:

Time (min) Soldiers in the first army Soldiers in the second army
00 2929 33
0.50.5 1010 2222
11 33 1111
1.51.5 66 88
22 44
2.52.5 33
33 11
3.53.5
44 00

Constraints

For 100%100\% of the testdata, 1n5×1061 \le n \le 5 \times 10^6, 1q1051 \le q \le 10^5. It is guaranteed that for all ai,mia_i, m_i, 1ai1091 \le a_i \le 10^9, 1mi1061 \le m_i \le 10^6.

Translated by ChatGPT 5