POJ3264线段树基础

http://poj.org/problem?id=3264
给出一段区间,求区间最大最小值差

#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<map>
#include<queue>
#include<climits>
#include<stdlib.h>
#include<iomanip>
#include<string.h>
#include<cstring>
using namespace std;
/*********************************************/
#define N 200000+5
#define R (rt<<1|1)
#define L (rt<<1)
#define lson l,mid,L
#define rson mid+1,r,R
#define mid (l+r)/2
#define INF 0x3f3f3f3f
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define LL long long int
#define sd1(a) scanf("%d",&a)
#define sd2(a,b) scanf("%d%d",&a,&b)
#define sd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd1(a) printf("%d",a)
#define pd2(a,b) printf("%d%d",a,b)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
/**********************************************/
//快速输入法,与此题无关
int Scan() {
    int res = 0, ch, flag = 0;
    if ((ch = getchar()) == '-')
        flag = 1;
    else if (ch >= '0' && ch <= '9')
        res = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + ch - '0';
    return flag ? -res : res;
}
void Out(int a) {
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}
/********************************************************/
int n, m;
struct node{
    int MAX, MIN;
};
node s[N << 2];
int maxn, minn;
void pushup(int rt){
    s[rt].MAX = max(s[L].MAX, s[R].MAX);
    s[rt].MIN = min(s[L].MIN, s[R].MIN);
}
void build(int l, int r, int rt){
    if (l == r){
        scanf("%d", &s[rt].MAX);
        s[rt].MIN = s[rt].MAX;
        return;
    }
    build(lson);
    build(rson);
    pushup(rt);
}
void  query(int a, int b, int l, int r, int rt){
    node temp;
    if (a <= l&&r <= b){
        maxn = max(maxn, s[rt].MAX);
        minn = min(minn, s[rt].MIN);
        return;
    }
    if (a <= mid)query(a, b, lson);
    if (b > mid)query(a, b, rson);
}
int main(){
    cin >> n >> m;
    build(1, n, 1);
    while (m--){
        int a, b;
        scanf("%d%d", &a, &b);
        maxn = -1, minn = INF;
        query(a, b, 1, n, 1);
        cout << maxn - minn << endl;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值