TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)

本文通过一个具体的编程问题,介绍了如何使用RMQ-ST算法解决区间最大最小值查询问题,并提供了两种实现方式:一种是基于稀疏表格的方法,另一种是利用线段树的数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1698

时间限制(普通/Java):5000MS/50000MS     内存限制:65536KByte

描述

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.

输入

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 ≤ ABN), representing the range of cows from A to B inclusive.

输出

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.

样例输入

6 3
1
7
3
4
2
5
1 5
4 6
2 2

样例输出

6
3
0

思路:题目大意就是,给n个数m个查询,下面行输入n个数。m行输入m个查询,查询最大值和最小值的差。

         rmq-st模板题。拿来练手的。作为丢人的初学线段树选手,也附上手打的线段树代码。

RMQ-ST代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map> 
#include<vector>
#define LL long long
#include<assert.h>
using namespace std;
int a[50001],dm[100000][20],dx[100000][20];
void rmq(int num){
    for(int i = 0 ; i < num ; i++){
        dm[i][0] = dx[i][0] = a[i];
    }
    for(int j = 1 ; (1<<j) <= num ; j++){
        for(int i = 0 ;i+(1<<j)-1 < num ;i ++){
            dx[i][j] = max(dx[i][j-1],dx[i+(1<<(j-1))][j-1]);
            dm[i][j] = min(dm[i][j-1],dm[i+(1<<(j-1))][j-1]);
        }
    }
}
int qmax(int st,int ed){
    int k = 0;
    while((1<<(k+1))<= ed - st + 1)k++;
    return max(dx[st][k],dx[ed - (1<<k) + 1][k]);
}
int qmin(int st,int ed){
    int k = 0;
    while((1<<(k+1))<= ed - st + 1)k++;
    return min(dm[st][k],dm[ed - (1<<k) + 1][k]);
}
int main(){
    int n,k;
    while(~scanf("%d %d",&n,&k)){
        memset(dx,0,sizeof(dx));
        memset(dm,0,sizeof(dm));
        for(int i = 0 ; i < n ; i++)scanf("%d",&a[i]);
        rmq(n);
        while(k--){
            int x,y;
            scanf("%d %d",&x,&y);
            printf("%d\n",qmax(x-1,y-1)-qmin(x-1,y-1));
        }
    }
}

线段树代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 100000;
struct note{
    int l,r;
    int nMin,nMax;
}segTree[maxn<<2];
int Max,Min;
int a[maxn];
void build(int i,int l,int r){
    segTree[i].l = l;
    segTree[i].r = r;
    if(l==r){
        segTree[i].nMin = segTree[i].nMax = a[l];
        return;
    }
    int mid = (l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    segTree[i].nMax = max(segTree[i<<1].nMax,segTree[i<<1|1].nMax);
    segTree[i].nMin = min(segTree[i<<1].nMin,segTree[i<<1|1].nMin);
}
void query(int i,int l,int r){
    if(segTree[i].nMax <= Max && segTree[i].nMin >= Min){
        return;
    }
    if(segTree[i].l == l && segTree[i].r == r){
        Max = max(segTree[i].nMax,Max);
        Min = min(segTree[i].nMin,Min);
        return;
    }
    int mid = (segTree[i].l + segTree[i].r) >> 1;
    if(r <= mid)
        query(i<<1,l,r);
    else if(l > mid) 
        query(i<<1|1,l,r);
    else{
        query(i<<1,l,mid);
        query(i<<1|1,mid+1,r);
    }
}
int main(){
    int n,m;
    while(~scanf("%d %d",&n,&m)){
        for(int i = 1 ; i <= n ;i++)scanf("%d",&a[i]);
        build(1,1,n);
        while(m--){
            int x,y;
            Max = -1000000;Min = 1000000;
            scanf("%d %d",&x,&y);
            query(1,x,y);
            printf("%d\n",Max-Min);
        }
    }
}

 

转载于:https://www.cnblogs.com/Esquecer/p/8641798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值