Under construction

Still being built. Things may change

Sequence and Maximum Rise Queries

Interview

Time limit1sMemory limit512 MB

Summary
Given a sequence, process point updates and report the maximum value of a_j - a_i with i at most j inside a query range.
Level

Medium4 of 10

Topics
Segment tree, Divide and conquer
Solved
No attempts yet

Problem

Given a sequence a_1,a_2,...,a_Na\_1, a\_2, ..., a\_N of length NN, write a program that processes the following queries.

  • 1 k x: Change a_ka\_k to xx.
  • 2 l r: Print the maximum rise of the interval [l,r][l, r]. The maximum rise of [l,r][l, r] is defined as max⁡(a_j−a_i) (l≤i≤j≤r)\max(a\_j-a\_i)\ (l\le i\le j \le r)

Input

The first line contains the length NN (1≤N≤100 0001 \le N \le 100\ 000) of the sequence.

The second line contains the elements a_1,a_2,...,a_Na\_1, a\_2, ..., a\_N of the sequence. The ii-th element a_ia\_i is an integer (−109≤a_i≤109-10^9 \le a\_i \le 10^9).

The third line contains the number of queries QQ (1≤Q≤100 0001 \le Q \le 100\ 000).

Each of the next QQ lines contains a query. A query consists of three integers, either 1  k  x1\ \ k\ \ x (1≤k≤N1 \le k \le N, −109≤x≤109-10^9 \le x \le 10^9) or 2  l  r2\ \ l\ \ r (1≤l≤r≤N1 \le l \le r \le N).

At least one query of type 2 is given.

Examples2

  1. Example 1

    Input
    5
    4 2 3 1 5
    4
    2 1 4
    2 2 5
    1 3 2
    2 2 4
    
    Expected output
    1
    4
    0
    
  2. Example 2

    Input
    3
    3 1 2
    4
    2 1 3
    2 1 1
    1 3 4
    2 1 3
    
    Expected output
    1
    0
    3