Balanced Lineup poj3264 线段树

题意

一串数,求出某个区间的最大值和最小值之间的差

解题思路

使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最小值,然后做差就好了,基本上就是线段树模板题

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=5e4+7;
struct node{
int l, r, max, min;
}tre[maxn<<2];
int num[maxn];
int n, m;
void up(int rt)
{
//为了效率使用了条件语句,但是好像也没有提高多少
tre[rt].max=tre[rt<<1].max > tre[rt<<1|1].max ? tre[rt<<1].max : tre[rt<<1|1].max;
tre[rt].min=tre[rt<<1].min < tre[rt<<1|1].min ? tre[rt<<1].min : tre[rt<<1|1].min;
}
void build(int rt, int l, int r) //正常的线段树建立函数
{
tre[rt].l=l;
tre[rt].r=r;
if(l==r)
{
tre[rt].max=num[l];
tre[rt].min=num[l];
return ;
}
int mid=(l+r)>>1;
build(rt<<1, l, mid);
build(rt<<1|1, mid+1, r);
up(rt);
}
int querymax(int rt, int L, int R)//寻找区间最大值
{
if(L <= tre[rt].l && tre[rt].r <= R)
{
return tre[rt].max;
}
int mid=(tre[rt].l+tre[rt].r)>>1;
int ans=0, tmp;
if(L<=mid)
{
tmp=querymax(rt<<1, L, R);
ans= ans > tmp ? ans:tmp;
}
if(R>mid)
{
tmp=querymax(rt<<1|1, L, R);
ans= ans > tmp ? ans:tmp;
}
return ans;
}
int querymin(int rt, int L, int R)//寻找区间最小值
{
if(L <= tre[rt].l && tre[rt].r <= R)
{
return tre[rt].min;
}
int mid=(tre[rt].l+tre[rt].r)>>1;
int ans=inf, tmp;
if(L<=mid)
{
tmp=querymin(rt<<1, L, R);
ans= ans < tmp ? ans:tmp;
}
if(R>mid)
{
tmp=querymin(rt<<1|1, L, R);
ans= ans<tmp ? ans:tmp;
}
return ans;
}
int main()
{
int L, R, c;
char s[4];
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i=1; i<=n; i++)
{
scanf("%d", &num[i]);
}
build(1, 1, n);
for(int i=1; i<=m; i++)
{
scanf("%d%d", &L, &R);
printf("%ld\n", querymax(1, L, R)-querymin(1, L, R));
}
}
return 0;
}

Balanced Lineup poj3264 线段树的更多相关文章

  1. kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  2. [POJ3264]Balanced Lineup(线段树,区间最值差)

    题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...

  3. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  4. POJ3264 Balanced Lineup 【线段树】+【单点更新】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 ...

  5. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  6. poj 3264 Balanced Lineup (线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42489   Accepted: 20000 ...

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

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

  8. Balanced Lineup:线段树:区间最值 / RMQ

    不要被线段树这个名字和其长长的代码吓到. D - Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ...

  9. poj 3246 Balanced Lineup(线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 38942   Accepted: 18247 ...

随机推荐

  1. NTC电阻Rt与温度T关系

    NTC电阻Rt与温度T公式如下: Rt=10000*exp(3950*(1/(273.15+T)-1/(273.15+25))). 例:0摄氏度时,电阻为33620.6037214357 欧姆 Rt= ...

  2. Django【第22篇】:基于Ajax实现的登录

    基于ajax实现的登录 一.需要知道的新知识点 1.刷新验证码.给src属性加一个?号.加个?会重新去请求 //#给验证码刷新 $(".vialdCode_img").click( ...

  3. Django【第6篇】:Django之ORM单表操作(增删改查)

    django之数据库表的单表查询 一.添加表记录 对于单表有两种方式 # 添加数据的两种方式 # 方式一:实例化对象就是一条表记录 Frank_obj = models.Student(name =& ...

  4. cron常用表达式

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11905247.html 推荐一个cron表达式生成的网站:https://www.freeformat ...

  5. watch和computed

    watch和computed都是以Vue的依赖追踪机制为基础的,它们都试图处理这样一件事情:当某一个数据(称它为依赖数据)发生变化的时候,所有依赖这个数据的“相关”数据“自动”发生变化,也就是自动调用 ...

  6. LeetCode--094--二叉树的中序遍历(python)

    递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...

  7. Spring AOP expose-proxy

    写在前面 expose-proxy.为是否暴露当前代理对象为ThreadLocal模式. SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲 ...

  8. 简单说说JavaBean的使用

    一:JavaBean定义 JavaBean是一种可重复使用.跨平台的软件组件.JavaBean可分为两种:一种是有用户界面(UI,User Interface)的JavaBean,例如中的那些可视化图 ...

  9. C#删除文件夹的文件

    using System.IO; //判断文件是不是存在if(File.Exists(@"文件路径")){//如果存在则删除File.Delete(@"文件路径" ...

  10. array object

    w object(stdClass)#3 (8) { ["MERCHANT_ID"]=> string(11) "MERCHANT_ID" [" ...