<题目链接>

题目大意:

求给定区间内最大值与最小值之差。

解题分析:

线段树水题,每个节点维护两个值,分别代表该区间的最大和最小值即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
#define INF 0x3f3f3f3f
+;
int n,m;
int a[N];
struct Tree{
    int mn,mx;
}tr[N<<];

void Pushup(int rt){
    tr[rt].mx=max(tr[rt<<].mx,tr[rt<<|].mx);
    tr[rt].mn=min(tr[rt<<].mn,tr[rt<<|].mn);
}

void build(int rt,int l,int r){
    if(l==r){
        tr[rt].mn=tr[rt].mx=a[l];
        return;
    }
    ;
    build(Lson);
    build(Rson);
    Pushup(rt);
}

int query(int rt,int l,int r,int L,int R,int c){
    if(L<=l&&r<=R){
        if(!c)return tr[rt].mn;
        else return tr[rt].mx;
    }
    ;
    int mxval=-INF,mnval=INF;
    if(L<=mid){
        if(!c)mnval=min(mnval,query(Lson,L,R,c));   //注意这里最大和最小都是向左儿子递归
        else mxval=max(mxval,query(Lson,L,R,c));
    }
    if(R>mid){
        if(!c)mnval=min(mnval,query(Rson,L,R,c));
        else mxval=max(mxval,query(Rson,L,R,c));
    }
    if(!c)return mnval;
    else return mxval;
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        ;i<=n;i++){
            scanf("%d",&a[i]);
        }
        build(,,n);
        ;i<=m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            ,,n,u,v,);
            ,,n,u,v,);
            printf("%d\n",mx-mn);
        }
    }
    ;
} 

2018-09-23

POJ 3264 Balanced Lineup 【线段树】的更多相关文章

  1. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  2. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  3. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  4. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  5. POJ 3264 Balanced Lineup (线段树)

    Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...

  6. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  7. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  8. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  9. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  10. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. Confluence 6 PostgreSQL 设置准备

    请查看 Supported Platforms 页面来获得 Confluence 系统支持的 PostgreSQL 数据库版本.你需要在安装 Confluence 之前升级你的 PostgreSQL ...

  2. Let the Balloon Rise <map>的应用

    Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the ...

  3. 高斯消元-poj1222熄灯问题状态压缩解法

    有点自闭的..为什么我最后的答案是倒着来的啊.. 搞明白了:因为一开始构造的系数就是反着的,,所以主元也倒过来了.. #include<iostream> #include<cstd ...

  4. lightoj1234 打表技巧:分块打表

    /* 打不了那么大的表,所以只记录分块的信息即可 */ #include<bits/stdc++.h> using namespace std; #define maxn 1000005 ...

  5. Java Insets获取窗口的顶、底、左、右的大小

    import java.awt.Insets; import javax.swing.JFrame; public class NewFrame { public static void main(S ...

  6. PHP编译安装时常见错误解决办法

    转载自:http://www.bkjia.com/PHPjc/1008013.html This article is post on https://coderwall.com/p/ggmpfa c ...

  7. ajax---异步请求对象的属性和方法

    方法: 1).open(method.url,asyn):创建请求,(post.get)asyn:表示同步(false)还是异步(true)提交 ,默认true 2)send(body) 发送请求,b ...

  8. java中的相对目录问题

    在java开发过程中,常常需要使用本地文件内容文件.在调试他人代码的过程中,可能不经意间改变了源代码的根目录(顶级目录),从而导致“java io filenotfoundexception ”.解决 ...

  9. 没有-jackson相关依赖会抛出如下异常--------在spring官方文档有解释

    <!--jackson相关依赖--><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackso ...

  10. tensorflow:验证码的识别(下)

    上两篇详细的说明了验证码的识别,不过我们采用的是方法二,下面采用方法一.注意和方法二的区别. 验证码识别方法一: 把标签转为向量,向量长度为40.(4位数字验证码) 验证码的生成和tf.record的 ...