zoukankan      html  css  js  c++  java
  • Day6

    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.. NQ+1: Two integers A and B (1 ≤ A ≤ B ≤ N), 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

    思路:ST表板子题,ST[i][j]表示下表从i到i+2^j-1的最值,查询时,已知l与r,长度len=r-l+1,且2^log2(len)>len/2,令k=log2(len),ST[l][k]肯定超过了长度的一半,反向取后侧,r-m+1=2^len,另一侧就是ST[r-2^k+1][k]
    const int maxm = 5e4+10;
    
    int Max[maxm][20], Min[maxm][20], N, Q;
    
    int main() {
        scanf("%d%d", &N, &Q);
        int t, l, r;
        for(int i = 1; i <= N; ++i) {
            scanf("%d", &t);
            Max[i][0] = Min[i][0] = t;
        }
        for(int k = 1; (1<<k) <= N; ++k) {
            for(int i = 1; i+(1<<k)-1 <= N; ++i) {
                Max[i][k] = max(Max[i][k-1], Max[i+(1<<(k-1))][k-1]);
                Min[i][k] = min(Min[i][k-1], Min[i+(1<<(k-1))][k-1]);
            }
        }
        for(int i = 0; i < Q; ++i) {
            scanf("%d%d", &l, &r);
            int k = log((double)(r-l+1)) / log(2.0);
            printf("%d
    ", max(Max[l][k],Max[r-(1<<k)+1][k]) - min(Min[l][k], Min[r-(1<<k)+1][k]));
        }
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    js简直了,连大小都不能直接比,还变量提升,这个是挖了多少坑啊,故意的把,,,,写起来又不简单,运行起来又不是很稳,很迷啊
    bootstrap 强制左移
    图片轮播/无缝滚动的原理
    jQuery放大镜的原理
    下拉列表的两种实现方式
    Html css图片文本对齐问题
    一键换肤原理
    setInterval与clearInterval用法
    jQuery课堂测验
    Css画圣诞树
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12198916.html
Copyright © 2011-2022 走看看