-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3187-peaks-in-array.cpp
More file actions
75 lines (70 loc) · 2.11 KB
/
3187-peaks-in-array.cpp
File metadata and controls
75 lines (70 loc) · 2.11 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
class Solution {
public:
struct SegmentTree {
int n;
vector<int> tree;
SegmentTree(int size) : n(size), tree(2 * size, 0) {}
void build() {
for (int i = n - 1; i > 0; i--) {
tree[i] = tree[i << 1] + tree[i << 1 | 1];
}
}
void modify(int p, int value) {
for (tree[p += n] = value; p > 1; p >>= 1) {
tree[p >> 1] = tree[p] + tree[p ^ 1];
}
}
int query(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
res += tree[l];
l++;
}
if (r & 1) {
r--;
res += tree[r];
}
}
return res;
}
};
vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {
int n = (int) nums.size();
SegmentTree st(n);
auto is_peak = [&](int index) -> int {
if (index == 0 || index == n - 1) {
return 0;
}
if (nums[index] > nums[index - 1] && nums[index] > nums[index + 1]) {
return 1;
}
return 0;
};
for (int i = 0; i < n; i++) {
st.tree[i + n] = is_peak(i);
}
st.build();
vector<int> ans;
for (auto &query: queries) {
int op = query[0];
if (op == 1) {
int l = query[1], r = query[2];
ans.push_back(st.query(l + 1, r));
} else if (op == 2) {
int index = query[1], val = query[2];
nums[index] = val;
st.modify(index, is_peak(index));
if (index + 1 < n) {
st.modify(index + 1, is_peak(index + 1));
}
if (index - 1 >= 0) {
st.modify(index - 1, is_peak(index - 1));
}
} else {
assert(false);
}
}
return ans;
}
};