|
1 | 1 | class NumArray { |
2 | 2 | public: |
3 | 3 |
|
| 4 | + public: |
4 | 5 | void buildTree(int idx, int low, int high) |
5 | 6 | { |
6 | 7 | if (low == high) |
7 | 8 | { |
8 | | - seg[idx] = v[low]; |
| 9 | + tree[idx] = arr[low]; |
9 | 10 | return; |
10 | 11 | } |
11 | 12 |
|
12 | | - int mid = (low + high) / 2; |
| 13 | + int mid = (low + high) >> 1; |
13 | 14 | buildTree(2 * idx + 1, low, mid); |
14 | 15 | buildTree(2 * idx + 2, mid + 1, high); |
15 | 16 |
|
16 | | - // update |
17 | | - seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; |
18 | | - } |
| 17 | + // updation part |
| 18 | + |
| 19 | + // sum |
| 20 | + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; |
19 | 21 |
|
20 | | - int query(int idx, int low, int high, int l, int r) |
| 22 | + // min |
| 23 | + // tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + public: |
| 28 | + int queryTree(int idx, int low, int high, int l, int r) |
21 | 29 | { |
22 | 30 | // no overlap |
23 | 31 | // l r low high or low high l r |
24 | 32 | if (r < low or l > high) |
25 | 33 | return 0; |
| 34 | + // return INT_MAX; // min |
26 | 35 |
|
27 | 36 | // complete overlap |
28 | 37 | // [l low high r] |
29 | 38 | if (low >= l and high <= r) |
30 | | - return seg[idx]; |
| 39 | + return tree[idx]; |
31 | 40 |
|
32 | 41 | // partial overlap |
33 | 42 | int mid = (low + high) / 2; |
34 | | - int left = query(2 * idx + 1, low, mid, l, r); |
35 | | - int right = query(2 * idx + 2, mid + 1, high, l, r); |
| 43 | + int left = queryTree(2 * idx + 1, low, mid, l, r); |
| 44 | + int right = queryTree(2 * idx + 2, mid + 1, high, l, r); |
| 45 | + |
| 46 | + // updation part |
| 47 | + // sum |
36 | 48 |
|
37 | | - // update |
38 | 49 | return left + right; |
| 50 | + |
| 51 | + // min |
| 52 | + // return min(left, right); |
39 | 53 | } |
40 | 54 |
|
41 | | - void update(int idx, int low, int high, int ind, int val) |
| 55 | +public: |
| 56 | + void updateTree(int idx, int low, int high, int ind, int val) |
42 | 57 | { |
43 | 58 | if (low == high) |
44 | 59 | { |
45 | | - seg[idx] = val; |
| 60 | + // sum ranges increment tree[idx] by val case |
| 61 | + // tree[idx] += val; |
| 62 | + |
| 63 | + // min and update single value case |
| 64 | + tree[idx] = val; |
46 | 65 | return; |
47 | 66 | } |
48 | | - |
49 | | - int mid = (low + high) / 2; |
| 67 | + int mid = (low + high) >> 1; |
50 | 68 | if (ind <= mid) |
51 | | - update(2 * idx + 1, low, mid, ind, val); |
| 69 | + updateTree(2 * idx + 1, low, mid, ind, val); |
52 | 70 | else |
53 | | - update(2 * idx + 2, mid + 1, high, ind, val); |
| 71 | + updateTree(2 * idx + 2, mid + 1, high, ind, val); |
54 | 72 |
|
55 | | - // update |
56 | | - seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; |
| 73 | + // updation Part |
| 74 | + // sum |
| 75 | + |
| 76 | + tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2]; |
| 77 | + |
| 78 | + // min |
| 79 | + |
| 80 | + // tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]); |
| 81 | + } |
| 82 | + |
| 83 | + void build() |
| 84 | + { |
| 85 | + buildTree(0, 0, N - 1); |
57 | 86 | } |
58 | 87 |
|
59 | | - vector<int> seg; |
60 | | - vector<int> v; |
| 88 | + int query(int l, int r) |
| 89 | + { |
| 90 | + return queryTree(0, 0, N - 1, l, r); |
| 91 | + } |
| 92 | + |
| 93 | + void Update(int ind, int val) |
| 94 | + { |
| 95 | + updateTree(0, 0, N - 1, ind, val); |
| 96 | + } |
61 | 97 |
|
| 98 | + vector<int> tree, arr; |
| 99 | + int N; |
62 | 100 | NumArray(vector<int>& nums) { |
63 | | - int n = nums.size(); |
64 | | - seg.resize(n * 4 + 5); |
65 | | - v = nums; |
66 | | - buildTree(0,0,n-1); |
| 101 | + N = nums.size(); |
| 102 | + arr = nums; |
| 103 | + tree.resize(N*4 + 5); |
| 104 | + build(); |
67 | 105 | } |
68 | 106 |
|
69 | 107 | void update(int index, int val) { |
70 | | - update(0,0,v.size()-1,index,val); |
| 108 | + Update(index,val); |
71 | 109 | } |
72 | 110 |
|
73 | 111 | int sumRange(int left, int right) { |
74 | | - return query(0,0,v.size()-1,left,right); |
| 112 | + return query(left, right); |
75 | 113 | } |
76 | 114 | }; |
77 | 115 |
|
|
0 commit comments