博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TOJ1698: Balanced Lineup
阅读量:6173 次
发布时间:2019-06-21

本文共 2358 字,大约阅读时间需要 7 分钟。

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

Source

输出区间最大最小值之差,RMQ

 

#include
using namespace std;const int N=80005;int dmi[N][30],dma[N][30],f[N];int n,q,l,r;void RMQ_init(){ for(int j=1; (1<
<=n; j++) for(int i=1; i+j-1<=n; i++) dmi[i][j]=min(dmi[i][j-1],dmi[i+(1<<(j-1))][j-1]),dma[i][j]=max(dma[i][j-1],dma[i+(1<<(j-1))][j-1]);}int RMQ(int l,int r){ int k=f[r-l+1]; return max(dma[l][k],dma[r-(1<
<

 

线段树

#include
using namespace std;#define lson l,mi,rt<<1#define rson mi+1,r,rt<<1|1struct T{ int ma,mi,l,r;}tree[200000];int h[50005];int ma,mi;void build(int l,int r,int rt){ tree[rt].l=l,tree[rt].r=r; if(l==r) { tree[rt].ma=tree[rt].mi=h[l]; return; } int mi=(l+r)>>1; build(lson); build(rson); tree[rt].ma=max(tree[rt<<1].ma,tree[rt<<1|1].ma); tree[rt].mi=min(tree[rt<<1].mi,tree[rt<<1|1].mi);}void findma(int l,int r,int rt){ if(tree[rt].l==l&&tree[rt].r==r) { if(tree[rt].ma>ma)ma=tree[rt].ma; return; } int mi=(tree[rt].l+tree[rt].r)>>1; if(mi>=r) findma(l,r,rt<<1); else if(mi
>1; if(mi>=r) findmi(l,r,rt<<1); else if(mi

 

 

转载于:https://www.cnblogs.com/BobHuang/p/8698477.html

你可能感兴趣的文章
博客园美化专用图片链接
查看>>
HDU_1969_二分
查看>>
高等代数葵花宝典—白皮书
查看>>
一种简单的图像修复方法
查看>>
基于DobboX的SOA服务集群搭建
查看>>
C#设计模式之装饰者
查看>>
[noip模拟20170921]模版题
查看>>
获取ip
查看>>
Spring Shell简单应用
查看>>
移动app可开发的意见于分析
查看>>
周总结7
查看>>
类似OutLook布局的开源控件XPanderControls
查看>>
Web前端工程师成长之路——知识汇总
查看>>
[2018-9-4T2]探索黑暗dark
查看>>
【学术信息】中科院2019年学术期刊分区-综合性期刊
查看>>
ShareObject离线存储相关
查看>>
C++ XML
查看>>
windows批处理 打开exe后关闭cmd
查看>>
Flask开发系列之快速入门
查看>>
关于SaveChanges
查看>>