Input: nums =[3,1,4,2,5], queries =[[2,3,4],[1,0,4]]Output: [0]Explanation:
First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`.Second query: The number of peaks in the `[3,1,4,4,5]`is0.
Input: nums =[4,1,4,2,1,5], queries =[[2,2,4],[1,0,2],[1,0,4]]Output: [0,1]Explanation:
First query:`nums[2]` should become 4, but it is already set to 4.Second query: The number of peaks in the `[4,1,4]`is0.Third query: The second 4is a peak in the `[4,1,4,2,1]`.
We need to efficiently support range peak queries and point updates. A peak is an element greater than its neighbors, and only elements at positions 1..n-2 can be peaks. We can use a Binary Indexed Tree (Fenwick Tree) or Segment Tree to maintain a binary array where 1 means the position is a peak, and 0 otherwise. For updates, only the updated index and its neighbors can change peak status.
#include<vector>usingnamespace std;
classFenwick {
vector<int> bit; int n;
public: Fenwick(int n): bit(n+2), n(n+2) {}
voidadd(int i, int x) { for (++i; i < n; i += i&-i) bit[i] += x; }
intsum(int i) { int s =0; for (++i; i >0; i -= i&-i) s += bit[i]; return s; }
intrange(int l, int r) { return sum(r) - sum(l-1); }
};
classSolution {
public: vector<int> processQueries(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size();
vector<int> is_peak(n, 0);
auto check = [&](int i) {
return i >0&& i < n-1&& nums[i] > nums[i-1] && nums[i] > nums[i+1];
};
for (int i =1; i < n-1; ++i) is_peak[i] = check(i);
Fenwick bit(n);
for (int i =0; i < n; ++i) if (is_peak[i]) bit.add(i, 1);
vector<int> res;
for (auto& q : queries) {
if (q[0] ==1) {
int l = q[1], r = q[2];
if (r - l <2) res.push_back(0);
else res.push_back(bit.range(l+1, r-1));
} else {
int idx = q[1], val = q[2];
nums[idx] = val;
for (int d =-1; d <=1; ++d) {
int i = idx + d;
if (i >0&& i < n-1) {
int was = is_peak[i];
int now = check(i);
if (was != now) { bit.add(i, now - was); is_peak[i] = now; }
}
}
}
}
return res;
}
};
import java.util.*;
classFenwick {
int[] bit;
Fenwick(int n) { bit =newint[n+2]; }
voidadd(int i, int x) { for (++i; i < bit.length; i += i&-i) bit[i]+= x; }
intsum(int i) { int s = 0; for (++i; i > 0; i -= i&-i) s += bit[i]; return s; }
intrange(int l, int r) { return sum(r) - sum(l-1); }
}
classSolution {
public List<Integer>processQueries(int[] nums, int[][] queries) {
int n = nums.length;
int[] isPeak =newint[n];
java.util.function.IntPredicate check = i -> i > 0 && i < n-1 && nums[i]> nums[i-1]&& nums[i]> nums[i+1];
for (int i = 1; i < n-1; i++) isPeak[i]= check.test(i) ? 1 : 0;
Fenwick bit =new Fenwick(n);
for (int i = 0; i < n; i++) if (isPeak[i]== 1) bit.add(i, 1);
List<Integer> res =new ArrayList<>();
for (int[] q : queries) {
if (q[0]== 1) {
int l = q[1], r = q[2];
if (r - l < 2) res.add(0);
else res.add(bit.range(l+1, r-1));
} else {
int idx = q[1], val = q[2];
nums[idx]= val;
for (int d =-1; d <= 1; d++) {
int i = idx + d;
if (i > 0 && i < n-1) {
int was = isPeak[i], now = check.test(i) ? 1 : 0;
if (was != now) { bit.add(i, now - was); isPeak[i]= now; }
}
}
}
}
return res;
}
}
classFenwick(n: Int) {
privateval bit = IntArray(n+2)
funadd(i: Int, x: Int) { var idx = i+1; while (idx < bit.size) { bit[idx] += x; idx += idx and -idx } }
funsum(i: Int): Int { var idx = i+1; var s = 0; while (idx > 0) { s += bit[idx]; idx -= idx and -idx }; return s }
funrange(l: Int, r: Int) = sum(r) - sum(l-1)
}
classSolution {
funprocessQueries(nums: IntArray, queries: Array<IntArray>): List<Int> {
val n = nums.size
val isPeak = IntArray(n)
funcheck(i: Int) = i > 0&& i < n-1&& nums[i] > nums[i-1] && nums[i] > nums[i+1]
for (i in1 until n-1) isPeak[i] = if (check(i)) 1else0val bit = Fenwick(n)
for (i in0 until n) if (isPeak[i] ==1) bit.add(i, 1)
val res = mutableListOf<Int>()
for (q in queries) {
if (q[0] ==1) {
val l = q[1]; val r = q[2]
if (r - l < 2) res.add(0)
else res.add(bit.range(l+1, r-1))
} else {
val idx = q[1]; val v = q[2]
nums[idx] = v
for (d in -1..1) {
val i = idx + d
if (i > 0&& i < n-1) {
val was = isPeak[i]; val now = if (check(i)) 1else0if (was != now) { bit.add(i, now - was); isPeak[i] = now }
}
}
}
}
return res
}
}
classFenwick:
def__init__(self, n):
self.n = n+2 self.bit = [0]*(n+2)
defadd(self, i, x):
i +=1while i < self.n:
self.bit[i] += x
i += i &-i
defsum(self, i):
i +=1 s =0while i >0:
s += self.bit[i]
i -= i &-i
return s
defrange(self, l, r):
return self.sum(r) - self.sum(l-1)
defprocessQueries(nums: list[int], queries: list[list[int]]) -> list[int]:
n = len(nums)
is_peak = [0]*n
defcheck(i):
return i >0and i < n-1and nums[i] > nums[i-1] and nums[i] > nums[i+1]
for i in range(1, n-1):
is_peak[i] = check(i)
bit = Fenwick(n)
for i in range(n):
if is_peak[i]: bit.add(i, 1)
res = []
for q in queries:
if q[0] ==1:
l, r = q[1], q[2]
if r - l <2:
res.append(0)
else:
res.append(bit.range(l+1, r-1))
else:
idx, val = q[1], q[2]
nums[idx] = val
for d in [-1,0,1]:
i = idx + d
if0< i < n-1:
was, now = is_peak[i], check(i)
if was != now:
bit.add(i, now-was)
is_peak[i] = now
return res
structFenwick {
bit: Vec<i32>, n: usize}
impl Fenwick {
fnnew(n: usize) -> Self { Self { bit: vec![0; n+2], n: n+2 } }
fnadd(&mut self, mut i: usize, x: i32) { i +=1; while i < self.n { self.bit[i] += x; i += i & (!i+1) } }
fnsum(&self, mut i: usize) -> i32 { i +=1; letmut s =0; while i >0 { s += self.bit[i]; i -= i & (!i+1) } s }
fnrange(&self, l: usize, r: usize) -> i32 { self.sum(r) - self.sum(l-1) }
}
fnprocess_queries(nums: &mut [i32], queries: &[Vec<i32>]) -> Vec<i32> {
let n = nums.len();
letmut is_peak =vec![0; n];
let check =|i: usize, nums: &[i32]| i >0&& i < n-1&& nums[i] > nums[i-1] && nums[i] > nums[i+1];
for i in1..n-1 { is_peak[i] = check(i, nums) asi32; }
letmut bit = Fenwick::new(n);
for i in0..n { if is_peak[i] ==1 { bit.add(i, 1); } }
letmut res =vec![];
for q in queries {
if q[0] ==1 {
let (l, r) = (q[1] asusize, q[2] asusize);
if r < l+2 { res.push(0); } else { res.push(bit.range(l+1, r-1)); }
} else {
let (idx, val) = (q[1] asusize, q[2]);
nums[idx] = val;
for d in [-1,0,1] {
let i = idx asi32+ d;
if i >0&& (i asusize) < n-1 {
let i = i asusize;
let was = is_peak[i];
let now = check(i, nums) asi32;
if was != now { bit.add(i, now-was); is_peak[i] = now; }
}
}
}
}
res
}