1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| class SegmentTree { int[] nums; int[] treeSum; int[] treeMin; int[] treeMax; int n; public SegmentTree(int[] nums) { this.nums = nums; this.n = nums.length; this.treeSum = new int[4 * n]; build(1, 0, n - 1); } private void build(int idx, int left, int right) { if (left == right) { treeSum[idx] = nums[left]; treeMin[idx] = nums[left]; treeMax[idx] = nums[left]; return; } int mid = left + (right - left) / 2; build(idx * 2, left, mid); build(idx * 2 + 1, mid + 1, right); pushUp(idx); }
private void pushUp(int idx) { treeSum[idx] = treeSum[idx * 2] + treeSum[idx * 2 + 1]; treeMax[idx] = Math.max(treeMax[idx * 2], treeMax[idx * 2 + 1]); treeMin[idx] = Math.min(treeMin[idx * 2], treeMin[idx * 2 + 1]); }
private void add(int idx, int left, int right, int val, int i) { if (left == right) { treeSum[idx] += val; treeMax[idx] = treeSum[idx]; treeMin[idx] = treeSum[idx]; return; } int mid = left + (right - left) / 2; if (i <= left) { add(idx * 2, left, mid, val, i); } else { add(idx * 2 + 1, mid + 1, right, val, i); } pushUp(idx); }
public void add(int i, int val) { add(1, 0, n - 1, val, i); }
private void update(int idx, int left, int right, int val, int i) { if (left == right) { treeSum[idx] = val; treeMin[idx] = val; treeMax[idx] = val; return; } int mid = left + (right - left) / 2; if (i <= mid) { update(idx * 2, left, mid, val, i); } else { update(idx * 2 + 1, mid + 1, right, val, i); } pushUp(idx); }
public void update(int i, int val) { update(1, 0, n - 1, val, i); }
private int querySum(int idx, int left, int right, int i) { if (left == right) { return treeSum[left]; } int mid = left + (right - left) / 2; if (i <= mid) { return querySum(idx * 2, left, mid, i); } else { return querySum(idx * 2 + 1, mid + 1, right, i); } }
public int querySum(int i) { return querySum(1, 0, n - 1, i); }
private int querySum(int idx, int left, int right, int L, int R) { if (left >= L && right <= R) { return treeSum[idx]; } int mid = left + (right - left) / 2; int ans = 0; if (L <= mid) { ans += querySum(idx * 2, left, mid, L, R); } if (R > mid) { ans += querySum(idx * 2 + 1, mid + 1, right, L, R); } return ans; }
public int querySum(int L, int R) { return querySum(1, 0, n - 1, L, R); }
private int min(int idx, int left, int right, int L, int R) { if (left >= L && right <= R) { return treeMin[idx]; } int mid = left + (right - left) / 2; int leftMin = Integer.MAX_VALUE; int rightMin = Integer.MAX_VALUE; if (L <= mid) { leftMin = min(idx * 2, left, mid, L, R); } if (R > mid) { rightMin = min(idx * 2 + 1, mid + 1, right, L, R); } return Math.min(leftMin, rightMin); } private int max(int idx, int left, int right, int L, int R) { if (left >= L && right <= R) { return treeMax[idx]; } int mid = left + (right - left) / 2; int leftMax = Integer.MIN_VALUE; int rightMax = Integer.MIN_VALUE; if (L <= mid) { leftMax = max(idx * 2, left, mid, L, R); } if (R > mid) { rightMax = max(idx * 2 + 1, mid + 1, right, L, R); } return Math.max(leftMax, rightMax); } public int min(int L, int R) { return min(1, 0, n - 1, L, R); } public int max(int L, int R) { return max(1, 0, n - 1, L, R); }
}
|