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. LOJ6358 前夕

    上来4的倍数又要交集恰好 单位根反演+二项式反演定了( 具体推柿子放下面了qwq $g(n) = \sum_{i=n}^N f(i) \binom{i}{n} \\g(n) = \binom{N}{n ...

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

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

  3. 对postcss-plugin-px2rem的研究

    1.安装postcss-plugin-px2rem 2.配置 css: { loaderOptions: { postcss: { plugins: [ require('postcss-plugin ...

  4. 进行移动端rem适配

    (function (designWidth, maxWidth) { var doc = document, win = window; var docEl = doc.documentElemen ...

  5. Kohana重写接收不到get参数问题

    .htaccess,不需要重启apache # Turn on URL rewriting RewriteEngine On # Installation directory RewriteBase ...

  6. Improving Network Management with Software Defined Networking

    Name of article:Improving Network Management with  Software Defined Networking Origin of the article ...

  7. 基于BootStrap的分页代码实现

    public class PageUtil { //targetUrl 访问url totalNum总记录数 currentPage 当前页数 pageSize每页的大小 public static ...

  8. [BZOJ4558]:[JLoi2016]方(容斥+模拟)

    题目传送门 题目描述 上帝说,不要圆,要方,于是便有了这道题.由于我们应该方,而且最好能够尽量方,所以上帝派我们来找正方形上帝把我们派到了一个有N行M列的方格图上,图上一共有$(N+1)\times ...

  9. gitlab+jenkins之环境搭建

    文中用到的安装包都已经上传到百度网盘,下载地址在文章底部(还没传...) 前置条件: 为了节约配置时间,在正式配置前,应该先做好如下准备: 首先先把整个流程仔仔细细的看3遍,确认对配置整体的流程,配置 ...

  10. Linux随笔 - vi/vim 编辑器显示行号

    显示行号 1. 打开vi 编辑器 2. 输入  :set number 3. 回车 关闭行号显示 1. 打开vi 编辑器 2. 输入  :set nonumber 3. 回车 行号在每次打开 vi/v ...