HTML & JS
import java.util.*;
import java.io.*;
class KGSS_Maximum_Sum {
    static class FastReader {
		BufferedReader br;
		StringTokenizer st;
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
    }
    private static ArrayList<ArrayList<Integer>>tree;
    private static int arr[];
    private static int n;
    public static void main(String[] args) {
        FastReader sc=new FastReader();
        n=sc.nextInt();
        arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        tree=new ArrayList<ArrayList<Integer>>(4*n);
        for(int i=0;i<4*n;i++){
            tree.add(new ArrayList<Integer>(2));
        }
        buildTree(0,n-1,0);
        int q=sc.nextInt();
        while(q-->0){
            char type=sc.next().charAt(0);
            if(type=='U'){
                int index=sc.nextInt()-1;
                int value=sc.nextInt();
                updateTree(0,n-1,0,index,value);
            }
            else{
                int left=sc.nextInt()-1;
                int right=sc.nextInt()-1;
                ArrayList<Integer>ans=query(0,n-1,0,left,right);
                System.out.println(ans.get(0)+ans.get(1));
            }
        }
    }
    private static ArrayList<Integer> query(int start, int end, int node, int left, int right) {
        if(start>right||end<left){
            ArrayList<Integer>ans=new ArrayList<>(2);
            ans.add(-1);
            ans.add(-1);
            return ans;
        }
        if(left<=start&&end<=right) return tree.get(node);
        int mid=start+(end-start)/2;
        ArrayList<Integer>l_part=query(start, mid, 2*node+1, left, right);
        ArrayList<Integer>r_part=query(mid+1, end, 2*node+2, left, right);
        
        int st_max=Math.max(l_part.get(0),r_part.get(0));
        int nd_max=Math.max(Math.min(l_part.get(0),r_part.get(0)),Math.max(l_part.get(1), r_part.get(1)));
        ArrayList<Integer>ans=new ArrayList<Integer>(2);
        ans.add(st_max);
        ans.add(nd_max);
        return ans;
    }
    private static void updateTree(int start, int end, int node, int index, int value) {
        if(start==end){
            tree.get(node).set(0,value);
            tree.get(node).set(1,-1);
            arr[index]=value;
            return;
        }
        int mid=start+(end-start)/2;
        if(index<=mid){
            updateTree(start, mid, 2*node+1, index, value);
        }
        else{
            updateTree(mid+1, end, 2*node+2, index, value);
        }
        tree.get(node).set(0,Math.max(tree.get(2*node+1).get(0), tree.get(2*node+2).get(0)));
        tree.get(node).set(1,Math.max(Math.min(tree.get(2*node+1).get(0), tree.get(2*node+2).get(0)), Math.max(tree.get(2*node+1).get(1), tree.get(2*node+2).get(1))));
    }
    private static void buildTree(int start, int end, int node) {
        if(start==end){
            tree.get(node).add(arr[start]);
            tree.get(node).add(-1);
            return;
        }
        int mid=start+(end-start)/2;
        buildTree(start, mid, 2*node+1);
        buildTree(mid+1, end, 2*node+2);
        tree.get(node).add(Math.max(tree.get(2*node+1).get(0), tree.get(2*node+2).get(0)));
        tree.get(node).add(Math.max(Math.min(tree.get(2*node+1).get(0), tree.get(2*node+2).get(0)), Math.max(tree.get(2*node+1).get(1), tree.get(2*node+2).get(1))));
    }
}

Generated URL:

Output:

7
9
11
12
Report Bug