poj 3264 RMQ
Posted 半根毛线code
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3264 RMQ相关的知识,希望对你有一定的参考价值。
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
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
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
Source
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 }
以上是关于poj 3264 RMQ的主要内容,如果未能解决你的问题,请参考以下文章
poj 3264 Balanced Lineup RMQ问题