zoukankan      html  css  js  c++  java
  • 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  } 
     
  • 相关阅读:
    jzoj 6278. 2019.8.5【NOIP提高组A】跳房子
    2019.08.05【NOIP提高组】模拟 A 组 总结
    HTML笔记
    html中的锚点设置
    前端HTML
    数据库设计(第一范式,第二范式,第三范式)
    MySQL之锁、事务、优化、OLAP、OLTP
    MySQL数据备份与还原(mysqldump)
    MySQl创建用户和授权
    MySQL之索引原理与慢查询优化
  • 原文地址:https://www.cnblogs.com/hsd-/p/5525606.html
Copyright © 2011-2022 走看看