Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 53703   Accepted: 25237
Case Time Limit: 2000MS

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 ≤ ABN), 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

Source

分析:线段树求最大值和最小值,然后最大值减去最小值即为正解!貌似这题好像有暴力写法?
下面给出AC代码:
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxsize 200020
typedef struct
{
int left,right;
int maxn;
int minn;
}Node;
int n,m;
int Max,Min;
int num[maxsize];
Node tree[maxsize*];
inline void buildtree(int root,int left,int right)// 构建线段树
{
int mid;
tree[root].left=left;
tree[root].right=right;// 当前节点所表示的区间
if(left==right)// 左右区间相同,则此节点为叶子,max 应储存对应某个学生的值
{
tree[root].maxn=num[left];
tree[root].minn=num[left];
return;
}
mid=(left+right)/;
//int a,b;// 递归建立左右子树,并从子树中获得最大值
buildtree(*root,left,mid);
buildtree(*root+,mid+,right);
tree[root].maxn=max(tree[root*].maxn,tree[root*+].maxn);
tree[root].minn=min(tree[root*].minn,tree[root*+].minn);
}
inline void find(int root,int left,int right)// 从节点 root 开始,查找 left 和 right 之间的最大值
{
int mid;
//if(tree[root].left>right||tree[root].right<left)// 若此区间与 root 所管理的区间无交集
//return;
if(left==tree[root].left&&tree[root].right==right)// 若此区间包含 root 所管理的区间
{
Max=max(tree[root].maxn,Max);
Min=min(tree[root].minn,Min);
return;
}
mid=(tree[root].left+tree[root].right)/;
if(right<=mid)
find(root*,left,right);
else if(left>mid)
find(root*+,left,right);
else
{
find(root*,left,mid);
find(root*+,mid+,right);
//tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
//tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
//return;
}
} int main()
{
//char c;
int i;
int x,y;
//scanf("d%d",&n,&m);
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&num[i]);
buildtree(,,n);
for(i=;i<=m;i++)
{
//getchar();
Max=-;
Min= ;
scanf("%d%d",&x,&y);
//if(c=='Q')
//printf("%d\n",find(1,x,y));
//else
//{
// num[x]=y;
// update(1,x,y);
//}
find(,x,y);
printf("%d\n",Max-Min);
}
}
return ;
}

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】的更多相关文章

  1. POJ 3264 Balanced Lineup 线段树RMQ

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

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

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

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

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

  4. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

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

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

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

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

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

  8. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  9. Poj 3264 Balanced Lineup RMQ模板

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

随机推荐

  1. springboot 入门八-自定义配置信息(编码、拦截器、静态资源等)

    若想实际自定义相关配置,只需要继承WebMvcConfigurerAdapter.WebMvcConfigurerAdapter定义些空方法用来重写项目需要用到的WebMvcConfigure实现.具 ...

  2. Mac和Xcode常用的快捷键

    Mac电脑一般都不怎么用鼠标,因此除了触摸屏的各种双指.三指甚至四指的操作之外,快捷键的使用可以带来非常大的便利,本文则主要收集整理了自己在Mac常规和Xcode开发过程中常用的一些快捷键. 一.Ma ...

  3. bzoj 2733: [HNOI2012]永无乡

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  4. Spring基础篇——Spring容器和应用上下文理解

    上文说到,有了Spring之后,通过依赖注入的方式,我们的业务代码不用自己管理关联对象的生命周期.业务代码只需要按照业务本身的流程,走啊走啊,走到哪里,需要另外的对象来协助了,就给Spring说,我想 ...

  5. Webpack 2 视频教程 020 - Webpack 2 中的 HMR ( Hot Module Replacement )

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  6. 关于java字节流的read()方法返回值为int的思考

    我们都知道java中io操作分为字节流和字符流,对于字节流,顾名思义是按字节的方式读取数据,所以我们常用字节流来读取二进制流(如图片,音乐 等文件).问题是为什么字节流中定义的read()方法返回值为 ...

  7. JavaScript的DOM编程--07--节点的属性

    节点的属性: 1). nodeName: 代表当前节点的名字. 只读属性. 如果给定节点是一个文本节点, nodeName 属性将返回内容为 #text 的字符串 2). nodeType:返回一个整 ...

  8. popupwindow那些坑

    1. new PopupWindow(vw, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 如果 ...

  9. TCP协议(二)——TIME_WAIT状态

    当TCP主动关闭套接字时,采用四步握手机制来彻底关闭连接.如图: 客户端主动关闭连接,发送FIN段到服务端.TCP状态由ESTABLISHED(连接状态)转为FIN_WAIT1(表示,发送的FIN需要 ...

  10. 19 Zabbix 利用Scripts栏目对Hosts远程执行命令

    点击返回:自学Zabbix之路 19 Zabbix 利用Scripts栏目对Hosts远程执行命令 在Monitoring板块中,有Host出现的地方,单击Host按钮后,都可以执行对Host远程执行 ...