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. VMmare下安装redhat

    一.虚拟机必须安装在自定义的文件夹下,虚拟硬盘文件必须存放在自定义路径下(避免中文) 二.安装时选择linux类型时必须选择red hat enterprise linux 5 64位 三.操作系统名 ...

  2. [CF1208D] Restore Permutation

    传送门 题意:有一个长为\(n\)的排列\(p\),设\(S_i=\sum_{j=1}^{i-1}p_j\cdot[p_j<p_i]\),给出\(S\),要求还原出\(p\).保证有解,\(n\ ...

  3. C++ GUI Qt4学习笔记09

    C++ GUI Qt4学习笔记09   qtc++ 本章介绍Qt中的拖放 拖放是一个应用程序内或者多个应用程序之间传递信息的一种直观的现代操作方式.除了剪贴板提供支持外,通常它还提供数据移动和复制的功 ...

  4. 用电脑Python控制Arduino

    python指令: import serial #导入串口通讯库 import time ser=serial.Serial("com4",9600,timeout=1) demo ...

  5. Java——类

    [类]  

  6. 6.25考试整理:江城唱晚&&不老梦&&棠梨煎雪——题解

    按照旧例,先安利一下主要作者:一扶苏一 以及扶苏一直挂念的——银临姐姐:银临_百度百科 (滑稽) 好哒,现在步入正题: 先看第一题: 题解: 在NOIP范围内,看到“求方案数”,就说明这个题是一个计数 ...

  7. string matching

    string matching exkmp #include<bits/stdc++.h> using namespace std; ; int Nex[maxn],extend[maxn ...

  8. border、outline、boxshadow那些事

    border 边框是我们美化网页.增强样式最常用的手段之一.例如: <div class="text"></div> .text { width: 254p ...

  9. [CSP-S模拟测试]:毛二琛(DP)

    题目描述 $MYC$在$NOI2018$中,遇到了$day1T2$这样一个题,题目是让你求有多少“好”的排列.$MYC$此题没有获得高分,感到非常惭愧,于是回去专心研究排列了.如今数排列的题对$MYC ...

  10. Parse error: syntax error, unexpected 'class' (T_CLASS)

    电脑坏了重新下载代码. 结果报错 Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_ST ...