zoukankan      html  css  js  c++  java
  • 【vijos】P1514天才的记忆

     

    描述

    从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏。在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以继承他的宝藏。题目是这样的:给你一大串数字(编号为1到N,大小可不一定哦!),在你看过一遍之后,它便消失在你面前,随后问题就出现了,给你M个询问,每次询问就给你两个数字A,B,要求你瞬间就说出属于A到B这段区间内的最大数。一天,一位美丽的姐姐从天上飞过,看到这个问题,感到很有意思(主要是据说那个宝藏里面藏着一种美容水,喝了可以让这美丽的姐姐更加迷人),于是她就竭尽全力想解决这个问题。BUT,她每次都以失败告终,因为这数字的个数是在太多了!于是她请天才的你帮他解决。如果你帮她解决了这个问题,可是会得到很多甜头的哦!

    格式

    输入格式

    一个整数N表示数字的个数,接下来一行为N个数。第三行读入一个M,表示你看完那串数后需要被提问的次数,接下来M行,每行都有两个整数A,B。

    输出格式

    输出共M行,每行输出一个数。

    样例输入1

    6
    34 1 8 123 3 2
    4
    1 2
    1 5
    3 4
    2 3

    样例输出1

    34
    123
    123
    8

    RMQ
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<map>
     7 #define inf 10000000
     8 using namespace std;
     9 
    10 int n;
    11 int f[200001][22];
    12 void RMQ()
    13 {
    14 
    15     for(int j=1;j<=log(n)/log(2);j++)
    16     {
    17         for(int i=1;i<=n;i++)
    18         {
    19             f[i][j]=f[i][j-1];
    20             if(i+(1<<(j-1))<=n)
    21             {
    22                 f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
    23             }
    24         }
    25     }
    26 }
    27 int main()
    28 {
    29     int m,x,y;
    30     int a[200001];
    31     scanf("%d",&n);
    32     for(int i=1;i<=n;i++)
    33     {
    34         scanf("%d",&a[i]);
    35     }
    36     for(int i=1;i<=n;i++)
    37     {
    38         f[i][0]=a[i];
    39     }
    40     RMQ();
    41     scanf("%d",&m);
    42     for(int i=1;i<=m;i++)
    43     {
    44         scanf("%d%d",&x,&y);
    45         int k=log(y-x+1)/log(2);
    46         printf("%d
    ",max(f[x][k],f[y-(1<<k)+1][k]));
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    洛谷 P7520
    Involuting Bunny! (2021.8)
    Solution -「Gym 102979E」Expected Distance
    Solution -「Gym 102979L」 Lights On The Road
    Solution -「CodeChef JUMP」Jump Mission
    Solution -「洛谷 P4372」Out of Sorts P
    Solution -「Gym 102956F」Find the XOR
    Solution -「Gym 102956B」Beautiful Sequence Unraveling
    Solution -「Gym 102956F」Border Similarity Undertaking
    Solution -「LOJ #6029」「雅礼集训 2017」市场
  • 原文地址:https://www.cnblogs.com/zxhl/p/4655706.html
Copyright © 2011-2022 走看看