POJ3264----区间最值Balanced Lineup
来源:互联网 发布:java 单个键值对 编辑:程序博客网 时间:2024/05/19 09:38
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 40501 Accepted: 19039Case 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 andQ.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤A ≤ B ≤ N), representing the range of cows from A toB inclusive.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤A ≤ B ≤ N), representing the range of cows from A toB 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
Source
USACO 2007 January Silver
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<stack>#include<climits>#include<vector>using namespace std;const int N = 2e5+5;int maxn[N][20];int mm[N][20];vector<int>A;void init(const vector<int>& A){ int n=A.size(); for(int i=0;i<n;i++) maxn[i][0]=mm[i][0]=A[i]; for(int j=1;(1<<j)<=n;j++) for(int i=0;i+(1<<j)-1<n;i++) { mm[i][j]=min(mm[i][j-1],mm[i+(1<<(j-1))][j-1]); maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]); }}int RMQ(int L,int R){ int k=0; while((1<<(k+1))<=R-L+1)k++; return max(maxn[L][k],maxn[R-(1<<k)+1][k])-min(mm[L][k],mm[R-(1<<k)+1][k]);}int main(){ int m,n; int L,R; int x; while(~scanf("%d%d",&n,&m)) { A.clear(); for(int i=0;i<n;i++) { scanf("%d",&x); A.push_back(x); } init(A); while(m--) { scanf("%d%d",&L,&R); printf("%d\n",RMQ(L-1,R-1)); } } return 0;}
0 0
- POJ3264----区间最值Balanced Lineup
- poj3264-Balanced Lineup(点更新,区间最值)
- POJ3264 Balanced Lineup(线段树,区间最值)
- POJ3264 Balanced Lineup
- POJ3264:Balanced Lineup
- poj3264 - Balanced Lineup
- POJ3264 Balanced Lineup
- POJ3264:Balanced Lineup
- POJ3264 Balanced Lineup
- POJ3264-Balanced Lineup RMQ
- poj3264 Balanced Lineup
- [Poj3264][Bzoj1699]Balanced Lineup
- POJ3264 Balanced Lineup
- poj3264 Balanced Lineup
- [POJ3264]Balanced Lineup
- POJ3264 Balanced Lineup
- poj3264 Balanced Lineup
- POJ3264 Balanced Lineup (RMQ)
- WinCE6.0BSP移植(一)
- recyclerview 实现正方形网格效果
- Spring MVC + Hibernate + Maven: Crud操作示例
- MySQL数据库的约束
- Developing Universal Windows Apps 开发UWA应用 问答
- POJ3264----区间最值Balanced Lineup
- WinCE6.0BSP移植(二)
- Java中的Serializable浅谈,只是浅谈
- 电脑中文环境变量设置
- UpdateData、Invalidate、InvalidateRect和UpdateWindow及RedrawWindow
- poj 3468 A Simple Problem with Integers || 线段树
- hdu-2603
- 随笔
- MySQL性能分析与优化