1636: [Usaco2007 Jan]Balanced Lineup

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 772  Solved: 560线段树裸题。。。

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.

每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别.

注意: 在最大数据上, 输入和输出将占用大部分运行时间. 

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.

第1行:N,Q
第2到N+1行:每头牛的身高
第N+2到N+Q+1行:两个整数A和B,表示从A到B的所有牛。(1<=A<=B<=N)

Output

6 3
1
7
3
4
2
5
1 5
4 6
2 2
 

Sample Input

* 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 Output

6
3
0
 

#include<cstdio>
#include <iostream>
#define M 50010
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
struct tree{int l,r,minn,maxx;}tr[*M];
int a[M];
void make(int l,int r,int p)
{
tr[p].l=l;
tr[p].r=r;
if(l==r){
tr[p].minn=a[l];
tr[p].maxx=a[l];
return ;
}
int mid=(l+r)>>;
make(l,mid,p<<);
make(mid+,r,p<<|);
tr[p].minn=min(tr[p<<].minn,tr[p<<|].minn);
tr[p].maxx=max(tr[p<<].maxx,tr[p<<|].maxx);
}
int fmin(int l,int r,int x)
{
if(tr[x].l==l&&tr[x].r==r) return tr[x].minn;
int mid=(tr[x].l+tr[x].r)>>,q=x<<;
if(r<=mid) return fmin(l,r,q);
else if(l>mid) return fmin(l,r,q+);
else return min(fmin(l,mid,q),fmin(mid+,r,q+));
}
int fmax(int l,int r,int x)
{
if(tr[x].l==l&&tr[x].r==r) return tr[x].maxx;
int mid=(tr[x].l+tr[x].r)>>;
if(r<=mid) return fmax(l,r,x<<);
else if(l>mid) return fmax(l,r,x<<|);
else return max(fmax(l,mid,x<<),fmax(mid+,r,x<<|));
}
int main()
{
int n,m,i,x,y;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",&a[i]);
make(,n,);
for(i=;i<m;i++){
scanf("%d%d",&x,&y);
printf("%d\n",fmax(x,y,)-fmin(x,y,));
}
}
 

bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树的更多相关文章

  1. BZOJ 1636: [Usaco2007 Jan]Balanced Lineup

    noip要来了,刷点基础水题. 题意: RMQ,给你N个数,Q个询问,每次查询[l,r]内,最大值减最小值是多少. 写的ST. 代码: #include<iostream> #includ ...

  2. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  3. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  4. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  5. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  6. BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

  7. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  8. 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...

  9. BZOJ1636: [Usaco2007 Jan]Balanced Lineup

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[ ...

随机推荐

  1. Linux静态库和共享库【转】

    转自:http://www.cnblogs.com/zlcxbb/p/6806269.html 1.什么是静态库 静态库类似windows中的静态lib 关于windows中的静态lib,可参考 Wi ...

  2. BP神经网络-- 基本模型

    转载:http://www.cnblogs.com/jzhlin/archive/2012/07/28/bp.html BP 神经网络中的 BP 为 Back  Propagation 的简写,最早它 ...

  3. SourceTree 过期,注册导入许可证

    参考这里:SourceTree过期,需要注册导入 SourceTree License 许可证 很详细 补充: 如果在 SourceTree 软件里注册失败,可以在网页注册. 如果其他邮箱不支持,可以 ...

  4. perl_nc.pl

    #!/usr/bin/perl use strict; use IO::Socket; use IO::Select; use Getopt::Std; my %option;getopts('lp: ...

  5. C基础 多用户分级日志库 sclog

    引言 - sclog 总的设计思路 sclog在之前已经内置到simplec 简易c开发框架中一个日志库. 最近对其重新设计了一下. 减少了对外暴露的接口. 也是C开发中一个轮子. 比较简单, 非常适 ...

  6. python中eval函数使用

    把字符串转换为字典: s = "{'a':1}" eval(s)

  7. LinkedList 源码分析

    LinkedList :双向链表结构, 内部存在frist节点 和 last节点.通过改变 首节点和 尾节点的引用来实现新增和修改 有一个内部类: //节点类,内部包括前节点和后节点,和数据项 // ...

  8. leetcode 之LRU Cache(26)

    很实际的一道题.定义一个双向链表list,方便插入和删除:定义一个哈希表,方便查找. 具体的,哈希表存放每个结点的key和它对应的结点的地址:访问结点时,如果结点存在,则将其交换到头部,同是更新哈希表 ...

  9. mysql5.7 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    mysql5.7初次登录使用提示 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before ...

  10. Djangp2.x版本报错找不到模版目录下的文件

    1.报错内容:django.template.exceptions.TemplateDoesNotExist: index.html 2.解决办法,在settings.py文件中,找到TEMPLATE ...