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. rsync服务实践

    RSYNC数据备份 RSYNC=Remote Sync 远程同步   高效,一定要结合shell 官方网站:https://rsync.samba.org/ Author:     Andrew Tr ...

  2. day02项目配置代码

    一.maven相互依赖结构 1.parent(pom)[父类]2.common(jar)[工具类]3.pojo(jar)[实体类] 依赖 common(jar)4.dao(jar)[CRUD] 依赖 ...

  3. [CF852D] Exploration plan

    问题描述 The competitors of Bubble Cup X gathered after the competition and discussed what is the best w ...

  4. layui 单选框取消选中

    <ul> <li> <span class="time">17:18</span> <span class="typ ...

  5. bzoj 4298 [ONTAK2015]Bajtocja——哈希+启发式合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4298 题面: 给定d张无向图,每张图都有n个点.一开始,在任何一张图中都没有任何边.接下来有 ...

  6. D - Find Integer

    D - Find Integer $a^{n}+b^{n}=c^{n}$ 给定a,n求解$b,c$ 三次以上没有整数解 #include<bits/stdc++.h> using name ...

  7. 小程序cover-view

    cover-view包裹的元素设置定位,元素内容长短会影响cover-view的位置,即使设置的left,top一致 最佳解决方法,就是给cover-view设置宽度

  8. CG-CTF | 密码重置2

    跟则提示走,美滋滋: 1.找到邮箱: 2.下载备份: 3.PHP弱类型,string与int用的是“==” ........这一行是省略的代码........ if(!empty($token)&am ...

  9. Collections 索引

    About Me NOIp 数据结构专题总结 NOIp 图论算法专题总结 NOIp 基础数论知识点总结 NOIp 数学知识点总结 搜索算法总结 (不包含朴素 DFS, BFS) 位运算 字符串算法总结 ...

  10. 个推一键认证SDK重磅推出,打造秒级登录体验,让用户一“键”倾心

    移动互联网时代,用户注意力的持续时间越来越短,他们追求便捷与高效.从账号密码登录.短信验证,到第三方登录甚至人脸识别登录,APP的注册/登录方式在逐步变化,开发者希望在这重要的交互端口提升用户的体验, ...