|
| 1 | +package template.seg; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +public class SegmentTreeBinarySearchIndex0 { |
| 6 | + static class Node { |
| 7 | + long maxVal, minVal, sum; |
| 8 | + |
| 9 | + public Node(long maxVal, long minVal, long sum) { |
| 10 | + this.maxVal = maxVal; |
| 11 | + this.minVal = minVal; |
| 12 | + this.sum = sum; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + int n; |
| 17 | + Node[] tree; |
| 18 | + |
| 19 | + public SegmentTreeBinarySearchIndex0(int[] nums) { |
| 20 | + n = nums.length; |
| 21 | + tree = new Node[4 * n]; |
| 22 | + Arrays.setAll(tree, e -> new Node(0, 0, 0)); |
| 23 | + buildTree(nums, 0, 0, n - 1); |
| 24 | + } |
| 25 | + |
| 26 | + private void buildTree(int[] nums, int treeIndex, int lo, int hi) { |
| 27 | + if (lo == hi) { |
| 28 | + tree[treeIndex] = new Node(nums[lo], nums[lo], nums[lo]); |
| 29 | + return; |
| 30 | + } |
| 31 | + int mid = lo + (hi - lo) / 2; |
| 32 | + buildTree(nums, treeIndex * 2 + 1, lo, mid); |
| 33 | + buildTree(nums, treeIndex * 2 + 2, mid + 1, hi); |
| 34 | + pushUp(treeIndex); |
| 35 | + } |
| 36 | + |
| 37 | + private void pushUp(int treeIndex) { |
| 38 | + tree[treeIndex].maxVal = Math.max(tree[treeIndex * 2 + 1].maxVal, tree[treeIndex * 2 + 2].maxVal); |
| 39 | + tree[treeIndex].minVal = Math.min(tree[treeIndex * 2 + 1].minVal, tree[treeIndex * 2 + 2].minVal); |
| 40 | + tree[treeIndex].sum = tree[treeIndex * 2 + 1].sum + tree[treeIndex * 2 + 2].sum; |
| 41 | + } |
| 42 | + |
| 43 | + void update(int index, long val) { |
| 44 | + updateTree(0, 0, n - 1, index, val); |
| 45 | + } |
| 46 | + |
| 47 | + private void updateTree(int treeIndex, int lo, int hi, int arrIndex, long val) { |
| 48 | + if (lo == hi) { |
| 49 | + tree[treeIndex] = new Node(val, val, val); |
| 50 | + return; |
| 51 | + } |
| 52 | + int mid = lo + (hi - lo) / 2; |
| 53 | + if (arrIndex <= mid) { |
| 54 | + updateTree(treeIndex * 2 + 1, lo, mid, arrIndex, val); |
| 55 | + } else { |
| 56 | + updateTree(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val); |
| 57 | + } |
| 58 | + pushUp(treeIndex); |
| 59 | + } |
| 60 | + |
| 61 | + long queryMax(int i, int j) { |
| 62 | + return queryTree(0, 0, n - 1, i, j).maxVal; |
| 63 | + } |
| 64 | + |
| 65 | + long queryMin(int i, int j) { |
| 66 | + return queryTree(0, 0, n - 1, i, j).minVal; |
| 67 | + } |
| 68 | + |
| 69 | + long querySum(int i, int j) { |
| 70 | + return queryTree(0, 0, n - 1, i, j).sum; |
| 71 | + } |
| 72 | + |
| 73 | + private Node queryTree(int treeIndex, int lo, int hi, int i, int j) { |
| 74 | + if (lo > j || hi < i) return new Node(Integer.MIN_VALUE, Integer.MAX_VALUE, 0); |
| 75 | + if (i <= lo && hi <= j) return tree[treeIndex]; |
| 76 | + int mid = lo + (hi - lo) / 2; |
| 77 | + Node leftQuery = queryTree(treeIndex * 2 + 1, lo, mid, i, j); |
| 78 | + Node rightQuery = queryTree(treeIndex * 2 + 2, mid + 1, hi, i, j); |
| 79 | + |
| 80 | + return new Node(Math.max(leftQuery.maxVal, rightQuery.maxVal), |
| 81 | + Math.min(leftQuery.minVal, rightQuery.minVal), |
| 82 | + leftQuery.sum + rightQuery.sum); |
| 83 | + } |
| 84 | + |
| 85 | + long acc; |
| 86 | + |
| 87 | + int findTargetSumIndex(long target) { |
| 88 | + acc = 0; |
| 89 | + return binarySearchSum(0, 0, n - 1, target); |
| 90 | + } |
| 91 | + |
| 92 | + private int binarySearchSum(int treeIndex, int curLo, int curHi, long target) { |
| 93 | + if (curLo == curHi) { |
| 94 | + if (acc + tree[treeIndex].sum >= target) return curLo; |
| 95 | + acc += tree[treeIndex].sum; |
| 96 | + return -1; |
| 97 | + } |
| 98 | + int mid = curLo + (curHi - curLo) / 2; |
| 99 | + if (acc + tree[treeIndex * 2 + 1].sum >= target) { |
| 100 | + return binarySearchSum(treeIndex * 2 + 1, curLo, mid, target); |
| 101 | + } else { |
| 102 | + acc += tree[treeIndex * 2 + 1].sum; |
| 103 | + return binarySearchSum(treeIndex * 2 + 2, mid + 1, curHi, target); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + int binarySearchMax(int i, int j, long target) { |
| 108 | + return binarySearchMax(0, 0, n - 1, i, j, target); |
| 109 | + } |
| 110 | + |
| 111 | + private int binarySearchMax(int treeIndex, int curLo, int curHi, |
| 112 | + int targetLo, int targetHi, long target) { |
| 113 | + if (curHi < targetLo || curLo > targetHi |
| 114 | + || tree[treeIndex].maxVal < target) { |
| 115 | + return -1; |
| 116 | + } |
| 117 | + if (curLo == curHi) { |
| 118 | + return tree[treeIndex].maxVal >= target ? curLo : -1; |
| 119 | + } |
| 120 | + |
| 121 | + int mid = curLo + (curHi - curLo) / 2; |
| 122 | + int res = -1; |
| 123 | + // 先尝试在左子树中查找 |
| 124 | + if (mid >= targetLo) { // 只有当左子树可能包含目标区间时才搜索 |
| 125 | + res = binarySearchMax(treeIndex * 2 + 1, curLo, mid, targetLo, targetHi, target); |
| 126 | + } |
| 127 | + // 如果左子树没有找到,并且右子树可能包含目标区间,才在右子树中搜索 |
| 128 | + if (res == -1 && mid < targetHi && tree[treeIndex * 2 + 2].maxVal >= target) { |
| 129 | + res = binarySearchMax(treeIndex * 2 + 2, mid + 1, curHi, targetLo, targetHi, target); |
| 130 | + } |
| 131 | + return res; |
| 132 | + } |
| 133 | + |
| 134 | + int binarySearchMin(int i, int j, long target) { |
| 135 | + return binarySearchMin(0, 0, n - 1, i, j, target); |
| 136 | + } |
| 137 | + |
| 138 | + private int binarySearchMin(int treeIndex, int curLo, int curHi, |
| 139 | + int targetLo, int targetHi, long target) { |
| 140 | + if (curHi < targetLo || curLo > targetHi |
| 141 | + || tree[treeIndex].minVal > target) { |
| 142 | + return -1; |
| 143 | + } |
| 144 | + if (curLo == curHi) { |
| 145 | + return tree[treeIndex].maxVal <= target ? curLo : -1; |
| 146 | + } |
| 147 | + |
| 148 | + int mid = curLo + (curHi - curLo) / 2; |
| 149 | + int res = -1; |
| 150 | + // 先尝试在左子树中查找 |
| 151 | + if (mid >= targetLo && tree[treeIndex * 2 + 1].minVal <= target) { // 只有当左子树可能包含目标区间时才搜索 |
| 152 | + res = binarySearchMin(treeIndex * 2 + 1, curLo, mid, targetLo, targetHi, target); |
| 153 | + } |
| 154 | + // 如果左子树没有找到,并且右子树可能包含目标区间,才在右子树中搜索 |
| 155 | + if (res == -1 && mid < targetHi) { |
| 156 | + res = binarySearchMin(treeIndex * 2 + 2, mid + 1, curHi, targetLo, targetHi, target); |
| 157 | + } |
| 158 | + return res; |
| 159 | + } |
| 160 | +} |
0 commit comments