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
思路:RMQ算法是一个快速求区间最值的算法,与处理时间复杂度O(n*log(n)),查询O(1),是一个很快速的算法。
同时还可以用线段树解决,算法复杂度为:O(N)~O(logN)。
RMQ:
#include
#include
using namespace std;
const int MAX = 5e4 + 5;
int cows[MAX];
int st_max[MAX][20];
int st_min[MAX][20];
int n, q, l, r;
void initst_max (){
for (int i = 1; i <= n; i++){ //这里要跟主函数中输入的i从一开始同步
st_max[i][0] = st_min[i][0] = cows[i]; //这里两个都要初始化
}
for (int j = 1; j < 20; j++){ //外层是j
for (int i = 1; i <= n; i++){
if (i + (1 << j) - 1 <= n){
st_max[i][j] = max(st_max[i][j - 1], st_max[i + (1 << (j - 1))][j - 1]);
st_min[i][j] = min(st_min[i][j - 1], st_min[i + (1 << (j - 1))][j - 1]);
}
}
}
}
int queryst_max (int l, int r){
int k = log2(r - l + 1); //重点
return max(st_max[l][k], st_max[r - (1 << k) + 1][k]);
}
int queryst_min (int l, int r){
int k = log2(r - l + 1);
return min(st_min[l][k], st_min[r - (1 << k) + 1][k]);
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; i++){
cin >> cows[i];
}
initst_max();
while(q--){
cin >> l >> r;
cout << queryst_max(l, r) - queryst_min(l, r) << endl;
}
return 0;
}
线段树:
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
const int MAX = 2e5 + 5;
int nmax, nmin;
struct node{
int left;
int right;
int data;
int Maxn;
int Minn;
}st[MAX << 2];
void Build (int l, int r, int rt){
st[rt].left = l; //这两行必须在if外面
st[rt].right = r;
if (l == r){
cin >> st[rt].data;
st[rt].Maxn = st[rt].Minn = st[rt].data;
return;
}
int m = (l + r) >> 1;
Build(l, m, rt << 1);
Build(m + 1, r, rt << 1 | 1);
st[rt].Maxn = max(st[rt << 1].Maxn, st[rt << 1 | 1].Maxn);
st[rt].Minn = min(st[rt << 1].Minn, st[rt << 1 | 1].Minn);
}
void Query (int l, int r, int rt){
if (st[rt].left == l && st[rt].right == r){
nmax = max(nmax, st[rt].Maxn);
nmin = min(nmin, st[rt].Minn);
return ;
}
int m = (st[rt].left + st[rt].right) >> 1;
if (r <= m){
Query(l, r, rt << 1);
}
else if (l > m){
Query(l, r, rt << 1 | 1);
}
else {
Query(l, m, rt << 1);
Query(m + 1, r, rt << 1 | 1);
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
Build(1, n, 1);
// for (int i = 1; i <= 4 * n; i++){
// cout << st[i].Maxn << endl;
// }
int a, b;
while (q--){
nmax = -inf;
nmin = inf;
cin >> a >> b;
Query(a, b, 1);
cout << nmax - nmin << endl;
}
return 0;
}