• poj 3264 RMQ


    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 44075   Accepted: 20687
    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

    Source

     
    题意: 求区间最大最小值 输出差值
     
    题解: RMQ问题 裸题   dp思想+位运算
         
              基础问题码一遍  f[i][j] 代表 以第i个数为起点 长度为2^j次方的区间的最小值
              查询操作中的以左右边界为起点取并集 输出ans
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<map>
     8 #define ll __int64
     9 using namespace std;
    10 int n,q;
    11 int f[50005][30];
    12 int ff[50005][30];
    13 int l,r;
    14 void rmq_init()
    15 {
    16     for(int j=1;(1<<j)<=n;j++)
    17     for(int i=1;i+(1<<(j))-1<=n;i++)
    18     {
    19         f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
    20         ff[i][j]=max(ff[i][j-1],ff[i+(1<<(j-1))][j-1]);
    21         //cout<<f[i][j]<<endl;
    22     }
    23     
    24 }
    25 int rmq(int aa,int bb)
    26 {
    27     int k=0;
    28     int ans1,ans2;
    29     while((1<<(k+1))<=bb-aa+1)
    30     k++;
    31     ans1=min(f[aa][k],f[bb-(1<<k)+1][k]);
    32     ans2=max(ff[aa][k],ff[bb-(1<<k)+1][k]);
    33     return ans2-ans1;
    34 }
    35 int main()
    36 {
    37     while(scanf("%d %d",&n,&q)!=EOF)
    38     {
    39         for(int i=1;i<=n;i++)
    40             {
    41             scanf("%d",&f[i][0]);
    42             ff[i][0]=f[i][0];
    43             }
    44         rmq_init();
    45         for(int i=1;i<=q;i++)
    46          {
    47            scanf("%d %d",&l,&r);
    48            cout<<rmq(l,r)<<endl;
    49          }    
    50     }
    51     return 0;
    52  } 
     
  • 相关阅读:
    一篇说为什么有些人穷,有人却可以致富的亲身经历的文章
    jquery的隐式类型转换
    git学习笔记02-创建一个仓库提交一个文件-原来就是这么简单
    git学习笔记01-git最基本的工作原理分布式
    git-github-svn你们都是个什么东东
    工作中遇到的问题,虽然很白痴但是你可能也会遇到-2016-10-24
    你只有非常努力,才会看起起来毫不费力!
    jQuery的.click,.bind,.unbind,.on,.off,.delegate,.undelegate
    jQuery里面的普通绑定事件和on委托事件
    Phantomjs 在cmd命令行显示中文乱码
  • 原文地址:https://www.cnblogs.com/hsd-/p/5525606.html
Copyright © 2020-2023  润新知