• Balanced Lineup 倍增思想到ST表RMQ


     
                                Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 36864   Accepted: 17263
    Case Time Limit: 2000MS

    Description

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

    Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

    Input

    Line 1: Two space-separated integers, N and Q.
    Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
    Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

    Output

    Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

    Sample Input

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    Sample Output

    6
    3
    0


    题目抽象:求数组区间[a,b]之间的最大值与最小值的差。



     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 const int INF=0x4fffffff;
     8 const int MS=50005;
     9 
    10 int a[MS];
    11 int minv[MS][20];
    12 int maxv[MS][20];
    13 
    14 int N,Q;
    15 
    16 void RMQ_init()
    17 {
    18     for(int i=0;i<N;i++)
    19         minv[i][0]=maxv[i][0]=a[i];
    20     for(int j=1;(1<<j)<=N;j++)
    21     {
    22         for(int i=0;i+(1<<j)-1<N;i++)
    23         {
    24             minv[i][j]=min(minv[i][j-1],minv[i+(1<<(j-1))][j-1]);
    25             maxv[i][j]=max(maxv[i][j-1],maxv[i+(1<<(j-1))][j-1]);
    26         }
    27     }
    28 }
    29 
    30 int query_min(int l,int r)
    31 {
    32     int k=0;
    33     while((1<<(k+1))<(r-l+1))
    34         k++;
    35     return min(minv[l][k],minv[r-(1<<k)+1][k]);
    36 }
    37 
    38 int query_max(int l,int r)
    39 {
    40     int k=0;
    41     while((1<<(k+1))<(r-l+1))
    42         k++;
    43     return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
    44 }
    45 
    46 int main()
    47 {
    48     scanf("%d%d",&N,&Q);
    49     for(int i=0;i<N;i++)
    50         scanf("%d",&a[i]);
    51     RMQ_init();
    52     while(Q--)
    53     {
    54         int l,r;
    55         scanf("%d%d",&l,&r);
    56         l--;
    57         r--;
    58         int max_v=query_max(l,r);
    59         int min_v=query_min(l,r);
    60         printf("%d
    ",max_v-min_v);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    【转载】Lua中实现类的原理
    游戏资源压缩
    lua中的继承
    lua滚动文字效果
    【转】IOS版本自定义字体步骤
    luaj luaoc 回调函数传递的一些小总结
    cocos2dx中启用lua脚本
    Lua中调用C++方法
    cocos2dx 某缩放的页面 CCTableView最后一个标签无法点中
    C++ Vector 中自定义对象的排序
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4307898.html
Copyright © 2020-2023  润新知