#P9530. [JOIST 2022] 鱼 2 / Fish 2

[JOIST 2022] 鱼 2 / Fish 2

Background

JOISC2022 D4T2

Problem Description

JOI has NN fish, numbered 1,2,,N1,2,\dots,N. The size of fish ii (1iN)(1 \le i \le N) is AiA_i.

When raising fish, we need to pay attention to the following fact: if two fish are close to each other, then as time passes, one of them may eat the other. Two fish are close to each other if and only if there is no fish between them.
More specifically, if fish xx has size not less than fish yy, and fish x,yx,y are close to each other, then xx can eat yy, and the size of xx becomes the sum of the original sizes of xx and yy. If xx and yy have the same size, then either xx eats yy or yy eats xx may happen.

JOI will raise fish for QQ days. To pass the time, he will do the following thought experiment. On day jj (1jQ)(1 \le j \le Q), JOI will perform one of the following actions:

  • Type 1: JOI feeds fish XjX_j some special food. This changes the size of fish XjX_j to YjY_j.

  • Type 2: JOI takes out the fish with indices in the interval [Lj,Rj][L_j,R_j] and performs the following experiment:
    JOI puts fish Lj,Lj+1,,RjL_j,L_j+1,\dots,R_j into a fish tank from left to right in order. Due to the property described above, in the end only one fish will survive. The index of the surviving fish depends on which fish eat which fish at which times. JOI wants to know how many fish could possibly become the final survivor. During the experiment, fish indices do not change, and it is not allowed that two fish eat the same fish at the same time.

Write a program that, given JOI's fish and the experiment information, computes the answer for each Type 2 action, so that JOI can prove or disprove his idea. Note that this is only a thought experiment; no fish are actually eaten.

Input Format

The first line contains a positive integer NN, representing the number of fish.

The second line contains NN positive integers A1,A2,,ANA_1,A_2,\dots,A_N, representing the size of each fish.

The third line contains a positive integer QQ, representing the number of days of raising fish.
The next QQ lines describe the operations. On line jj (1jQ)(1 \le j \le Q), there are several space-separated integers, and the first integer is TjT_j, representing the operation type.

  • If Tj=1T_j=1, then the line also contains two positive integers Xj,YjX_j,Y_j, meaning that on day jj JOI performed a Type 1 action. The size of fish XjX_j becomes YjY_j.
  • If Tj=2T_j=2, then the line also contains two positive integers Lj,RjL_j,R_j, meaning that on day jj JOI performed a Type 2 action. JOI performs an experiment on the fish with indices in [Lj,Rj][L_j,R_j].

Output Format

For each Type 2 action (that is, for each jj (1jQ)(1 \le j \le Q) with Tj=2T_j=2), output one line with one integer, representing how many fish could possibly become the final survivor.

5
6 4 2 2 6
6
2 1 5
2 1 3
1 3 1
2 2 5
2 1 5
2 2 4
5
2
2
3
1
13
10 4 2 5 20 5 4 8 20 10 3 3 7
1
2 1 13
7
12
32 32 4 1 1 1 1 4 4 16 32 128
7
2 1 12
2 2 6
2 8 10
2 1 9
2 3 8
2 5 9
2 2 12
12
1
1
2
6
2
1
10
2 3 5 10 1 3 4 9 5 2
8
2 1 10
1 10 5
2 1 10
1 4 1000000000
2 1 10
1 8 20
1 4 8
2 1 10
4
6
1
6

Hint

Sample Explanation #1

Over 66 days, JOI performed the following actions:

  • Day 1: He performed an experiment on fish 1,2,3,4,51,2,3,4,5.
  • Day 2: He performed an experiment on fish 1,2,31,2,3.
  • Day 3: He fed fish 33 special food, changing its size to 11.
  • Day 4: He performed an experiment on fish 2,3,4,52,3,4,5.
  • Day 5: He performed an experiment on fish 1,2,3,4,51,2,3,4,5.
  • Day 6: He performed an experiment on fish 2,3,42,3,4.

The result of the experiment on Day 1 is as follows:

  • The fish sizes in the tank in order are [6,4,2,2,6][6,4,2,2,6].
  • For example, through the following process, fish 22 can become the final survivor (where the bold number is the size of fish 22).
    [6,4,2,2,6][6,\textbf 4,2,2,6] (initial state) \longrightarrow [6,4,4,6][6,\textbf 4,4,6] (fish 44 eats fish 33) \longrightarrow [6,8,6][6,\textbf 8,6] (fish 22 eats fish 44) \longrightarrow [14,6][\textbf{14},6] (fish 22 eats fish 11) \longrightarrow [20][\textbf{20}] (fish 22 eats fish 55).
  • Similarly, fish 1,2,3,4,51,2,3,4,5 can all become the final survivor. Therefore, the answer is 55.

This sample satisfies the constraints of subtasks 1,3,61,3,6.

Sample Explanation #2

This sample satisfies the constraints of all subtasks.

Sample Explanation #3

This sample satisfies the constraints of subtasks 1,3,4,61,3,4,6.

Sample Explanation #4

This sample satisfies the constraints of subtasks 1,3,5,61,3,5,6.

Constraints

For all testdata, it holds that:

  • 1N,Q1000001 \le N,Q \le 100\,000.
  • 1Ai1091 \le A_i \le 10^9 (1iN)(1\le i\le N).
  • Tj{1,2}T_j \in \{1,2\}.
  • 1XjN1 \le X_j \le N (1jQ)(1\le j\le Q).
  • 1Yj1091 \le Y_j \le 10^9
  • 1LjRjN1 \le L_j \le R_j \le N (1jQ)(1 \le j \le Q).

The additional constraints and scores for subtasks are shown in the table below:

Subtask ID Additional Constraints Score
11 N500N \le 500, Q500Q \le 500 55
22 Q=1Q=1, Tj=2T_j=2, Lj=1L_j=1, Rj=NR_j=N (1jQ)(1 \le j \le Q) 88
33 Q1000Q\le 1\,000 1212
44 Tj=2T_j=2 (1jQ)(1 \le j\le Q) 2323
55 For each jj (1jQ)(1\le j\le Q) with Tj=2T_j=2, it holds that Lj=1L_j=1, Rj=NR_j=N 3535
66 No additional constraints 1717

Translated by ChatGPT 5