PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000023. Đếm đỉnh trong mảng (Peaks in Array)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đế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 nn, with indices 00 through n−1n-1. An element at index ii 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 qq operations. Command 1 l r considers the consecutive subarray from ll through rr and counts peaks inside that subarray. Because a peak requires two neighbors, neither endpoint ll nor rr can be counted, even if it is a peak in the full array. A subarray with fewer than three elements always has answer 00.

Command 2 i v replaces nums[i] with vv; subsequent queries must use the updated values. No elements are inserted, removed, or swapped.

Input

  • Line 1: n q gives array length and operation count.
  • Line 2: nums_0 nums_1 ... nums_(n-1) gives the initial values in index order.
  • Next qq lines: each line contains one of the following commands, in execution order:
    • 1 l r: count peaks strictly inside subarray [l,r][l,r].
    • 2 i v: assign vv to nums[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):

  • 3≤n≤243\le n\le 24.
  • 1≤q≤301\le q\le 30.
  • 1≤numsi,v≤1051\le nums_i,v\le 10^5 for all initial elements and update values.
  • query 1: 0≤l≤r<n0\le l\le r<n.
  • update 2: 0≤i<n0\le i<n.

Subtask 2 (30 points):

  • 3≤n≤12003\le n\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 1≤numsi,v≤1051\le nums_i,v\le 10^5 for all initial elements and update values.
  • query 1: 0≤l≤r<n0\le l\le r<n.
  • update 2: 0≤i<n0\le i<n.

Subtask 3 (50 points):

  • 3≤n≤1000003\le n\le 100000.
  • 1≤q≤1000001\le q\le 100000.
  • 1≤numsi,v≤1051\le nums_i,v\le 10^5 for all initial elements and update values.
  • query 1: 0≤l≤r<n0\le l\le r<n.
  • update 2: 0≤i<n0\le i<n.

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.

  1. 2 2 4: set nums[2] to 44. It was already 44, so the array remains [4, 1, 4, 2, 1, 5].

  2. 1 0 2: the subarray is [4,1,4] at indices 0,1,20,1,2. The only interior index is 11, but its value 11 is not greater than its two neighbors, both equal to 44. Index 22 is the subarray's right endpoint and cannot count as an interior peak. Print 0.

  3. 1 0 4: the subarray [4,1,4,2,1] has interior indices 1,2,31,2,3. Index 22 is a peak because 4>14>1 and 4>24>2. Neither index 11 nor index 33 is a peak. Exactly one peak exists; print 1.