POJ Balanced Lineup(RMQ_ST)

题目链接:http://poj.org/problem?id=3264

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 ≤ 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

Source

USACO 2007 January Silver

PS:

百度百科RMQ:http://baike.baidu.com/view/1536346.htm?fr=aladdin

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
const int MAXN = 100117;
int n,query;
int num[MAXN];

int F_Min[MAXN][20],F_Max[MAXN][20];

void Init()
{
    for(int i = 1; i <= n; i++)
    {
        F_Min[i][0] = F_Max[i][0] = num[i];
    }

    for(int i = 1; (1<<i) <= n; i++)  //按区间长度递增顺序递推
    {
        for(int j = 1; j+(1<<i)-1 <= n; j++)  //区间起点
        {
            F_Max[j][i] = max(F_Max[j][i-1],F_Max[j+(1<<(i-1))][i-1]);
            F_Min[j][i] = min(F_Min[j][i-1],F_Min[j+(1<<(i-1))][i-1]);
        }
    }
}

int Query_max(int l,int r)
{
    int k = (int)(log(double(r-l+1))/log((double)2));
    return max(F_Max[l][k], F_Max[r-(1<<k)+1][k]);
}

int Query_min(int l,int r)
{
    int k = (int)(log(double(r-l+1))/log((double)2));
    return min(F_Min[l][k], F_Min[r-(1<<k)+1][k]);
}

int main()
{
    int a,b;
    scanf("%d %d",&n,&query);
    for(int i = 1; i <= n; i++)
        scanf("%d",&num[i]);
    Init();
    while(query--)
    {
        scanf("%d %d",&a,&b);
        //printf("区间%d到%d的最大值为:%d\n",a,b,Query_max(a,b));
        //printf("区间%d到%d的最小值为:%d\n",a,b,Query_min(a,b));
        printf("%d\n",Query_max(a,b)-Query_min(a,b));
    }
    return 0;
}
时间: 2024-12-28 11:24:17

POJ Balanced Lineup(RMQ_ST)的相关文章

POJ 3264 Balanced Lineup(RMQ)

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

POJ 题目3264 Balanced Lineup(RMQ)

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

Balanced Lineup(RMQ)

原题传送门 就是裸RMQ啊.. 求区间最大值和区间最小值,一看就像RMQ,当然线段树貌似也可以. 至于算法嘛.自己学~(好吧,放个传送门...) 然后就是最后把maxsum-minsum就好啦233~ 时间效率:预处理O(N)查找O(1) 是不是很快~ 下面贴代码 #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int n

POJ 3264-Balanced Lineup(线段树:单点更新+区间查询)

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

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差. 分析:按区间取值,很明显使用的线段树.区间大小取200000 * 4 = 8 * 10 ^5; 进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root].L判断,多余了, 逻辑不正确. #include <iostream> #include <stdio.h> #inc

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

POJ 1840 Eqs(暴力)

Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)