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 <cstdio>
#include <algorithm>
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<<k)+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<<k)+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<<j) <= n ; j ++) {
        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<<j) <= n ; j ++) {
        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 <cstdio>
#include <algorithm>
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;
}
时间: 2024-08-02 02:49:52

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用的相关文章

POJ-3264 Balanced Lineup(线段树)

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

POJ3264 Balanced Lineup 线段树区间最大值 最小值

Q个数 问区间最大值-区间最小值 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #i

【POJ】3264 Balanced Lineup ——线段树 区间最值

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

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

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

poj3264 Balanced Lineup(树状数组)

题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: 30135 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 Farm

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

POJ 3264 Balanced Lineup (线段树)

Balanced Lineup 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

POJ 3264 Balanced Lineup 线段树 第三题

Balanced Lineup 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

POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值. #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algori