-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSegmentTree.cpp
More file actions
112 lines (94 loc) · 2.78 KB
/
Copy pathSegmentTree.cpp
File metadata and controls
112 lines (94 loc) · 2.78 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
class NumArray {
public:
class SegTreeNode
{
public:
int leftInd;
int rightInd;
int val;
SegTreeNode *left;
SegTreeNode *right;
SegTreeNode()
{
left = NULL;
right = NULL;
}
};
SegTreeNode *root;
int makeTree(SegTreeNode *root, const vector<int> &vec)
{
if(root->leftInd == root->rightInd)
{
root->val = vec[root->leftInd];
return vec[root->leftInd];
}
int mid = (root->leftInd + root->rightInd)/2;
SegTreeNode *lf = new SegTreeNode();
lf->leftInd = root->leftInd;
lf->rightInd = mid;
root->left = lf;
SegTreeNode *rf = new SegTreeNode();
rf->leftInd = mid+1;
rf->rightInd = root->rightInd;
root->right = rf;
root->val = makeTree(lf, vec) + makeTree(rf, vec);
return root->val;
}
NumArray(vector<int> nums) {
if(nums.size() > 0){
root = new SegTreeNode();
root->leftInd = 0;
root->rightInd = nums.size()-1;
SegTreeNode *dummy = root;
int sum = makeTree(dummy, nums);}
}
void update(int i, int val) {
SegTreeNode *dummy = root;
getUpdate(dummy, i, val);
return;
}
int getUpdate(SegTreeNode *root, int &i, int &value)
{
if(root->leftInd == i && root->rightInd == i)
{
int diff = value - root->val;
root->val = value;
return diff;
}
if(i > root->rightInd || i < root->leftInd)
return INT_MAX;
int a = getUpdate(root->left, i, value);
int b = getUpdate(root->right, i, value);
if(a != INT_MAX)
{
root->val += a;
return a;
}
if(b != INT_MAX)
{
root->val += b;
return b;
}
}
int sumRange(int i, int j) {
SegTreeNode *dummy = root;
return sumVal(dummy, i, j);
}
int sumVal(SegTreeNode *root, int &i, int &j)
{
if(i > root->rightInd || j < root->leftInd)
return 0;
if(root->leftInd >= i && root->rightInd <= j)
{
// cout<<"i "<<i<<" leftind "<<root->leftInd<<" r "<<root->rightInd<<endl;
return root->val;
}
return sumVal(root->left, i, j) + sumVal(root->right, i, j);
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.update(i,val);
* int param_2 = obj.sumRange(i,j);
*/