Under construction

Still being built. Things may change

Sequence and Queries 27

Time limit4sMemory limit512 MB

Summary
Apply range add, range chmax, and range chmin to array A, then answer the current range minimum of A and the range historical minimum and maximum of past values.
Level

Hard9 of 10

Topics
Segment tree, Implementation
Solved
No attempts yet

Problem

You are given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_N of length NN. There are also two sequences BB and CC of length NN, where Bi=AiB_i = A_i and Ci=AiC_i = A_i initially. Write a program that processes the following queries.

  • 1 L R X: For every L≤i≤RL \le i \le R, add XX to AiA_i.
  • 2 L R Y: For every L≤i≤RL \le i \le R, set Ai=max⁡(Ai,Y)A_i = \max(A_i, Y).
  • 3 L R Y: For every L≤i≤RL \le i \le R, set Ai=min⁡(Ai,Y)A_i = \min(A_i, Y).
  • 4 L R: Print min⁡(AL,AL+1,…,AR)\min(A_L, A_{L+1}, \ldots, A_R).
  • 5 L R: Print min⁡(BL,BL+1,…,BR)\min(B_L, B_{L+1}, \ldots, B_R).
  • 6 L R: Print max⁡(CL,CL+1,…,CR)\max(C_L, C_{L+1}, \ldots, C_R).

After each query, for every 1≤i≤N1 \le i \le N, BiB_i becomes min⁡(Bi,Ai)\min(B_i, A_i) and CiC_i becomes max⁡(Ci,Ai)\max(C_i, A_i).

Input

The first line contains NN, the length of the sequence. (1≤N≤500,0001 \le N \le 500{,}000)

The second line contains A1,A2,…,ANA_1, A_2, \ldots, A_N. (−109≤Ai≤109-10^9 \le A_i \le 10^9)

The third line contains MM, the number of queries. (1≤M≤500,0001 \le M \le 500{,}000)

Each of the next MM lines contains one query. (1≤L≤R≤N1 \le L \le R \le N, −2,000≤X≤2,000-2{,}000 \le X \le 2{,}000, −109≤Y≤109-10^9 \le Y \le 10^9) Queries 4, 5, and 6 appear at least once.

Output

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

Examples1

  1. Example 1

    Input
    3
    1 2 3
    6
    5 3 3
    1 2 3 -2
    3 1 3 0
    5 3 3
    2 2 3 4
    5 1 3
    
    Expected output
    3
    0
    0