博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3264 Balanced Lineup RMQ问题
阅读量:5249 次
发布时间:2019-06-14

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

Balanced Lineup

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=3264

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

 

HINT

 

题意

给你n个数,然后查询区间的最大值减去最小值是多少

 

题解:

线段树RMQ,也可以ST来做,都是nlogn的复杂度

 

代码:

 

//qscqesze#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define maxn 200001#define mod 10007#define eps 1e-9int Num;char CH[20];//const int inf=0x7fffffff; //нчоч╢Сconst int inf=0x3f3f3f3f;/*inline void P(int x){ Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts("");}*///**************************************************************************************inline ll read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}inline void P(int x){ Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts("");}struct node{ int l,r,ma,mi;}a[maxn];int d[maxn];void build(int x,int l,int r){ a[x].l=l,a[x].r=r; a[x].ma=-inf,a[x].mi=inf; if(l==r) { a[x].ma=d[l]; a[x].mi=d[l]; return; } else { int mid=(l+r)>>1; build(x<<1,l,mid); build(x<<1|1,mid+1,r); a[x].ma=max(a[x<<1].ma,a[x<<1|1].ma); a[x].mi=min(a[x<<1].mi,a[x<<1|1].mi); }}int query1(int x,int st,int ed){ int l=a[x].l,r=a[x].r; if(st<=l&&r<=ed) return a[x].mi; else { int mid=(l+r)>>1; int mi1=inf,mi2=inf; if(st<=mid) mi1=query1(x<<1,st,ed); if(ed>mid) mi2=query1(x<<1|1,st,ed); return min(mi1,mi2); }}int query2(int x,int st,int ed){ int l=a[x].l,r=a[x].r; if(st<=l&&r<=ed) return a[x].ma; else { int mid=(l+r)>>1; int mi1=-inf,mi2=-inf; if(st<=mid) mi1=query2(x<<1,st,ed); if(ed>mid) mi2=query2(x<<1|1,st,ed); return max(mi1,mi2); }}int main(){ int n=read(),q=read(); int dd,ee; for(int i=1;i<=n;i++) d[i]=read(); build(1,1,n); for(int i=0;i

 

转载于:https://www.cnblogs.com/qscqesze/p/4447979.html

你可能感兴趣的文章
bzoj 3991: [SDOI2015]寻宝游戏
查看>>
Linux学习-1进程
查看>>
理解Javascript的闭包
查看>>
设计模式之简单工厂模式
查看>>
集成学习算法
查看>>
构造方法和final,static关键字
查看>>
cmd 下切换目录
查看>>
从程序员到项目经理(29):怎样写文档
查看>>
(原创)如何将Nios II硬件和软件合成一个文件(NIOS II)(硬件)(软件)(合并)...
查看>>
浅谈java volatile
查看>>
把linux可执行程序做成一个服务[转]
查看>>
docker入门基础(八)
查看>>
1.tomcat部署项目的几种方式和weblogic部署方式及一点通讯
查看>>
c语言在windows下和Mac下的不同表现!
查看>>
单链表实现的栈
查看>>
AC自动机
查看>>
用C#实现网络爬虫(二)
查看>>
Android 动画之ScaleAnimation应用详解
查看>>
【转】WEKA 中用于学习方案的通用选项
查看>>
java IO和NIO区别
查看>>