-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment-tree.js
More file actions
171 lines (140 loc) · 4.69 KB
/
segment-tree.js
File metadata and controls
171 lines (140 loc) · 4.69 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
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
169
170
171
function build(index, from, to) {
this.heap[index] = new SegmentTreeNode();
this.heap[index].from = from;
this.heap[index].to = from + to - 1;
if (isLeafNode(to)) {
const leafVal = this.containsObjects ? this.array[from][this.propertyKey] : this.array[from];
this.heap[index].sum = leafVal;
this.heap[index].min = leafVal;
this.heap[index].max = leafVal;
} else {
build.call(this, 2 * index, from, (to / 2) | 0);
build.call(this, 2 * index + 1, from + ((to / 2) | 0), to - ((to / 2) | 0));
this.heap[index].sum = this.heap[2 * index].sum + this.heap[2 * index + 1].sum;
this.heap[index].min = Math.min(this.heap[2 * index].min, this.heap[2 * index + 1].min);
this.heap[index].max = Math.max(this.heap[2 * index].max, this.heap[2 * index + 1].max);
}
}
function isLeafNode(to) {
return to === 1;
}
function propagate(index) {
const node = this.heap[index];
if (node.pendingVal != null) {
change.call(this, this.heap[2 * index], node.pendingVal);
change.call(this, this.heap[2 * index + 1], node.pendingVal);
node.pendingVal = null;
}
}
function change(node, value) {
node.pendingVal = value;
node.sum = node.size() * value;
node.min = value;
node.max = value;
if (this.containsObjects) {
this.array[node.from][this.propertyKey] = value;
} else {
this.array[node.from] = value;
}
}
function contains(from1, to1, from2, to2) {
return from2 >= from1 && to2 <= to1;
}
function intersects(from1, to1, from2, to2) {
return (from1 <= from2 && to1 >= from2) || (from1 >= from2 && from1 <= to2);
}
function hasNodeBeenUpdated(node, from, to) {
return node.pendingVal != null && contains(node.from, node.to, from, to);
}
/**
* Time-Complexity: O(n*log(n))
*/
module.exports = class SegmentTree {
constructor(array, containsObjects, propertyKey) {
this.array = array;
this.heap = [];
this.containsObjects = containsObjects;
this.propertyKey = propertyKey;
build.call(this, 1, 0, array.length);
}
size() {
return this.array.length;
}
rangeSumQuery(from, to, index = 1) {
const node = this.heap[index];
// If you did a range update that contained this node, you can infer
// the Sum without going down the tree
if (hasNodeBeenUpdated(node, from, to)) {
return (to - from + 1) * node.pendingVal;
}
if (contains(from, to, node.from, node.to)) {
return this.heap[index].sum;
}
if (intersects(from, to, node.from, node.to)) {
propagate.call(this, index);
const leftSum = this.rangeSumQuery(from, to, 2 * index);
const rightSum = this.rangeSumQuery(from, to, 2 * index + 1);
return leftSum + rightSum;
}
return 0;
}
rangeMinQuery(from, to, index = 1) {
const node = this.heap[index];
// If you did a range update that contained this node, you can infer the Min value without going down the tree
if (hasNodeBeenUpdated(node, from, to)) {
return node.pendingVal;
}
if (contains(from, to, node.from, node.to)) {
return this.heap[index].min;
}
if (intersects(from, to, node.from, node.to)) {
propagate.call(this, index);
const leftMin = this.rangeMinQuery(from, to, 2 * index);
const rightMin = this.rangeMinQuery(from, to, 2 * index + 1);
return Math.min(leftMin, rightMin);
}
return Number.MAX_VALUE;
}
rangeMaxQuery(from, to, index = 1) {
const node = this.heap[index];
if (contains(from, to, node.from, node.to)) {
return this.heap[index].max;
}
if (intersects(from, to, node.from, node.to)) {
propagate.call(this, index);
const leftMax = this.rangeMaxQuery(from, to, 2 * index);
const rightMax = this.rangeMaxQuery(from, to, 2 * index + 1);
return Math.max(leftMax, rightMax);
}
return 0;
}
update(from, to, value, index = 1) {
// The Node of the heap tree represents a range of the array with bounds: [n.from, n.to]
const node = this.heap[index];
if (contains(from, to, node.from, node.to)) {
change.call(this, node, value);
}
if (node.size() === 1) return;
if (intersects(from, to, node.from, node.to)) {
propagate.call(this, index);
this.update(from, to, value, 2 * index);
this.update(from, to, value, 2 * index + 1);
node.sum = this.heap[2 * index].sum + this.heap[2 * index + 1].sum;
node.min = Math.min(this.heap[2 * index].min, this.heap[2 * index + 1].min);
node.max = Math.max(this.heap[2 * index].max, this.heap[2 * index + 1].max);
}
}
};
class SegmentTreeNode {
constructor() {
this.pendingVal = null;
this.sum = 0;
this.min = 0;
this.max = 0;
this.from = 0;
this.to = 0;
}
size() {
return this.to - this.from + 1;
}
}