POJ3264 Balanced Lineup

Posted SilverNebula

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3264 Balanced Lineup相关的知识,希望对你有一定的参考价值。

 

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

 

ST算法求区间内最值

 

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 const int mxn=60000;
 8 int n,Q;
 9 int h[mxn];
10 int fmx[mxn][20],fmi[mxn][20];//f[i][j]表示从i开始到i*(1<<j)范围内的目标值 
11 int ST(int a,int b){//ST算法,倍增求区间最值 
12     int i,j;
13     for(i=1;i<=n;i++){//自身 
14         fmx[i][0]=fmi[i][0]=h[i];
15     }
16     int k=(int)(log(n*1.0)/log(2.0));//k=以2为底n的对数 
17     for(i=1;i<=k;i++){//第一层为次数 
18         for(j=1;j<=n;j++){//第二层为范围 
19             fmx[j][i]=fmx[j][i-1];
20             if(j+(1<<(i-1)) <=n)
21                 fmx[j][i]=max(fmx[j][i],fmx[j+(1<<(i-1))][i-1]);
22             fmi[j][i]=fmi[j][i-1];
23             if(j+(1<<(i-1)) <=n)
24                 fmi[j][i]=min(fmi[j][i],fmi[j+(1<<(i-1))][i-1]);
25         }
26     }
27     return 0;
28 }
29 int ansmx(int a,int b){//查找最大值 
30     int k=(int)(log(b-a+1.0)/log(2.0));
31     return max(fmx[a][k],fmx[b-(1<<k)+1][k]);
32 }
33 int ansmi(int a,int b){//查找最小值 
34     int k=(int)(log(b-a+1.0)/log(2.0));
35     return min(fmi[a][k],fmi[b-(1<<k)+1][k]);
36 }
37 int main(){
38     scanf("%d%d",&n,&Q);
39     int i,j;
40     for(i=1;i<=n;i++){
41         scanf("%d",&h[i]);
42     }
43     int a,b;
44     ST(1,n);
45     for(i=1;i<=Q;i++){
46         scanf("%d%d",&a,&b);
47         printf("%d\n",ansmx(a,b)-ansmi(a,b));//输出询问区间内最大值和最小值的差 
48     }
49     return 0;
50 }

 

以上是关于POJ3264 Balanced Lineup的主要内容,如果未能解决你的问题,请参考以下文章

poj 3264 Balanced Lineup

POJ3264 Balanced Lineup

poj3264_Balanced Lineup

Balanced Lineup POJ - 3264

POJ 3264 Balanced Lineup

POJ 3264 Balanced Lineup