bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
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.
Output
Sample Input
to a reply and indicates the difference in height between the
tallest and shortest cow in the range.
Sample Output
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 -- 线段树的更多相关文章
- BZOJ 1636: [Usaco2007 Jan]Balanced Lineup
noip要来了,刷点基础水题. 题意: RMQ,给你N个数,Q个询问,每次查询[l,r]内,最大值减最小值是多少. 写的ST. 代码: #include<iostream> #includ ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )
RMQ.. ------------------------------------------------------------------------------- #include<cs ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Description 每天,农夫 John ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树
题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...
- ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...
- 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...
- BZOJ1636: [Usaco2007 Jan]Balanced Lineup
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 476 Solved: 345[ ...
随机推荐
- Perl6 Bailador框架(5):利用正则匹配路径
use v6; use Bailador; =begin pod 我们在路径设置上, 可以利正则表达式捕获的字符串作为子例程参数 =end pod get '/perl6/(.+)' => su ...
- python基础===autopep8__python代码规范
关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性. 官网地址:http ...
- H5对安卓WeView开发中的影响
1.body,或者html 高度为100% 会导致下拉直接触发原生的刷新控件,而不是webView滑动到顶部后刷新,以及不会执行onScrollChanged 方法,并且getScrollY 总是返 ...
- VPS性能综合测试(7):服务器压力测试,VPS系统负载测试
1.可能有的VPS主机使用性能测评工具得出的结果很优秀,但是最终运用到实际生产时却发现VPS主机根本无法承受理论上应该达到的流量压力,这时我们就不得不要怀疑VPS商是不是对VPS主机的参数进行了“篡改 ...
- Github精选 – 适合移动端的HTML5 Datepicker
2016-01-12 (updated: 2017-01-07) 15731 通过 HTML5 的 <input> 新属性可以简单方便地调用手机的原生 Datepicker,但功能较弱, ...
- How ConcurrentHashMap offers higher concurrency without compromising thread safety
https://www.ibm.com/developerworks/library/j-jtp08223/
- win10更换登陆背景和关闭锁屏
更换登陆背景图 用国外一个hacker写的工具,可在github上下载,或者下载我上传到百度网盘的备份.win10默认的登陆背景太难看了. 去掉锁屏图片 登陆界面其实本身就是一种锁屏,为什么还要单独搞 ...
- qtp录制时间控件不允许用户手动输入的解决办法
qtp录制时间控件不允许用户手动输入的解决办法 [前面的话] 一边学习qtp,一边用自己的项目试着写代码,而遇到一个问题就会让自己卡壳很久,这次也是这样的,在写好了登录代码以后,自己就试着写第一个预订 ...
- CentOS7.5安装teamviwer13
1.首先到官网下载teamviewer13的rpm包 https://www.teamviewer.com/zhcn/download/linux/ 2.安装 安装依赖包 http://mirror. ...
- springboot的mvn与gradle启动方式
1 很简单,直接鼠标右击项目,run启动项目 2 项目目录下的命令行 mvn spring-boot:run 或者 gradlew bootRun 3 进入libs下或者target下 java ...