POJ 3264-Balanced Lineup(RMQ-ST演算法)
Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 51826 | Accepted: 24285 | |
Case 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 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
Source
求N個數中給定區間L-R中最大值與最小值的差。
以前這題作為入門題用線段樹搞了搞,突然翻出來用RMQ水了一發。
坑爹的是cin、cout會超時…
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 50010
int a[MAXN],dpmin[MAXN][20],dpmax[MAXN][20];
int n,q;
void rmq()
{
int temp=(int)(log((double)n)/log(2.0));
for(int i=0; i<n; i++)
dpmin[i][0]=a[i],dpmax[i][0]=a[i];
for(int j=1; j<=temp; j++)
for(int i=0; i<=n-(1<<j); i++)
{
dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
}
}
int Minimum(int s,int e)
{
int k=(int)(log((double)e-s+1)/log(2.0));
return min(dpmin[s][k],dpmin[e-(1<<k)+1][k]);
}
int Maxmum(int s,int e)
{
int k=(int)(log((double)e-s+1)/log(2.0));
return max(dpmax[s][k],dpmax[e-(1<<k)+1][k]);
}
int main()
{
scanf("%d%d",&n,&q);
memset(a,0,sizeof(a));
memset(dpmin,0,sizeof(dpmin));
memset(dpmax,0,sizeof(dpmax));
for(int i=0; i<n; ++i)//下標0-n-1
scanf("%d",&a[i]);
rmq();//dp預處理
for(int i=0; i<q; ++i)
{
int s,e;
scanf("%d%d",&s,&e);
--s,--e;
if(s==e) printf("0\n");
else printf("%d\n",Maxmum(s,e)-Minimum(s,e));
}
return 0;
}
相關文章
- POJ 3264-Balanced Lineup詳解(線段樹區間求值)
- 【RMQ】poj 3264 Balanced LineupMQ
- POJ3264 Balanced Lineup【RMQ】MQ
- POJ 3264 Balanced Lineup【RMQ問題】MQ
- POJ 3264 Balanced Lineup(簡單的RMQ)MQ
- poj--3264Balanced Lineup+ST演算法求區間最大最小值演算法
- POJ 3264 Balanced Lineup 線段樹入門(點的查詢)
- POJ 基本演算法演算法
- poj 3436 最大流的增廣路演算法演算法
- POJ 1113-Wall(凸包-Graham演算法)演算法
- POJ 1511 Invitation Cards(最短路spfa演算法)演算法
- POJ3461-KMP演算法的簡單運用KMP演算法
- POJ 3565 Ants (最小權完美匹配 KM演算法)演算法
- POJ 1523-SPF(Tarjan演算法-關節點)演算法
- poj1258--資料結構prim演算法資料結構演算法
- POJ 3318 Matrix Multiplication(隨機化演算法)隨機演算法
- POJ 2195-Going Home(KM演算法/最小費用最大流演算法)Go演算法
- POJ 1330 LCA最近公共祖先 離線tarjan演算法演算法
- POJ3660 Cow Contest【Floyd演算法 傳遞閉包】演算法
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割演算法)演算法
- POJ 1325-Machine Schedule(二分圖匹配-匈牙利演算法)Mac演算法
- POJ 3592 Instantaneous Transference 圖論演算法tarjan+spfa圖論演算法
- poj2400 KM演算法二分圖的完美匹配演算法
- POJ3268 Silver Cow Party【Dijkstra演算法+思維】演算法
- POJ 2914-Minimum Cut(Stoer_Wagner最小割演算法)演算法
- POJ 1523 SPF (無向圖求割點 tarjan演算法)演算法
- POJ-3461 Oulipo-匹配的字元有幾個(KMP演算法)字元KMP演算法
- POJ 2186 Popular Cows(強連通分量縮點,Tarjan演算法)演算法
- B - Gold Balanced Lineup解題報告(張浩盛倫)Go
- POJ2387 Til the Cows Come Home【最短路 Dijkstra演算法】演算法
- 演算法學習之路|POJ-2479最大子串和(簡單dp)演算法
- POJ 1459-Power Network(最大流-Edmond-Karp演算法)演算法
- POJ 1469-COURSES(二分圖匹配入門-匈牙利演算法)演算法
- POJ 3461 kmpKMP
- poj3417
- POJ 3014:Asteroids(二分匹配,匈牙利演算法)AST演算法
- POJ 3169(Bellman-Ford演算法,差分約束系統)演算法
- POJ 2115-C Looooops-擴充套件歐幾里德演算法OOP套件演算法