-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathpeaks-in-array.cpp
148 lines (130 loc) · 3.73 KB
/
peaks-in-array.cpp
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
// Time: O(n + qlogn)
// Space: O(n)
// bit, fenwick tree
class Solution {
public:
vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {
const auto& check = [&](int i) {
return nums[i - 1] < nums[i] && nums[i] > nums[i + 1];
};
vector<int> arr(size(nums));
for (int i = 0; i < size(nums); ++i) {
if (1 <= i && i <= size(nums) - 2 && check(i)) {
arr[i] = 1;
}
}
BIT bit(arr);
const auto& update = [&](int x, int d) {
for (int i = max(x - 1, 1); i <= min(x + 1, static_cast<int>(size(nums)) - 2); ++i) {
if (check(i)) {
bit.add(i, d);
}
}
};
vector<int> result;
for (const auto& q : queries) {
const int t = q[0], x = q[1], y = q[2];
if (t == 1) {
result.emplace_back(y - 1 >= x + 1 ? bit.query(y - 1) - bit.query((x + 1) - 1) : 0);
continue;
}
update(x, -1);
nums[x] = y;
update(x, +1);
}
return result;
}
private:
class BIT {
public:
BIT(const vector<int>& nums) : bit_(size(nums) + 1) { // 0-indexed
for (int i = 1; i < bit_.size(); ++i) {
bit_[i] = nums[i - 1] + bit_[i - 1];
}
for (int i = bit_.size() - 1; i >= 1; --i) {
int last_i = i - lower_bit(i);
bit_[i] -= bit_[last_i];
}
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
};
// Time: O(nlogn + qlogn)
// Space: O(n)
// bit, fenwick tree
class Solution2 {
public:
vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {
const auto& check = [&](int i) {
return nums[i - 1] < nums[i] && nums[i] > nums[i + 1];
};
BIT bit(size(nums));
for (int i = 1; i + 1 < size(nums); ++i) {
if (check(i)) {
bit.add(i, +1);
}
}
const auto& update = [&](int x, int d) {
for (int i = max(x - 1, 1); i <= min(x + 1, static_cast<int>(size(nums)) - 2); ++i) {
if (check(i)) {
bit.add(i, d);
}
}
};
vector<int> result;
for (const auto& q : queries) {
const int t = q[0], x = q[1], y = q[2];
if (t == 1) {
result.emplace_back(y - 1 >= x + 1 ? bit.query(y - 1) - bit.query((x + 1) - 1) : 0);
continue;
}
update(x, -1);
nums[x] = y;
update(x, +1);
}
return result;
}
private:
class BIT {
public:
BIT(int n) : bit_(n + 1) { // 0-indexed
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
};