Under construction

Still being built. Things may change

Joint Training

Time limit3sMemory limit1024 MB

Summary
Given N units with bus costs and dissatisfaction updates, check each lineup's validity, find the max feasible session cost, and count distinct costs per unit.
Level

Hard10 of 10

Topics
Graph, Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

With a special training period approaching, NN units work together in a joint training exercise. Each joint training session selects a total of 2N2N soldiers. No unit has a limit on how many soldiers it can send, and some units may send none. Each participating unit rents a bus to carry its soldiers to the training site. The rental cost for unit ii is aia_i. The cost of a joint training session is the sum of the bus rental costs of the participating units.

The session is held with the 2N2N soldiers standing in a line. After training ends, for every pair of adjacent soldiers, the dissatisfaction between their units increases by 11. Pairs from the same unit do not increase dissatisfaction. The commander does not approve any plan in which two adjacent soldiers come from units with dissatisfaction at least KK. Before training starts, the dissatisfaction between every pair of units is 00.

During the special training period, the commander must write reports on three kinds of events.

  1. The commander receives a training plan that lists the units of the 2N2N soldiers in line order. If any pair of adjacent soldiers has dissatisfaction at least KK, the plan is not approved. Otherwise, the training is carried out and the commander reports its total cost.
  2. A dispute breaks out between units sis_i and tit_i, and the dissatisfaction between the two units increases by xix_i. To find a budget that allows training under any circumstances, the commander reports the maximum cost among all feasible plans.
  3. Due to rising prices, the bus rental cost of unit sis_i increases by xix_i. Paying each unit a different amount makes administration complicated, so the commander reports the maximum number of distinct costs among the training plans that include unit sis_i.

The increases in dissatisfaction and cost from each event accumulate and remain in effect until all events end.

Input

The first line contains two integers NN and KK, the number of units and the smallest dissatisfaction value that forbids adjacency.

The second line contains the bus rental costs aia_i of each unit, separated by spaces.

The third line contains the number of events QQ.

The next QQ lines each give the event type qiq_i and the parameters for that event.

  • If qi=1q_i=1, the unit numbers bib_i of the soldiers are given in line order, separated by spaces.
  • If qi=2q_i=2, the two unit numbers sis_i and tit_i and the increase in dissatisfaction xix_i are given, separated by spaces.
  • If qi=3q_i=3, the unit number sis_i and the increase in bus rental cost xix_i are given, separated by spaces.

Output

On the ii-th line, print the answer to the ii-th event.

  • If qi=1q_i=1, print the total cost of the training. If the plan is not approved, print -1.
  • If qi=2q_i=2, print the maximum cost of a training session that can be carried out.
  • If qi=3q_i=3, print the maximum number of distinct costs among the training plans that include unit sis_i.

Constraints

  • 1≤N≤100 0001 \leq N \leq 100\,000; 1≤K≤1091 \leq K \leq 10^9
  • 1≤ai≤1091 \leq a_i \leq 10^9
  • 1≤Q≤100 0001 \leq Q \leq 100\,000
  • 1≤qi≤31 \leq q_i \leq 3
  • 1≤bi,si,ti≤N1 \leq b_i, s_i, t_i \leq N
  • si≠tis_i \neq t_i
  • 1≤xi≤1091 \leq x_i \leq 10^9
  • The total number of integers in the input does not exceed 500 000500\,000.

Examples1

  1. Example 1

    Input
    3 4
    1 2 4
    5
    1 2 1 1 3 3 1
    1 3 3 3 3 3 3
    2 1 3 8
    2 3 2 5
    3 1 1
    
    Expected output
    7
    4
    7
    4
    1