POJ-3264 Balanced Lineup

本文介绍了一种使用线段树的数据结构实现方法,并通过一个具体的应用案例来展示如何进行区间查询,包括最大值和最小值的计算。文章详细解释了线段树的构建过程、插入操作以及查询算法。

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

传送门

题目大意:就是建立一个线段树,然后从1到n插入数字,然后查询几组区间,求这几组区间当中每一组区间最大值和最小值的差

代码(板子):

#include<iostream>
#include<cstdio>
using namespace std;
const int INF=0xfffff0;
int minV=INF;
int maxV=-INF;
 struct Node{
    int L,R;
    int minV,maxV;
    int Mid(){
        return (L+R)/2;
    }
 };
 Node tree[800010];
 //4倍叶子节点的数
 void BuildTree(int root,int L,int R){
    tree[root].L=L;
    tree[root].R=R;
    tree[root].minV=INF;
    tree[root].maxV=-INF;
    //初始化最小的是最大的
    //最大的是最小的
    if(L!=R){

        BuildTree(2*root+1,L,(L+R)/2);
        BuildTree(2*root+2,(L+R)/2+1,R);
    }
 }
 void Insert(int root,int i,int v){
    if(tree[root].L==tree[root].R){

        tree[root].minV=tree[root].maxV=v;
        return ;
    }
    tree[root].minV=min(tree[root].minV,v);
    tree[root].maxV=max(tree[root].maxV,v);
    if(i<=tree[root].Mid())
        Insert(2*root+1,i,v);
    else
        Insert(2*root+2,i,v);
 }
 void Query(int root,int s,int e){
    if(tree[root].minV>=minV&&tree[root].maxV<=maxV)
        return ;
    if(tree[root].L==s&&tree[root].R==e){
        minV=min(minV,tree[root].minV);
        maxV=max(maxV,tree[root].maxV);
        return ;
    }
    if(e<=tree[root].Mid())
        Query(2*root+1,s,e);
    else if(s>tree[root].Mid())
        Query(2*root+2,s,e);
    else{
        Query(2*root+1,s,tree[root].Mid());
        Query(2*root+2,tree[root].Mid()+1,e);
    }
 }
int main()
{
    int n,q,h;
    int i,j,k;
    scanf("%d%d",&n,&q);
    BuildTree(0,1,n);//以0为根节点,从1到n建树
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&h);
        //第i个数,值为h插入线段树
        Insert(0,i,h);
    }
    for(int i=0; i<q; i++)
    {
        //输入q个查询数
        int s,e;
        scanf("%d %d",&s,&e);
        minV=INF;
        maxV=-INF;
        Query(0,s,e);
        printf("%d\n",maxV-minV);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值