POJ 3264 Balanced Lineup - (RMQ)

题目链接:http://poj.org/problem?id=3264

RMQ水题

#include <stdio.h>  
#include <vector>   
#include <algorithm>
using namespace std;

//POJ 3264 Balanced Lineup

class SegmentTree {

    struct SegmentTreeNode
    {
        int start, end;
        int _min, _max;
        SegmentTreeNode(int start, int end, int min, int max) {
            this->start = start;
            this->end = end;
            this->_min = min;
            this->_max = max;
        }
    };

public:

    SegmentTree(vector<int>& Vec)
        :_size(1),
        _Identity_Element_for_MIN(0x7fffffff),
        _Identity_Element_for_MAX(0)
    {
        // do intialization if necessary  
        if (Vec.empty())return;

        find_size(Vec.size());
        ST.resize(2 * _size - 1);
        build(0, 0, _size - 1, Vec);

    }


    int queryMIN(int start, int end) const
    {
        return doQueryMIN(0, start, end);
    }

    int queryMAX(int start, int end) const
    {
        return doQueryMAX(0, start, end);
    }

protected:

    int _size;

    vector<SegmentTreeNode*> ST;

    const int _Identity_Element_for_MIN;

    const int _Identity_Element_for_MAX;

private:

    //find_size
    void find_size(int size)
    {
        while (_size < size)
        {
            _size <<= 1;
        }
    }


    //SegmentTree Initialization
    void build(int index, int start, int end, const vector<int>& Vec)
    {

        //leaf node
        if (start == end)
        {
            ST[index] = new SegmentTreeNode(start, end,
                (start < Vec.size()) ? Vec[start] : _Identity_Element_for_MIN,
                (start < Vec.size()) ? Vec[start] : _Identity_Element_for_MAX);
            return;
        }

        //internal node (non-leaf)
        int mid = (start + end) / 2;


        //construct this node with initial val(_Identity_Element)
        ST[index] = new SegmentTreeNode(start, end,
            _Identity_Element_for_MIN, _Identity_Element_for_MAX);


        //construct left and right subTree (recursion)
        build((index << 1) + 1, start, mid, Vec);
        build((index << 1) + 2, mid + 1, end, Vec);


        //set value
        ST[index]->_min = min(ST[(index << 1) + 1]->_min,
            ST[(index << 1) + 2]->_min);
        ST[index]->_max = max(ST[(index << 1) + 1]->_max,
            ST[(index << 1) + 2]->_max);

    }


    //index: cur_node
    int doQueryMIN(int index, int start, int end) const
    {

        //no segment union
        if (start > ST[index]->end || end < ST[index]->start)
            return _Identity_Element_for_MIN;


        //querying segment includes root segment
        if (start <= ST[index]->start && ST[index]->end <= end)
            return ST[index]->_min;


        //partially coincide
        return min(doQueryMIN((index << 1) + 1, start, end),
            doQueryMIN((index << 1) + 2, start, end));

    }


    //index: cur_node
    int doQueryMAX(int index, int start, int end) const
    {

        //no segment union
        if (start > ST[index]->end || end < ST[index]->start)
            return _Identity_Element_for_MAX;


        //querying segment includes root segment
        if (start <= ST[index]->start && ST[index]->end <= end)
            return ST[index]->_max;


        //partially coincide
        return max(doQueryMAX((index << 1) + 1, start, end),
            doQueryMAX((index << 1) + 2, start, end));

    }

};
int main() {
    int N, Q;
    scanf("%d%d", &N, &Q);
    vector<int> height(N);
    for (int i = 0; i < N; ++i)
        scanf("%d", &height[i]);
    SegmentTree ST(height);
    for (int i = 0; i < Q; ++i)
    {
        int start, end;
        scanf("%d%d", &start, &end);
        printf("%d\n", ST.queryMAX(start - 1, end - 1) - ST.queryMIN(start - 1, end - 1));
    }

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值