-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (116 loc) · 3.52 KB
/
main.cpp
File metadata and controls
132 lines (116 loc) · 3.52 KB
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
// Source: https://leetcode.com/problems/range-sum-query-mutable
// Title: Range Sum Query - Mutable
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an integer array `nums`, handle multiple queries of the following types:
//
// - **Update** the value of an element in `nums`.
// - Calculate the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** where `left <= right`.
//
// Implement the `NumArray` class:
//
// - `NumArray(int[] nums)` Initializes the object with the integer array `nums`.
// - `void update(int index, int val)` **Updates** the value of `nums[index]` to be `val`.
// - `int sumRange(int left, int right)` Returns the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** (i.e. `nums[left] + nums[left + 1] + ... + nums[right]`).
//
// **Example 1:**
//
// ```
// Input:
// ["NumArray", "sumRange", "update", "sumRange"]
// [[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
//
// Output:
// [null, 9, null, 8]
//
// Explanation:
// * NumArray numArray = new NumArray([1, 3, 5]);
// * numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
// * numArray.update(1, 2); // nums = [1, 2, 5]
// * numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 3 * 10^4`
// - `-100 <= nums[i] <= 100`
// - `0 <= index < nums.length`
// - `-100 <= val <= 100`
// - `0 <= left <= right < nums.length`
// - At most `3 * 10^4` calls will be made to `update` and `sumRange`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>
using namespace std;
// Fenwick Tree
class NumArray {
int n;
vector<int> nums;
vector<int> tree; // tree[i] = sum of [i-lowbit(i), i)
public:
NumArray(vector<int>& nums) : nums(nums) {
n = nums.size();
tree.assign(n + 1, 0);
// Build O(N)
for (int i = 0; i < n; ++i) tree[i + 1] = nums[i];
for (int i = 1; i <= n; ++i) {
int p = i + (i & -i); // parent
if (p <= n) tree[p] += tree[i];
}
}
// Update: O(logN); nums[idx] = val
void update(int idx, int val) {
const int delta = val - nums[idx];
nums[idx] = val;
for (int i = idx + 1; i <= n; i += (i & -i)) {
tree[i] += delta;
}
}
// Query: O(logN); sum of [0, r)
int query(int r) {
int sum = 0;
for (int i = r; i >= 1; i -= (i & -i)) {
sum += tree[i];
}
return sum;
}
// Query O(logN); sum of [left, right]
int sumRange(int left, int right) {
++right; // convert to closed-open range
return query(right) - query(left);
}
};
// Segment Tree
class NumArray2 {
int n;
vector<int> tree; // parent i -> child 2i & 2i+1
public:
NumArray2(const vector<int>& nums) {
n = nums.size();
tree.resize(2 * n);
// Build O(N)
for (int i = 0; i < n; ++i) tree[i + n] = nums[i];
for (int i = n - 1; i >= 1; --i) {
tree[i] = tree[2 * i] + tree[2 * i + 1];
}
}
// Update: O(logN); nums[idx] = val
void update(int i, int val) {
i += n;
tree[i] = val;
for (i /= 2; i >= 1; i /= 2) {
tree[i] = tree[2 * i] + tree[2 * i + 1];
}
}
// Query O(logN); sum of [l, r]
int sumRange(int l, int r) {
++r; // convert to closed-open range
int sum = 0;
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l & 1) sum += tree[l++];
if (r & 1) sum += tree[--r];
}
return sum;
}
};