Incinerator

Time limit2sMemory limit512 MB

Summary
Maintain a queue of waste and M incinerator cells under burn, query, append, and recycle commands, then report the final cells.
Level

Medium7 of 10

Topics
Implementation, Queue, Array, Simulation
Solved
No attempts yet

Problem

Jongyeong wants to burn NN pieces of waste in order. There are KK kinds, numbered 11 through KK. The kinds in queue order form the sequence A1,A2,…,ANA_1, A_2, \dots, A_N. More waste to burn can be added to the back of the queue later.

The incinerator has MM cells in a row, numbered 11 to MM from the left. At the start, take min⁡(N,M)\min(N, M) pieces from the front of the queue and place them in order into cells 11 through min⁡(N,M)\min(N, M). If N<MN < M, the cells on the right stay empty.

One burning operation burns everything in a consecutive interval [L,R][L, R] (L≤RL \le R) at once. After burning, cells [L,R][L, R] are empty, so refill them in order from cell LL with pieces taken in order from the front of the queue. If the queue runs out while refilling, leave the remaining cells empty.

Write a program that performs the following four kinds of commands QQ times in total.

  • Burn the interval [L,R][L, R] of the incinerator.
  • Print the kind in cell ii of the incinerator.
  • Append qq pieces of kind pp to the back of the current queue.
  • Remove tt pieces from the front of the current queue for recycling.

After all commands, also print the current state of the incinerator.

Input

The first line holds N,M,K,QN, M, K, Q in order. (1≤N,M,K,Q≤5×1051 \le N, M, K, Q \le 5 \times 10^5)

The second line holds NN integers A1,A2,…,ANA_1, A_2, \dots, A_N. (1≤Ai≤K1 \le A_i \le K)

Each of the next QQ lines holds one command. Every line starts with an integer oo giving the command kind. (1≤o≤41 \le o \le 4)

  • If o=1o = 1, it is the first command, followed by LL and RR. (1≤L≤R≤M1 \le L \le R \le M)
  • If o=2o = 2, it is the second command, followed by ii. (1≤i≤M1 \le i \le M)
  • If o=3o = 3, it is the third command, followed by pp and qq. (1≤p≤K1 \le p \le K, 1≤q≤1061 \le q \le 10^6)
  • If o=4o = 4, it is the fourth command, followed by tt. (1≤t≤1 \le t \le the number of pieces currently in the queue)

The second command appears at least once.

Output

On the first line, print the answers to all second commands in order, separated by spaces. On the second line, print the kinds left in the incinerator from cell 11 to cell MM in order, separated by spaces, after all commands. Print 00 for an empty cell.

Examples3

  1. Example 1

    Input
    10 3 100 7
    1 2 3 4 5 7 7 8 9 10
    1 1 3
    4 4
    3 100 1000000
    4 999999
    2 2
    1 1 3
    2 2
    Expected output
    5 0
    100 0 0
  2. Example 2

    Input
    1 1 1 1
    1
    2 1
    Expected output
    1
    1
  3. Example 3

    Input
    10 10 500000 8
    1 2 3 4 5 6 7 8 9 10
    1 1 10
    3 314159 10
    1 1 10
    1 2 4
    1 7 9
    2 5
    3 500000 6
    1 2 8
    Expected output
    314159
    314159 500000 500000 500000 500000 500000 500000 0 0 314159