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. (一)最小的Django

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 本文基本内容均出自<Lightweight Django>(中文为<轻量级D ...

  2. The Movie db (TMDB)的API申请

    在共享API TMDB中申请时,一只报错Application summary please elaborate on how you plan to use our API,我是用汉字描述的,开始以 ...

  3. CET——4 常用短语

    在网上看到的,先拔到自己这来,四级大大,求过!!!!

  4. 童话故事 --- CPU的贴身侍卫ITCM和ICache

    "叮铃铃- 叮铃铃-" "谁呀?"黛丝博士打开了家门,"哇,高飞,你怎么来了?" 高飞狗:"好久不见,想来看看你,还买了你最喜欢吃 ...

  5. Zabbix自动发现监控Tomcat进程

    1.编辑自动发现脚本 自动发现脚本只支持JSON格式 #!/usr/bin/env python # -*- coding:utf-8 -*- import commands import psuti ...

  6. unity创建和加载AssetBundle

    先说一下为什么要使用AssetBundle吧,以前做东西一直忽略这个问题,现在认为这个步骤很重要,代码是次要的,决策和为什么这样搞才是关键. 一句话概括吧,AssetBundle实现了资源与服务分离, ...

  7. Webpack 2 视频教程 019 - Webpack 2 中配置多页面编译

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

  8. bash脚本之数组学习

    在bash中可使用索引数组和关联数组,bash在4.0版本之后才添加了对关联数组的支持 一.索引数组 1.定义索引数组 # 方式1 array_value=(1 2 3 4 5 6)或者array_v ...

  9. Perl 中 `cmd` 和system"cmd"的区别

    在perl中,调用系统命令有两种形势,`cmd` 和system"cmd",他们主要的区别是`cmd`会获取返回结果,而system"cmd"会直接将结果输出到 ...

  10. VS代码生成工具ReSharper使用手册:配置快捷键(转)

    原文:http://blog.csdn.net/fhzh520/article/details/46364603 VS代码生成工具ReSharper提供了丰富的快捷键,可以极大地提高你的开发效率. 配 ...