标签:
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
Output
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)); } } }
标签:
原文地址:http://www.cnblogs.com/hlmark/p/4248353.html