Range of a query

2.2

6 votes
Advanced Data Structures, Segment Trees, C++, Data Structures
Problem

You are given an array A of N integers. You are required to answer Q queries of the following types:

  • 1LR: Choose any two indices i, j such that LijR and find the maximum possible value of function F(i, j) 
  • 2XV: Update the value of the element at index X with V that is set A[X]=V.

Function F(i,j) is defined as follows:

  • F(i,j)=0, if all the elements in the subarray A[i..j] are not equal. Otherwise, F(i,j)=A[i]+max(0,A[i+1]1)+max(0,A[i+2]2)+....+max(0,A[j]j+i)

Note: Assume 1-based indexing.

Input format

  • The first line contains two space-separated integers N and Q denoting the number of elements in array A and the number of queries respectively.
  • The second line contains N space-separated integers denoting the elements of array A.
  • Next Q lines contain three space-separated integers denoting the queries.

Output format

For every query of type 1 in space-separated format, print the maximum possible value of function F.

Constraints

1N,Q3×1051A[i],V1061LRN1XN

Sample Input
5 3
2 2 1 1 1
1 2 3
2 3 2
1 1 3
Sample Output
2 3
Time Limit: 2.5
Memory Limit: 256
Source Limit:
Explanation
  • Query 1 is of type 1 with L = 2, R = 3.
    • Choose i = 2, j = 2. Value of function F(2,2) = 2
  • Query 2 is of type 2 with X = 3, V = 2.
    • Set A[3] = 2
  • Query 3 is of type 3 with L = 1, R = 3.
    • Choose i = 1, j = 3. Value  of function F(1,3) = 2 + max(0, 1) = 3.
Editor Image

?