• CQUOJ 9920 Ladder


    You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original array, that is, the sequence of numbers ali, ali + 1, ali + 2, ..., ari. For each query you should check whether the corresponding segment is a ladder.

    A ladder is a sequence of integers b1, b2, ..., bk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b1 ≤ b2 ≤ ... ≤ bx ≥ bx + 1 ≥ bx + 2... ≥ bk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.

     

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of array elements and the number of queries. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where number ai stands for the i-th array element.

    The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the subsegment of the initial array.

    The numbers in the lines are separated by single spaces.

     

    Output

    Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.

     

    Sample Input

    Input
    8 6
    1 2 1 3 3 5 2 1
    1 3
    2 3
    2 4
    8 8
    1 4
    5 8
    Output
    Yes
    Yes
    No
    Yes
    No
    Yes
     1 /*
     2 2016年4月22日21:37:37 
     3     题意: 给出一个序列,m次查询,每次给出一个子串,
     4         问这个子串是否满足,中间能够找到一个元素,
     5         让这个元素作为前后分别单调的分界. 
     6     预处理 向左向右分别扫一遍,记录某一个点向右或向左 离的最近的的那个最大数的位置 
     7     然后就可以判断了 好好想想 
     8     样例  1 2 1 4 5 5 2 1
     9     样例  1 2 1 4 5 4 5 1
    10 */
    11 # include <iostream>
    12 # include <cstdio>
    13 # include <cstring>
    14 # include <algorithm>
    15 # include <queue>
    16 # include <vector>
    17 # include <cmath>
    18 # define INF 0x3f3f3f3f
    19 using namespace std;
    20 const int N = 1e5;
    21 int a[N], R[N], L[N];
    22 
    23 int main(void)
    24 {
    25     int n, m, i, l, r;
    26     while (~scanf("%d %d", &n, &m))
    27     {
    28         for (i = 1; i <= n; i++)
    29             scanf("%d", &a[i]);
    30         R[n] = n;
    31         for (i = n-1; i >= 1; i--){
    32             if (a[i] > a[i+1]) R[i] = i;
    33             else R[i] = R[i+1];
    34         }
    35         L[1] = 1;
    36         for (i = 2; i <= n; i++){
    37             if (a[i] > a[i-1]) L[i] = i;
    38             else L[i] = L[i-1];
    39         }
    40         while (m--){
    41             scanf("%d %d", &l, &r);
    42             if (L[r] <= R[l])        
    43                 printf("Yes
    ");     
    44             else printf("No
    ");     
    45         }
    46     }
    47     
    48     return 0;    
    49 }
  • 相关阅读:
    [组件封装] 微信小程序
    发布一个npm包(webpack loader)
    深入理解JavaScript隐式类型转换(详解 +
    通过nginx代理之后,获取客户端ip
    IntelliJ Idea 常用快捷键列表
    Example For maven-compiler-plugin
    maven 使用tomcat插件 自动化部署war
    jsoup: Java HTML Parser (类似jquery)
    Export Data from mysql Workbench 6.0
    安装openJDK 8
  • 原文地址:https://www.cnblogs.com/hyq123456/p/5422885.html
Copyright © 2020-2023  润新知