-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree.js
More file actions
58 lines (50 loc) · 1.74 KB
/
SegmentTree.js
File metadata and controls
58 lines (50 loc) · 1.74 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
class SegmentTree {
constructor(arr) {
this.n = arr.length;
this.tree = new Array(4 * this.n);
this.build(arr, 0, 0, this.n - 1);
}
build(arr, node, start, end) {
if (start === end) {
this.tree[node] = arr[start];
} else {
const mid = Math.floor((start + end) / 2);
this.build(arr, 2 * node + 1, start, mid);
this.build(arr, 2 * node + 2, mid + 1, end);
this.tree[node] = Math.max(this.tree[2 * node + 1], this.tree[2 * node + 2]);
}
}
query(node, start, end, l, r) {
if (r < start || end < l) {
return -Infinity;
}
if (l <= start && end <= r) {
return this.tree[node];
}
const mid = Math.floor((start + end) / 2);
const left = this.query(2 * node + 1, start, mid, l, r);
const right = this.query(2 * node + 2, mid + 1, end, l, r);
return Math.max(left, right);
}
rangeQuery(l, r) {
return this.query(0, 0, this.n - 1, l, r);
}
}
function solve(input) {
const lines = input.trim().split('\n');
const N = parseInt(lines[0]);
const A = lines[1].split(' ').map(Number);
const D = parseInt(lines[2]);
const queries = lines.slice(3).map((line) => line.split(' ').map(Number));
const segTree = new SegmentTree(A);
const results = [];
for (const [L, R] of queries) {
const maxLeft = L > 1 ? segTree.rangeQuery(0, L - 2) : -Infinity;
const maxRight = R < N ? segTree.rangeQuery(R, N - 1) : -Infinity;
results.push(Math.max(maxLeft, maxRight));
}
console.log(results.join('\n'));
}
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin', 'utf-8').trim();
solve(input);