暑期训练狂刷系列——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.为了对付每场 ...
随机推荐
- SpringBoot初始教程之日志处理(二)
SpringBoot初始教程之日志处理(二) 1.介绍 SpringBoot默认是采用logback进行日志处理.Logback是由log4j创始人设计的又一个开源日志组件.Logback是由log4 ...
- Android中AsyncTask使用具体解释
在Android中我们能够通过Thread+Handler实现多线程通信.一种经典的使用场景是:在新线程中进行耗时操作.当任务完毕后通过Handler向主线程发送Message.这样主线程的Handl ...
- linux动态库的种种要点
linux下使用动态库,基本用起来还是非常easy.但假设我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地执行. 本篇先谈谈动态库符号方面的问题. 測 ...
- oracle 重要函数
1)instr()函数的格式 (俗称:字符查找函数) 格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串) 格式二:instr( string1, st ...
- 【iOS系列】-UIWebView加载网页禁止左右滑动
[iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...
- [IT练习册]Python练习项目 思路
1.爬虫:爬取如下网站一年的内容. http://www.calvarymoravian.org/dailytext 2.蛇形棋: 开发一个类似蛇形棋的游戏.最好基于Web. 3.爬虫+通讯录: 从公 ...
- oracle的日期蛋
一切都是扯鸡巴蛋. 在网上查oracle的日期函数用法,得到一大堆语法,林林总总,都是扯鸡巴蛋,没能解决我的问题. 其实,我想写这么一条语句:查找某个日期(不含时分秒)产生或有关的记录.咋写? SQL ...
- nhibernate的关系
用nhibernate,觉得比较难把握的是其中表间的关系. 我用的是Fluently Nhibernate,直接用代码,而不是XML来书写代码与数据表的映射.其中表间关系有3种: 1.Referenc ...
- how to create modals with Bootstrap
In this tutorial you will learn how to create modals with Bootstrap. Creating Modals with Bootstrap ...
- 解决Linux环境Oracle显示乱码
首先查看当前的编码格式 select userenv('language') from dual; 解决方法: 一.临时解决方法 切换到Oracle用户,执行 export NLS_LAN ...