#FT0000023. Đếm đỉnh trong mảng (Peaks in Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm đỉnh trong mảng (Peaks in Array)
Peaks in Array
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an integer array nums of length , with indices through . An element at index is a peak if it has both a previous and a next neighbor, and its value is strictly greater than both: nums[i] > nums[i-1] and nums[i] > nums[i+1]. Equal values do not satisfy the condition.
Process operations. Command 1 l r considers the consecutive subarray from through and counts peaks inside that subarray. Because a peak requires two neighbors, neither endpoint nor can be counted, even if it is a peak in the full array. A subarray with fewer than three elements always has answer .
Command 2 i v replaces nums[i] with ; subsequent queries must use the updated values. No elements are inserted, removed, or swapped.
Input
- Line 1:
n qgives array length and operation count. - Line 2:
nums_0 nums_1 ... nums_(n-1)gives the initial values in index order. - Next lines: each line contains one of the following commands, in execution order:
1 l r: count peaks strictly inside subarray .2 i v: assign tonums[i].
Output
For each 1 l r command, print the requested integer computed from the current state on its own line, in query order. Update commands produce no output.
Subtasks
Subtask 1 (20 points):
- .
- .
- for all initial elements and update values.
- query
1: . - update
2: .
Subtask 2 (30 points):
- .
- .
- for all initial elements and update values.
- query
1: . - update
2: .
Subtask 3 (50 points):
- .
- .
- for all initial elements and update values.
- query
1: . - update
2: .
Examples
Input
6 3
4 1 4 2 1 5
2 2 4
1 0 2
1 0 4
Output
0
1
Explanation
Initially nums = [4, 1, 4, 2, 1, 5], and there are three operations.
-
2 2 4: setnums[2]to . It was already , so the array remains[4, 1, 4, 2, 1, 5]. -
1 0 2: the subarray is[4,1,4]at indices . The only interior index is , but its value is not greater than its two neighbors, both equal to . Index is the subarray's right endpoint and cannot count as an interior peak. Print0. -
1 0 4: the subarray[4,1,4,2,1]has interior indices . Index is a peak because and . Neither index nor index is a peak. Exactly one peak exists; print1.