CF1817A.Almost Increasing Subsequence

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A sequence is almost-increasing if it does not contain three consecutive elements x,y,zx, y, z such that x≥y≥zx\ge y\ge z.

You are given an array a1,a2,…,ana_1, a_2, \dots, a_n and qq queries.

Each query consists of two integers 1≤l≤r≤n1\le l\le r\le n. For each query, find the length of the longest almost-increasing subsequence of the subarray al,al+1,…,ara_l, a_{l+1}, \dots, a_r.

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.

如果一个序列不包含三个连续元素 x,y,zx, y, z 满足 x≥y≥zx\ge y\ge z,则称该序列为近似递增序列(almost-increasing)。

给定一个数组 a1,a2,…,ana_1, a_2, \dots, a_n 和 qq 个查询。

每个查询包含两个整数 1≤l≤r≤n1\le l\le r\le n。对于每个查询,请找出子数组 al,al+1,…,ara_l, a_{l+1}, \dots, a_r 的最长近似递增子序列的长度。

子序列是指从原序列中删除零个或多个元素(不改变剩余元素的相对顺序)所得到的序列。

输入格式

The first line of input contains two integers, nn and qq (1≤n,q≤200 0001 \leq n, q \leq 200\,000) — the length of the array aa and the number of queries.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — the values of the array aa.

Each of the next qq lines contains the description of a query. Each line contains two integers ll and rr (1≤l≤r≤n1 \leq l \leq r \leq n) — the query is about the subarray al,al+1,…,ara_l, a_{l+1}, \dots, a_r.

输入的第一行包含两个整数 nn 和 qq(1≤n,q≤200 0001 \leq n, q \leq 200\,000)——分别表示数组 aa 的长度和查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \leq a_i \leq 10^9)——表示数组 aa 的各元素值。

接下来的 qq 行,每行描述一个查询。每行包含两个整数 ll 和 rr(1≤l≤r≤n1 \leq l \leq r \leq n)——该查询针对子数组 al,al+1,…,ara_l, a_{l+1}, \dots, a_r。

输出格式

For each of the qq queries, print a line containing the length of the longest almost-increasing subsequence of the subarray al,al+1,…,ara_l, a_{l+1}, \dots, a_r.

对于每个查询,输出一行,包含子数组 al,al+1,…,ara_l, a_{l+1}, \dots, a_r 的最长近似递增子序列的长度。

输入输出样例

  • 输入#1

    9 8
    1 2 4 3 3 5 6 2 1
    1 3
    1 4
    2 5
    6 6
    3 7
    7 8
    1 8
    8 8

    输出#1

    3
    4
    3
    1
    4
    2
    7
    1

说明/提示

In the first query, the subarray is a1,a2,a3=[1,2,4]a_1, a_2, a_3 = [1,2,4]. The whole subarray is almost-increasing, so the answer is 33.

In the second query, the subarray is a1,a2,a3,a4=[1,2,4,3]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≥y≥zx \geq y \geq z. So the answer is 44.

In the third query, the subarray is a2,a3,a4,a5=[2,4,3,3]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≥3≥34 \geq 3 \geq 3. An almost-increasing subsequence of length 33 can be found (for example taking a2,a3,a5=[2,4,3]a_2,a_3,a_5 = [2,4,3] ). So the answer is 33.

在第一个查询中,子数组为 a1,a2,a3=[1,2,4]a_1, a_2, a_3 = [1,2,4]。整个子数组是几乎递增的,因此答案为 33。

在第二个查询中,子数组为 a1,a2,a3,a4=[1,2,4,3]a_1, a_2, a_3,a_4 = [1,2,4,3]。整个子数组是几乎递增的,因为不存在三个连续元素满足 x≥y≥zx \geq y \geq z。因此答案为 44。

在第三个查询中,子数组为 a2,a3,a4,a5=[2,4,3,3]a_2, a_3, a_4, a_5 = [2, 4, 3, 3]。整个子数组不是几乎递增的,因为最后三个元素满足 4≥3≥34 \geq 3 \geq 3。但可以找到一个长度为 33 的几乎递增子序列(例如取 a2,a3,a5=[2,4,3]a_2,a_3,a_5 = [2,4,3])。因此答案为 33。

输入解题思路,AI测评打分。不知道怎么写?

首页