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

求区间最大最小值差

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+7;
const double PI = acos(-1.0);
const double eps = 1e-6 ;
const int N =  50010;
const int mod = 2333333;
int dp_m[N][20] , dp_M[N][20] , mm[N] , b[N] , n , m ;
void initRMQ( int n ) {
    mm[0] = -1 ;
    for( int i = 1 ; i <= n ; ++i ){
        mm[i] = ( (i&(i-1)) ==0 )?mm[i-1]+1:mm[i-1];
        dp_m[i][0]=dp_M[i][0]=b[i];
    }
    for(int j = 1 ; j <= mm[n] ; ++j ) {
        for( int i = 1 ; i+(1<<j)-1<=n ; ++i ) {
            dp_M[i][j] = max( dp_M[i][j-1] ,dp_M[i+(1<<(j-1))][j-1]);
            dp_m[i][j] = min( dp_m[i][j-1] ,dp_m[i+(1<<(j-1))][j-1]);
        }
    }
}
int rmq( int x , int y ){
    int k = mm[y-x+1];
    return max(dp_M[x][k],dp_M[y-(1<<k)+1][k]) - min(dp_m[x][k],dp_m[y-(1<<k)+1][k]) ;
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
//        freopen("out.txt","w",stdout);
    #endif // LOCAL
    int _ ,x ,y , c ;
    while( ~scanf("%d%d",&n,&m) ) {
        for( int i =1 ; i <= n ; ++i ){
            scanf("%d",&b[i]);
        }
        initRMQ(n);
        while(m--) {
            scanf("%d%d",&x,&y);
            printf("%d\n",rmq(x,y));
        }
    }
}

时间: 2024-08-07 09:31:04

POJ 3264 Balanced Lineup(RMQ)的相关文章

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

POJ 3264 Balanced Lineup (线段树||RMQ)

A - Balanced Lineup Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always li

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

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36833   Accepted: 17252 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.

POJ 3264 Balanced Lineup(st或者线段树)

A - Balanced Lineup Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 Appoint description:  System Crawler  (2015-08-03) Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00

POJ - 3264——Balanced Lineup(入门线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 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(最大最小差值 线段树水题)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37122   Accepted: 17383 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.

POJ - 3264 - Balanced Lineup (线段树)

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