publicclassSolution { /** *@param root, index, value: The root of segment tree and *@ change the node's value with [index, index] to the new given value *@return: void */publicvoidmodify(SegmentTreeNode root,int index,int value) {// write your code hereif(root ==null) return;if(index <root.start|| index >root.end) return;// Segment Tree 不会出现单独分叉的节点,所以到叶节点可以直接返回。if(index ==root.start&& index ==root.end){root.max= value;return; }modify(root.left, index, value);modify(root.right, index, value);root.max=Math.max(root.left.max,root.right.max); }}
publicclassSolution { /** *@param root, start, end: The root of segment tree and * an segment / interval *@return: The maximum number in the interval [start, end] */publicintquery(SegmentTreeNode root,int start,int end) {// write your code hereif(end <root.start) returnInteger.MIN_VALUE;if(start >root.end) returnInteger.MIN_VALUE; start =Math.max(start,root.start); end =Math.min(end,root.end);if(start ==root.start&& end ==root.end) returnroot.max;int left =query(root.left, start, end);int right =query(root.right, start, end);returnMath.max(left, right); }}
publicclassSolution { /** *@param root, start, end: The root of segment tree and * an segment / interval *@return: The count number in the interval [start, end] */publicintquery(SegmentTreeNode root,int start,int end) {// write your code hereif(root ==null) return0;if(end <root.start) return0;if(start >root.end) return0; start =Math.max(start,root.start); end =Math.min(end,root.end);if(root.start== start &&root.end== end) returnroot.count;int left =query(root.left, start, end);int right =query(root.right, start, end);return left + right; }}