POJ——3264Balanced Lineup(RMQ模版水题)
来源:互联网 发布:斑马zebra笔怎么样知乎 编辑:程序博客网 时间:2024/06/11 19:10
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 44112 Accepted: 20713Case Time Limit: 2000MS
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.
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 31734251 54 62 2
Sample Output
630
代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<sstream>#include<cstring>#include<cstdio>#include<string>#include<deque>#include<stack>#include<cmath>#include<queue>#include<set>#include<map>#define INF 0x3f3f3f3f#define MM(x) memset(x,0,sizeof(x))using namespace std;typedef long long LL;const int N=50010;int n,q;int maxm[N][18],minm[N][18];void rmq(){int i,j;for (j=1; (1<<j)<=n; j++){for (i=1; i+(1<<j)-1<=n; i++){minm[i][j]=min(minm[i][j-1],minm[i+(1<<(j-1))][j-1]);maxm[i][j]=max(maxm[i][j-1],maxm[i+(1<<(j-1))][j-1]);}}}int main(void){int i,j,l,r;while (~scanf("%d%d",&n,&q)){MM(maxm);MM(minm);for (i=1; i<=n; i++){scanf("%d",&maxm[i][0]);minm[i][0]=maxm[i][0];}rmq();for (i=0; i<q; i++){scanf("%d%d",&l,&r);int k=(int)log2(r-l+1);int ans=max(maxm[l][k],maxm[r-(1<<k)+1][k])-min(minm[l][k],minm[r-(1<<k)+1][k]);printf("%d\n",ans);}}return 0;}
0 0
- POJ——3264Balanced Lineup(RMQ模版水题)
- POJ 3264 Balanced Lineup[RMQ入门题]
- POJ 3264 Balanced Lineup【RMQ模板题】
- POJ-3264 Balanced Lineup(rmq模板题)
- poj 3264 Balanced Lineup (RMQ))
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(简单RMQ)
- Balanced Lineup(RMQ)(POJ 3264)
- POJ 3264--Balanced Lineup(RMQ问题)
- POJ 3264 Balanced Lineup(RMQ详解)
- POJ 3264 Balanced Lineup(RMQ模板)
- POJ 题目3264 Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- Springmvc 拦截器
- 如何编写让别人能读懂的代码?
- 1010. 一元多项式求导 (25)
- js 实现banner轮播
- 解决ViewPager嵌套ViewPager滑动冲突问题
- POJ——3264Balanced Lineup(RMQ模版水题)
- ACM 数据结构-并查集 HDU 1232 畅通工程
- Hibernate关联映射-一对一(主外键,单双方向)
- 花开,只为芳溢人间
- 项目2——动物这样叫2
- hrbust/哈理工oj 1592 六芒星【矩阵快速幂】
- 快速学习者的高效学习策略
- Java之------IO从基础到加强(二)
- myeclipse中项目名有红叉,但项目中文件没有报错的解决办法