Mike 发表于 2021-7-21 16:40:16

poj 3264(RMQ或者线段树)

Balanced Lineup
Time Limit: 5000MS
 
Memory Limit: 65536K
Total Submissions: 42929
 
Accepted: 20184
Case Time Limit: 2000MSDescription
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 2Sample Output
6
3
0Source
题意:区间最大值与最小值之差RMQ版:(不懂的可以参考blog)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 50010

int a;
int max_dp;
int min_dp;
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=a;
    for(int j=1;(1<<j)<=n;j++){
      for(int i=1;i<=n-(1<<j)+1;i++){
            ///F=max(F, F)。
            max_dp = MAX(max_dp,max_dp);
      }
    }
}
int MAX_RMQ(int a,int b){
    int k = (int)(log(b-a+1.0)/log(2.0));
    ///RMQ(A, i, j)=min{F,F}
    return MAX(max_dp,max_dp);
}
void init_MIN_RMQ(int n){
    for(int i=1;i<=n;i++) min_dp=a;
    for(int j=1;(1<<j)<=n;j++){
      for(int i=1;i<=n-(1<<j)+1;i++){
            min_dp = MIN(min_dp,min_dp);
      }
    }
}
int MIN_RMQ(int a,int b){
    int k = (int)(log(b-a+1.0)/log(2.0));
    return MIN(min_dp,min_dp);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
      for(int i=1;i<=n;i++){
            scanf("%d",&a);
      }
      init_MAX_RMQ(n);
      init_MIN_RMQ(n);
      while(m--){
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%d\n",MAX_RMQ(a,b)-MIN_RMQ(a,b));
      }
    }
    return 0;
}线段树:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 50010

struct Tree{
    int l,r;
    int Max,Min;
}tree;
int a;
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.Max = MAX(tree.Max,tree.Max);
    tree.Min = MIN(tree.Min,tree.Min);
}
void build(int l,int r,int idx){
    tree.l = l;
    tree.r = r;
    if(l==r) {
      tree.Max = tree.Min = a;
      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.l==l&&tree.r==r){
      MAX_VALUE = MAX(MAX_VALUE,tree.Max);
      MIN_VALUE = MIN(MIN_VALUE,tree.Min);
      return;
    }
    int mid=(tree.l+tree.r)>>1;
    if(mid>=r) query(l,r,idx<<1);
    else if(mid<l) query(l,r,idx<<1|1);
    else{
      query(l,mid,idx<<1);
      query(mid+1,r,idx<<1|1);
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
      for(int i=1;i<=n;i++){
            scanf("%d",&a);
      }
      build(1,n,1);
      while(m--){
            int b,c;
            scanf("%d%d",&b,&c);
            MAX_VALUE=-1;
            MIN_VALUE=1000001;
            query(b,c,1);
            printf("%d\n",MAX_VALUE-MIN_VALUE);
      }
    }
    return 0;
}


文档来源:51CTO技术博客https://blog.51cto.com/u_15309663/3155197
页: [1]
查看完整版本: poj 3264(RMQ或者线段树)