Balanced Lineup

Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3

1

7

3

4

2

5

1 5

4 6

2 2

Sample Output

6
3
0

报告:

题目大意: 有一排高度不一的牛(...),现在给出它们的高度,给几个区间求区间内的最高的牛和最矮的牛的“身高差”。

题解:

  自己写的时候是用结构体记录各个区间内的最大值和最小值,然后返回查询的区间所求的最大值和最小值的差。样例过了,但是其他的却WA了。。表示很奇怪,现在写博客才想起来我这一点还没想明白,就直接去看题解了........那先把这个只过得了样例的程序传了吧。

  .......

结果去调试发现连样例都没有过,于是分析了一下,这样写的话,如果查询区间在以分的区间两端,就只会直接返回一段的差值了。代码如下: 

 1 #include<iostream>
#include<cstdio>
#define l(u) (u<<1)
#define r(u) (u<<1|1)
using namespace std;
int n,q,highest,lowest;
int cow[];
struct pp{
int l,r,mins,maxs;
};
pp node[];
int min(int a,int b)
{
return a<b?a:b;
}
int max(int a,int b)
{
return a>b?a:b;
}
void build(int u,int left,int right)
{
node[u].l=left;node[u].r=right;
if (node[u].l==node[u].r)
{
node[u].mins=cow[left];
node[u].maxs=cow[left];
return ;
}
int mid=(left+right)>>;
build(l(u),left,mid);
build(r(u),mid+,right);
node[u].mins=min(node[l(u)].mins,node[r(u)].mins);
node[u].maxs=max(node[l(u)].maxs,node[r(u)].maxs);
}
int query(int u,int left,int right)
{
if (node[u].l==left&&node[u].r==right)
{
return node[u].maxs-node[u].mins;
}
int mid=(node[u].l+node[u].r)>>;
if (right<=mid) query(l(u),left,right);
else if (left>mid) query(r(u),left,right);
else
{
query(l(u),left,mid);
query(r(u),mid+,right);
}
}
int main()
{
freopen("balance.in","r",stdin);
cin>>n>>q;
for(int i=;i<=n;i++)
scanf("%d",&cow[i]);
build(,,n);
for(int i=;i<=q;i++)
{
int a,b;
//highest=0;//***
//lowest=1234567;//***
scanf("%d%d",&a,&b);
printf("%d\n",query(,a,b));
}
return ;
}

正确的代码中是用 high 和low 来记录比较最大值和最小值并且只是 void 并不 返回值,这样一来就可以得到正确的答案了。

代码如下:

 #include<iostream>
#include<cstdio>
#define l(u) (u<<1)
#define r(u) (u<<1|1)
using namespace std;
int n,q,highest,lowest;
int cow[];
struct pp{
int l,r,mins,maxs;
};
pp node[];
int min(int a,int b)
{
return a<b?a:b;
}
int max(int a,int b)
{
return a>b?a:b;
}
void build(int u,int left,int right)
{
node[u].l=left;node[u].r=right;
if (node[u].l==node[u].r)
{
node[u].mins=cow[left];
node[u].maxs=cow[left];
return ;
}
int mid=(left+right)>>;
build(l(u),left,mid);
build(r(u),mid+,right);
node[u].mins=min(node[l(u)].mins,node[r(u)].mins);
node[u].maxs=max(node[l(u)].maxs,node[r(u)].maxs);
}
void query(int u,int left,int right)
{
if (node[u].l==left&&node[u].r==right)
{
lowest=min(lowest,node[u].mins);//***
highest=max(highest,node[u].maxs);///***
return ;
}
int mid=(node[u].l+node[u].r)>>;
if (right<=mid) query(l(u),left,right);
else if (left>mid) query(r(u),left,right);
else
{
query(l(u),left,mid);
query(r(u),mid+,right);
}
}
int main()
{
//freopen("balance.in","r",stdin);
cin>>n>>q;
for(int i=;i<=n;i++)
scanf("%d",&cow[i]);
build(,,n);
for(int i=;i<=q;i++)
{
int a,b;
highest=;//***
lowest=;//***
scanf("%d%d",&a,&b);
query(,a,b);
printf("%d\n",highest-lowest);
}
return ;
}

注意事项:

1、定初值。

POJ 3264 Balanced Lineup 线段树 第三题的更多相关文章

  1. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  2. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  3. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  4. 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 ...

  5. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  6. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  7. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

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

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

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

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

随机推荐

  1. HTML5 学习笔记--------》HTML5概要与新增标签!

      一.HTML5概要 1.1.为什么需要HTML5 HTML4陈旧不能满足日益发展的互联网需要,特别是移动互联网.为了增强浏览器功能Flash被广泛使用,但安全与稳定堪忧,不适合在移动端使用(耗电. ...

  2. (九)uboot配置编译、源码分析

    一.X210官方uboot配置编译实践1.找到官方移植好的uboot(BSP概念)(1)源头的源代码是uboot官网下载的.这个下载的源代码可能没有你当前使用的开发板的移植,甚至找不到当前开发板使用的 ...

  3. Android中view动画

    [1]透明 //点击按钮 实现iv 透明的效果 动画 public void click1(View v) { //1.0意味着着完全不透明 0.0意味着完全透明 AlphaAnimation aa ...

  4. oracle对象类型

    Oracle的对象类型 对象类型 在PL/SQL中,面向对象的程序设计师基于对象类型来完成的.对象类型是用户自定义的一种复合数据类型,它封装了数据结构和用于操纵这些数据结构的过程和函数. 数据库的对象 ...

  5. [css] 垂直居中方法

    原文链接:http://www.cnblogs.com/2050/p/3392803.html 一.text-algin:center; 适用于行内元素水平居中,如图片.按钮.文字, 但是在IE67下 ...

  6. 转!!常用的4种动态网页技术—CGI、ASP、JSP、PHP

    1.CGI   CGI(Common Gateway Interface,公用网关接口)是较早用来建立动态网页的技术.当客户端向Web服务器上指定的CGI程序发出请求时,Web服务器会启动一个新的进程 ...

  7. hiho_1055_刷油漆

    题目大意 一棵树,每个节点都有相应的value值.从根开始选择M个节点相互连通,使得这些节点的value值之和最大. 题目链接:[刷油漆][1] 题目分析 典型的树形dp,dp[i][j] 表示以节点 ...

  8. @ExceptionHandler

    @Controller public class AccessController { /** * 异常页面控制 * * @param runtimeException * @return */ @E ...

  9. Apache Commons fileUpload实现文件上传之一

      需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...

  10. --专访雷果国: 从1.5K到18K 一个程序员的5年成长之路--

    导语:今年三月份,在CSDN博客和新浪微博上有一篇<从1.5K到18K,一个程序员的5年成长之路>被众人分享和传阅,这篇博文首先介绍了作者自学之初薄弱的基础,然后通过流水账形式分享了那个从 ...