KGSS - Maximum Sum

You are given a sequence A[1], A[2], ..., A[N] ( 0 ≤ A[i] ≤ 10^8 , 2 ≤ N ≤ 10^5 ). There are two types of operations and they are defined as follows:


This will be indicated in the input by a 'U' followed by space and then two integers i and x.

U i x, 1 ≤ i ≤ N, and x, 0 ≤ x ≤ 10^8.

This operation sets the value of A[i] to x.


This will be indicated in the input by a 'Q' followed by a single space and then two integers i and j.

Q x y, 1 ≤ x < y ≤ N.

You must find i and j such that x ≤ i, j ≤ y and i != j, such that the sum A[i]+A[j] is maximized. Print the sum A[i]+A[j].


The first line of input consists of an integer N representing the length of the sequence. Next line consists of N space separated integers A[i]. Next line contains an integer Q, Q ≤ 10^5, representing the number of operations. Next Q lines contain the operations.


Output the maximum sum mentioned above, in a separate line, for each Query.


1 2 3 4 5
Q 2 4
Q 2 5
U 1 6
Q 1 5
U 1 7
Q 1 5


Warning: large Input/Output data, be careful with certain languages

hide comments
itachi_2324: 2018-06-30 10:25:20

My 100th :) Modified Range maximum Query
AC in one go.

vivek_dwivedi: 2018-06-22 16:51:30

Dont know why structured array is giving tle . Segment tree work's fine worth solving the problem. Must do it

lamia2658: 2018-05-31 11:10:55

nice problem.. learnt a lot.. AC in 1st go.. :) must try problem for beginner

atharva_sarage: 2018-04-06 08:51:22

AC in first attempt!!!

hrsh_sengar: 2018-03-12 12:14:46

my 99th :)

badboy_1496: 2018-03-03 20:53:30

AC in one go!!!!

coolio_1: 2018-03-01 11:57:56

AC in 1 go! Easy AF!

ramini1996: 2018-02-02 20:45:41

My 50th !!! AC in ONE GO !!!

kushagra__k: 2018-01-01 22:55:23

There can be 2 approaches here:
1> Use a structure array for the max and second max
2> A modification of the Range Maximum Query.

After getting AC on 1st one, go for the second one. :)

vijayrit: 2017-12-20 00:36:46

Please do take sufficienty large array for the tree. Costed me four SIGSEGV errors.

Added by:Swarnaprakash
Time limit:0.357s-0.857s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Kurukshetra 09 OPC