【RMQ】Balanced Lineup

原创 2012年03月21日 18:51:28

今天终于把RMQ学了,st算法

入门题目

 

    BalancedLineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17371   Accepted: 8044
Case TimeLimit: 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 Johndecides to organize a game of Ultimate Frisbee with some of thecows. To keep things simple, he will take a contiguous range ofcows from the milking lineup to play the game. However, for all thecows 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 thedifference in height between the shortest and the tallest cow inthe group.

Input

Line 1: Two space-separatedintegers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer thatis the height of cowi
Lines N+2..N+Q+1: Two integers A andB (1 ≤ABN), representing therange of cows from A toB inclusive.

Output

Lines 1..Q: Each linecontains a single integer that is a response to a reply andindicates the difference in height between the tallest and shortestcow in the range.

Sample Input

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

Sample Output

6
3
0


POJ不能用log2,不知道为什么,于是手打了一个,应该还快点。

inline long log2(long a)
{
    long ans = 0;
    while (a>>=1){ans++;}
    return ans;
}

#include <iostream>
//#include <cmath>
using std::cout;
using std::cin;
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
long n;long q;
long f[50010][20];
long f2[50010][20];

inline long log2(long a)
{
    long ans = 0;
    while (a>>=1){ans++;}
    return ans;
}

long gmax(long l,long r)
{
    long k = long(log2(r-l+1));
    
    return Max(f[l][k],f[r-(1<<k)+1][k]);
}

long gmin(long l,long r)
{
    long k = long(log2(r-l+1));
    
    return Min(f2[l][k],f2[r-(1<<k)+1][k]);    
}

int main()
{
    scanf("%ld%ld",&n,&q);
    for (long i=1;i<n+1;i++)
    {
        long h;
        scanf("%ld",&h);        
        f[i][0] = h;
        f2[i][0] = h;
    }
    for (long j=1;(1<<j)<=n;j++)
        for (long i=1;i<=n-(1<<j)+1;i++)
        {
            f[i][j] = Max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
            f2[i][j] = Min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
        }
    for (long i=1;i<q+1;i++)
    {
        long a;long b;
        scanf("%ld%ld",&a,&b);
        printf("%ld\n",gmax(a,b)-gmin(a,b));
    }
    return 0;
}


Balanced Lineup (基本线段树)

点击打开链接 1、题目大意: 有N头牛,给定N头牛的高度,输出给定区间内最高牛的高度与最低牛的高度差   输入:第一行两个数N、Q分别代表N头牛,Q个操作区间 接下来输入N个数,代表N头牛的...
  • sdjzping
  • sdjzping
  • 2013年01月29日 21:13
  • 733

POJ 3264 Balanced Lineup (ST算法入门)

转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove 题目:查询区间的最大值和最...
  • ACM_cxlove
  • ACM_cxlove
  • 2012年09月03日 18:31
  • 1730

BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树
  • wzq_QwQ
  • wzq_QwQ
  • 2015年08月18日 21:53
  • 983

【POJ】【P3264】【Balanced Lineup】【题解】【ST表】

传送门:http://poj.org/problem?id=3264 题意:给定长度为n的序列,m次询问区间[l,r]的最大值与最小值的差 题解:裸的RMQ问题,果断ST表破之 /* ID:i...
  • u012732945
  • u012732945
  • 2014年02月05日 17:18
  • 778

poj 3264 Balanced Lineup(简单线段树 或 rmq)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Tota...
  • u014737310
  • u014737310
  • 2015年02月26日 11:30
  • 780

bzoj1637 [Usaco2007 Mar]Balanced Lineup

Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,...
  • zhb1997
  • zhb1997
  • 2014年07月29日 23:20
  • 340

poj_3264 Balanced Lineup(RMQ)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 50087 ...
  • christry_stool
  • christry_stool
  • 2017年01月16日 19:48
  • 74

3264 Balanced Lineup //rmq

Balanced LineupTime Limit: 5000MS Memory Limit: 65536KTotal Submissions: 16743 Accepted: 7758Case Ti...
  • hqd_acm
  • hqd_acm
  • 2011年04月14日 18:51
  • 421

POJ-3264 Balanced Lineup【RMQ】

转载地址:http://blog.csdn.net/niushuai666/article/details/7401403
  • happy309best
  • happy309best
  • 2014年11月08日 17:23
  • 126

poj--3264 Balanced Lineup(RMQ)

poj 3264题解RMQ练手。#include #include #include #include using namespace std;const int maxn = 50000 +...
  • xushao_Movens
  • xushao_Movens
  • 2016年03月19日 22:01
  • 202
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【RMQ】Balanced Lineup
举报原因:
原因补充:

(最多只允许输入30个字)