暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)
题目连接:
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(线段树)的更多相关文章
- 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)
题目连接: http://poj.org/problem?id=3468 题目大意: 给出n个数,有两种操作: 1:"C a b c",[a,b]中的每一个数都加上c. 2:&qu ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 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 ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...
随机推荐
- httpclient请求去掉返回结果string中的多余转义字符
public String doGet() { String uriAPI = "http://XXXXX?str=I+am+get+String"; String result= ...
- Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自己主动填入功能结合实例)
用户注冊或者找回password时通常会用到短信验证功能.这里我们使用第三方的短信平台进行验证实例. 我们用到第三方短信验证平台是Mob,地址为:http://mob.com/ 一.注冊用户.获取SD ...
- 将oracle10g 升级至10.2.0.4
http://blog.csdn.net/launch_225/article/details/7221489 一.单实例环境,全时长一个半钟多. 详细图文说明到这下载 1.停止所有oracle相关进 ...
- Office PDF如何旋转页面之后保存
直接视图-旋转视图-逆时针,是不行的,旋转之后无法保存,另存为之后也再打开也没有效果. 要点击视图-工具-页面 然后在右边的菜单中点击旋转,然后执行旋转,然后就可以保存了.
- react-container-query
1.媒体查询 响应式组件 2.使用方法 (1)引入 import { ContainerQuery } from 'react-container-query'; (2)规定屏幕尺寸 /** * 媒体 ...
- SLF4J: Failed to load class的问题及解决
今天在做接口测试,一运行测试程序,就跳出这样一个大大的错误: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. SLF4 ...
- 【问题记录】LoadRunner 接口压测-json格式报文
[问题起因] 前段时间,协助其他项目录制接口压测脚本,对方要求请求报文内容实现参数化. 请求方法如下: 直接在Parameter List中新增一个parameter, 将请求报文放入dat文件中.这 ...
- 1.5.4 HAVING子句
1.5.4 HAVING子句正在更新内容.请稍后
- Windows 7旗舰版安装Visual Studio 2013 Ultimate的系统必备及注意事项
系统必备: 1.Windows7 SP1 2.IE 10
- Deep Learning 29: caffe入门学习
1.跑教程:深度学习(六)caffe入门学习,上面有比较好的注释 .prototxt文件:网络结构文件 solver.prototxt:网络求解文件 net: "examples/mnist ...