Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 42929 | Accepted: 20184 | |
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 31734251 54 62 2
Sample Output
630
Source
题意:区间最大值与最小值之差RMQ版:()
#include#include #include #include using namespace std;#define N 50010int a[N];int max_dp[N][20];int min_dp[N][20];int MAX(int i,int j){ if(i>=j) return i; return j;}int MIN(int i,int j){ if(i<=j) return i; return j;}void init_MAX_RMQ(int n){ for(int i=1;i<=n;i++) max_dp[i][0]=a[i]; for(int j=1;(1< <=n;j++){ for(int i=1;i<=n-(1<
线段树:
#include#include #include #include using namespace std;#define N 50010struct Tree{ int l,r; int Max,Min;}tree[4*N];int a[N];int MAX_VALUE;int MIN_VALUE;int MAX(int i,int j){ if(i>=j) return i; return j;}int MIN(int i,int j){ if(i<=j) return i; return j;}void PushUp(int idx){ tree[idx].Max = MAX(tree[idx<<1].Max,tree[idx<<1|1].Max); tree[idx].Min = MIN(tree[idx<<1].Min,tree[idx<<1|1].Min);}void build(int l,int r,int idx){ tree[idx].l = l; tree[idx].r = r; if(l==r) { tree[idx].Max = tree[idx].Min = a[l]; return ; } int mid=(l+r)>>1; build(l,mid,idx<<1); build(mid+1,r,idx<<1|1); PushUp(idx);}void query(int l,int r,int idx){ if(tree[idx].l==l&&tree[idx].r==r){ MAX_VALUE = MAX(MAX_VALUE,tree[idx].Max); MIN_VALUE = MIN(MIN_VALUE,tree[idx].Min); return; } int mid=(tree[idx].l+tree[idx].r)>>1; if(mid>=r) query(l,r,idx<<1); else if(mid