暑期训练狂刷系列——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.为了对付每场 ...
随机推荐
- MySQL错误日志、binlog日志、查询日志、慢查询日志简介
1.数据库的日志是帮助数据库管理员,追踪分析数据库曾经发生的各种事件的有力依据,mysql中提供了错误日志.binlog日志(二进制日志).查处日志.慢查询日志.在此,我力求解决以下问题:各个日志的作 ...
- SDUTOJ 2475 Power Strings
<pre class="cpp" name="code">#include<iostream> #include<stdio.h& ...
- java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException 遇到这样的问题 ...
- centos 安装mysql时错误unknown variable 'defaults-file=/opt/redmine-2.6.0-2/mysql/my.cnf'
找到my.cnf所在目录.运行 chmod 664 my.cnf,再启动mysql成功
- 【iOS系列】-UITableView的使用
UITableView的使用: 第一:数据展示条件 1,UITableView的所有数据都是由数据源(dataSource)提供,所以想在UITableView展示数据,必须设置UITableview ...
- [英语学习]王秒同学《21天TED英语精练团》
第一个分享: Chris Anderson的TED's secret to great public speaking(英音). There's no single formula for a gre ...
- 导入别人的Android项目,提示 /Libs/gen already exists but is not a source folder. Convert to a source folder or rename it
解决方法: 遇到这个问题的解决方法: 1. 右键点击工程,选择 "Properties" 2. 选择左边的 "Java Build Path" 3. 打开 &q ...
- mysql 时间间隔计算
业务需求说明 早8:30点执行该sql,写入定时脚本 [在稳定前,需要手动复检] INSERT INTO test_error ( url, no_open_times, no_ad_times, o ...
- hexSHA1散列加密解密(不可逆)
1.maven引入codec和commons依赖: <dependency> <groupId>commons-codec</groupId> <artifa ...
- 谈谈对redux的认识
redux是从flux演变而来,但又独立于react.简言之就是,redux是一种单纯的状态管理器.可以和react搭配,也可以和其它框架搭配. redux有三个重要的部分组成: store, act ...