POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

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

操作:实现查询[a,b]中最大值与最小值的差值。
按理说ST要比线段树快,但是st跑下来用了3000多ms…
两个query操作一个实现查找最大值,一个实现查找最小值,最后两者一减就行了。

#include 
#include 
using namespace std;
const int maxn = 200010;
int n;
int s[maxn];
int dpmin[maxn][20];
int dpmax[maxn][20];
int minquery(int a,int b)
{
    int k = 0;
    while(1<<(k+1) <= b-a+1) k++;
    return min(dpmin[a][k],dpmin[b-(1<1][k]);
}
int maxquery(int a,int b)
{
    int k = 0;
    while(1<<(k+1) <= b-a+1) k++;
    return max(dpmax[a][k],dpmax[b-(1<1][k]);
}
int main()
{
    int q;
    scanf("%d%d",&n,&q);
    for(int i = 1 ; i <= n ; i ++) scanf("%d",&s[i]);
    for(int i = 1 ; i <= n ; i ++) dpmin[i][0] = s[i];
    for(int j = 1 ; (1<for(int i = 1 ; i+(2<<(j-1))-1 <= n ; i ++) {
            dpmin[i][j] = min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
        }
    }
    for(int i = 1 ; i <= n ; i ++) dpmax[i][0] = s[i];
    for(int j = 1 ; (1<for(int i = 1 ; i+(2<<(j-1))-1 <= n ; i ++) {
            dpmax[i][j] = max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
        }
    }

    while(q--) {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",maxquery(a,b)-minquery(a,b));
    }
    return 0;
}

线段树代码

#include 
#include 
using namespace std;
const int maxn = 200010;
int n,masegTree[maxn*4+10],misegTree[maxn*4+10];
int s[maxn];
void buildma(int node,int l,int r)
{
    if(l == r) masegTree[node] = s[l];
    else {
        buildma(node*2,l,(l+r)/2);
        buildma(node*2+1,(l+r)/2+1,r);
        masegTree[node] = max(masegTree[node*2],masegTree[node*2+1]);
    }
}
void buildmi(int node,int l,int r)
{
    if(l == r) misegTree[node] = s[l];
    else {
        buildmi(node*2,l,(l+r)/2);
        buildmi(node*2+1,(l+r)/2+1,r);
        misegTree[node] = min(misegTree[node*2],misegTree[node*2+1]);
    }
}
int maquery(int a,int b,int k,int l,int r)
{
    if(l > b || r < a) return 1<<31;
    if(a <= l && r <= b) return masegTree[k];
    return max(maquery(a,b,k*2,l,(l+r)/2),maquery(a,b,k*2+1,(l+r)/2+1,r));
}
int miquery(int a,int b,int k,int l,int r)
{
    if(l > b || r < a) return 1<<31-1;
    if(a <= l && r <= b) return misegTree[k];
    return min(miquery(a,b,k*2,l,(l+r)/2),miquery(a,b,k*2+1,(l+r)/2+1,r));
}
int main()
{
    int q;
    scanf("%d%d",&n,&q);
    for(int i = 1 ; i <= n ; i ++) scanf("%d",&s[i]);
    for(int i = 1 ; i < 4*maxn+10 ; i ++) {
        masegTree[i] = 1<<31;
        misegTree[i] = 1<<31-1;
    }
    buildma(1,1,n);
    buildmi(1,1,n);
    while(q--) {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",maquery(a,b,1,1,n)-miquery(a,b,1,1,n));
    }
    return 0;
}

你可能感兴趣的:(算法与数据结构/ACM)