1817A.md (2139B)
1 Source: https://codeforces.com/problemset/problem/1817/A 2 3 A. Almost Increasing Subsequence 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 A sequence is almost-increasing if it does not contain three consecutive elements $x, y, z$ such that $x\ge y\ge z$. 22 23 You are given an array $a_1, a_2, \dots, a_n$ and $q$ queries. 24 25 Each query consists of two integers $1\le l\le r\le n$. For each query, find the length of the longest almost-increasing subsequence of the subarray $a_l, a_{l+1}, \dots, a_r$. 26 27 A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. 28 29 Input 30 31 The first line of input contains two integers, $n$ and $q$ ($1 \leq n, q \leq 200\,000$) — the length of the array $a$ and the number of queries. 32 33 The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the values of the array $a$. 34 35 Each of the next $q$ lines contains the description of a query. Each line contains two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) — the query is about the subarray $a_l, a_{l+1}, \dots, a_r$. 36 37 Output 38 39 For each of the $q$ queries, print a line containing the length of the longest almost-increasing subsequence of the subarray $a_l, a_{l+1}, \dots, a_r$. 40 41 Example 42 43 Input 44 45 ``` 46 9 8 47 1 2 4 3 3 5 6 2 1 48 1 3 49 1 4 50 2 5 51 6 6 52 3 7 53 7 8 54 1 8 55 8 8 56 ``` 57 58 Output 59 60 ``` 61 3 62 4 63 3 64 1 65 4 66 2 67 7 68 1 69 ``` 70 71 Note 72 73 In the first query, the subarray is $a_1, a_2, a_3 = [1,2,4]$. The whole subarray is almost-increasing, so the answer is $3$. 74 75 In the second query, the subarray is $a_1, a_2, a_3,a_4 = [1,2,4,3]$. The whole subarray is a almost-increasing, because there are no three consecutive elements such that $x \geq y \geq z$. So the answer is $4$. 76 77 In the third query, the subarray is $a_2, a_3, a_4, a_5 = [2, 4, 3, 3]$. The whole subarray is not almost-increasing, because the last three elements satisfy $4 \geq 3 \geq 3$. An almost-increasing subsequence of length $3$ can be found (for example taking $a_2,a_3,a_5 = [2,4,3]$ ). So the answer is $3$.