Under construction

Still being built. Things may change

Hanbyeol's perfect sequence and query class

Time limit6sMemory limit1024 MB

Summary
Process an array with range chmin, chmax, and add updates, answering range min, max, and sum queries.
Level

Medium6 of 10

Topics
Segment tree, Array
Solved
No attempts yet

Problem

Hanbyeol's sequence and query class is always known for its perfect lectures. But Hanbyeol is a fool, so even when given a sequence and query problem, he cannot write a program himself and hands the job to you every time. Solve the problem below in his place!


Given a sequence A1,A2,⋯ANA_1, A_2, \cdots A_N of length NN, write a program that processes the following queries.

  • 11 LL RR XX: For every L≤i≤RL \leq i \leq R, apply Ai=min⁡(Ai,X)A_i = \min(A_i, X).
  • 22 LL RR XX: For every L≤i≤RL \leq i \leq R, apply Ai=max⁡(Ai,X)A_i = \max(A_i, X).
  • 33 LL RR XX: For every L≤i≤RL \leq i \leq R, apply Ai=Ai+XA_i = A_i + X.
  • 44 LL RR: Print min⁡(AL,AL+1,⋯ ,AR)\min(A_L, A_{L+1}, \cdots, A_R).
  • 55 LL RR: Print max⁡(AL,AL+1,⋯ ,AR)\max(A_L, A_{L+1}, \cdots, A_R).
  • 66 LL RR: Print AL+AL+1+⋯+ARA_L + A_{L+1} + \cdots + A_R.

Note that the range of XX is not typical.

Input

The first line contains the size of the sequence NN. (1≤N≤500 0001 \leq N \leq 500\,000)

The second line contains the integers A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces. (1≤Ai≤1091 \leq A_i \leq 10^{9})

The third line contains the number of queries MM. (1≤M≤300 0001 \leq M \leq 300\,000)

Each of the next MM lines contains one query. XX is an integer. (1≤L≤R≤N1 \leq L \leq R \leq N, 1≤X≤101 \leq X \leq \mathbf{10})

Output

Print the results of queries 4, 5, and 6, one per line.

Hint

The statement of this problem may differ from Hanbyeol's original setting.

Examples1

  1. Example 1

    Input
    3
    20 10 2
    6
    2 1 3 7
    4 2 3
    1 1 2 4
    5 2 2
    3 1 1 5
    6 1 2
    
    Expected output
    7
    4
    13