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

Time Limit: 5000MS     Memory Limit: 65536K

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


Sample Output


Source

 
 
题意:
一个农夫有N头牛,每头牛的高度不同,我们需要找出区间[a,b]中最高的牛和最低的牛的高度差。
 
题解:
明显是区间最值问题,线段树和RMQ
 
线段树做法(耗时2.2s):
 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e5+;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n,q;
struct node
{
int l;
int r;
int MAX;
int MIN;
}SegTree[<<]; void PushUp(int rt)
{
SegTree[rt].MAX=max(SegTree[rt<<].MAX,SegTree[rt<<|].MAX);
SegTree[rt].MIN=min(SegTree[rt<<].MIN,SegTree[rt<<|].MIN);
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
if(l==r)
{
scanf("%d",&SegTree[rt].MAX);
SegTree[rt].MIN=SegTree[rt].MAX;
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
PushUp(rt);
} int Query_MAX(int L,int R,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)//一次也没有被涂过
{
return SegTree[rt].MAX;
}
int MAX=;
int mid=(l+r)>>;
if(L<=mid)
MAX=max(MAX,Query_MAX(L,R,rt<<));
if(R>mid)
MAX=max(MAX,Query_MAX(L,R,rt<<|));
return MAX;
} int Query_MIN(int L,int R,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)//一次也没有被涂过
{
return SegTree[rt].MIN;
}
int MIN=INF;
int mid=(l+r)>>;
if(L<=mid)
MIN=min(MIN,Query_MIN(L,R,rt<<));
if(R>mid)
MIN=min(MIN,Query_MIN(L,R,rt<<|));
return MIN;
} int main()
{
scanf("%d %d",&n,&q);
Build(,n,);
for(int i=;i<=q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",Query_MAX(a,b,)-Query_MIN(a,b,));
}
return ;
}
RMQ做法:
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
using namespace std; const int N = ;
int FMAX[N][], FMIN[N][]; void RMQ(int n)
{
for(int j = ; j != ; ++j)
{
for(int i = ; i <= n; ++i)
{
if(i + ( << j) - <= n)
{
FMAX[i][j] = max(FMAX[i][j - ], FMAX[i + ( << (j - ))][j - ]);
FMIN[i][j] = min(FMIN[i][j - ], FMIN[i + ( << (j - ))][j - ]);
}
}
}
} int main()
{
int num, query;
int a, b;
while(scanf("%d %d", &num, &query) != EOF)
{
for(int i = ; i <= num; ++i)
{
scanf("%d", &FMAX[i][]);
FMIN[i][] = FMAX[i][];
}
RMQ(num);
while(query--)
{
scanf("%d%d", &a, &b);
int k = (int)(log(b - a + 1.0) / log(2.0));
int maxsum = max(FMAX[a][k], FMAX[b - ( << k) + ][k]);
int minsum = min(FMIN[a][k], FMIN[b - ( << k) + ][k]);
printf("%d\n", maxsum - minsum);
}
}
return ;
}
 
最后推荐一篇写的挺好的一篇博客:https://blog.csdn.net/weixin_43272781/article/details/83443310
 
 
 
 

最新文章

  1. Java-IO之DeflaterOutputStream和InflaterOutputStream
  2. ios block 导致的循环引用
  3. SSD性能对比
  4. Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心
  5. Android_listView
  6. 托管host
  7. OC——动态添加Button和监听UIAlertView按钮
  8. java基础练习 11
  9. redhat7.3配置163 yum源
  10. 如何安装使用Impala
  11. JavaScript 逗号表达式
  12. iOS开发中@property的属性weak nonatomic strong readonly等介绍
  13. fetch数据请求的封装
  14. 一脸懵逼学习Hadoop-HA机制(以及HA机制的配置文件,测试)
  15. centos7.6 安装与配置 MongoDB yum方式
  16. 非常可乐 HDU - 1495
  17. Ubuntu 搭建 Zerotier One MOON 根目录服务器
  18. params关键字、工具辅助类与、加密与解密
  19. How to access business objects with their related ObjectSpaces (Multi-Database Environment)
  20. java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx

热门文章

  1. vue知识点散记
  2. python刷LeetCode:26. 删除排序数组中的重复项
  3. 强制浏览器以IE8版本运行
  4. html—表单控件
  5. 尝试解决 : Microsoft Visual C++ 14.0 is required 的问题
  6. NABCD模型——星遇
  7. Swift 中调试状态下打印日志
  8. 指针数组的初始化和遍历,并且通过for循环方式、函数传参方式进行指针数组的遍历
  9. poj 3013 最短路SPFA算法
  10. nginx_tcp_proxy代理酸酸乳