题目连接:

  http://poj.org/problem?id=3264

题目大意:

  有n个数从1开始编号,问在指定区间内,最大数与最小数的差值是多少?

解题思路:

  在节点中存储max,min,然后查询指定区间的max、min。

 #include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct node
{
int L, R;
int Min, Max;
int Mid()
{
return (L + R) / ;
}
};
node tree[maxn];
int Min, Max; void build(int root, int l, int r)
{
tree[root].L = l;
tree[root].R = r;
tree[root].Min = INF;
tree[root].Max = -INF;
if (l == r)
return ;
build (*root+, l, tree[root].Mid());
build (*root+, tree[root].Mid()+, r);
}
void insert (int root, int x, int s)
{
tree[root].Min = min (tree[root].Min, s);
tree[root].Max = max (tree[root].Max, s);
if (tree[root].L == tree[root].R)
return ;
if (x <= tree[root].Mid())
insert (*root+, x, s);
else if (x > tree[root].Mid())
insert (*root+, x, s);
}
void query (int root, int s, int e)
{
if (tree[root].L == s && tree[root].R == e)
{
Min = min (Min, tree[root].Min);
Max = max (Max, tree[root].Max);
return ;
}
if (e <= tree[root].Mid())
query (*root+, s, e);
else if (s > tree[root].Mid())
query (*root+, s, e);
else
{
query (*root+, s, tree[root].Mid());
query (*root+, tree[root].Mid()+, e);
}
}
int main ()
{
int n, q, num;
while (scanf ("%d %d", &n, &q) != EOF)
{
build(, , n);
for (int i=; i<=n; i++)
{
scanf ("%d", &num);
insert (, i, num);
}
while (q --)
{
int s, e;
scanf ("%d %d", &s, &e);
Min = INF;
Max = -INF;
query (, s, e);
printf ("%d\n", Max - Min);
}
}
return ;
}

暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)的更多相关文章

  1. 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)

    题目连接: http://poj.org/problem?id=3468 题目大意: 给出n个数,有两种操作: 1:"C a b c",[a,b]中的每一个数都加上c. 2:&qu ...

  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 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  6. 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 ...

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

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

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

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

  9. 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...

随机推荐

  1. 微信接入登录功能access_token流程记录

    提示:只有认证过的订阅号或者服务号才能获取access_token. 1.app微信登录第一步是,app调起来微信客户端,通过app端的配置,引入一个微信类库, 2.授权成功后,微信会返回你一个cod ...

  2. Node.js+Web TWAIN,实现Web文档扫描和图像上传

      目录(?)[+] 通过Dynamic Web TWAIN SDK和Node.js的组合,只需要几行代码就可以实现在浏览器中控制扫描仪,获取图像后上传到远程服务器. 原文:Document Imag ...

  3. [Angular] Expose Angular Component Logic Using State Reducers

    A component author has no way of knowing which state changes a consumer will want to override, but s ...

  4. iOS学习之动画效果的实现

    // //  ViewController.m //  UI-动画练习 // //  Created by jzq_mac on 15/7/22. //  Copyright (c) 2015年 jz ...

  5. 程序C++ to C#交互

    第一次用C#调用C/C++生成的DLL文件,感觉有点新鲜,事实上仅仅是实现了执行在公共语言执行库 (CLR) 的控制之外的"非托管代码"(执行在公共语言执行库(CLR)的控制之中的 ...

  6. Linux —— 查找与替换

    Linux —— 查找与替换 文本查找: grep, egrep, fgrep        grep:根据基本正则表达式定义的模式搜索文档,并将符合模式的文本行显示出来        注意:搜索时属 ...

  7. 【HDOJ 3652】B-number

    [HDOJ 3652]B-number 给一整数n 找<=n的整数中能被13整除且含有13的 数位dp 记忆化! . 一入记忆化深似海. ..再也不想用递推了...发现真的非常好想 仅仅要保证满 ...

  8. a simple and universal interface between web servers and web applications or frameworks: the Python Web Server Gateway Interface (WSGI).

    WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server comm ...

  9. 设计模式-(10)观察者模式 (swift版)

    一,概念 观察者(Observer)模式又名发布-订阅(Publish/Subscribe)模式.GOF给观察者模式如下定义:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它 ...

  10. VS2012变化的快捷键

    VS2012变化的快捷键: 注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctrl+K加 Ctrl ...