POJ 3264 Balanced Lineup【RMQ問題】
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
題解:ST演算法模板題,ST演算法在上一篇文章中。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 50005;
int F[maxn][32];
int G[maxn][32];
int a[maxn], n, q;
int minx = 99999999, maxx = -1;
void ST_prework() {
for(int i = 1; i<= n; i++) F[i][0] = a[i], G[i][0] = a[i];
int t = log(n*1.0) / log(2.0) + 1;
for(int j = 1; j < t; j++) {
for(int i = 1; i <= n - (1<<j) + 1; i++){
F[i][j] = max(F[i][j-1], F[i+(1<<(j-1))][j-1]);
G[i][j] = min(G[i][j-1], G[i+(1<<(j-1))][j-1]);
}
}
}
void ST_query(int l, int r){
int k = log((r-l+1)*1.0) / log(2.0);
maxx = max(F[l][k], F[r-(1<<k)+1][k]);
minx = min(G[l][k], G[r-(1<<k)+1][k]);
}
int main()
{
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
ST_prework();
while(q--){
int l, r;
scanf("%d %d", &l, &r);
ST_query(l, r);
printf("%d\n", maxx - minx);
}
return 0;
}
相關文章
- POJ3264 Balanced Lineup【RMQ】MQ
- poj--3264Balanced Lineup+ST演算法求區間最大最小值演算法
- poj--2019Cornfields+二維RMQ問題MQ
- $RMQ$問題($ST$表)MQ
- RMQ問題的各種解法MQ
- 資料結構——RMQ(ST表)問題資料結構MQ
- 棋盤問題 POJ - 1321
- POJ3984-迷宮問題
- POJ3984 迷宮問題【BFS】
- poj 1321 棋盤問題 回溯 JavaJava
- POJ1321棋盤問題(DFS)
- [題解]SP10606 Balanced Numbers
- RMQMQ
- Balanced Subsequences
- RMQ模板MQ
- 題解:CF1237E Balanced Binary Search Trees
- RMQ求lcaMQ
- 淺談RMQMQ
- POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)陣列
- 線段樹--RMQMQ
- HDU Find the hotel(RMQ)MQ
- 110-Balanced Binary Tree
- [題解]P3059 [USACO12NOV] Concurrently Balanced Strings G
- poj 1038 Bugs Integrated, Inc. 題解
- HDU 3183 A Magic Lamp (RMQ)LAMPMQ
- CF1237H Balanced Reversals
- 【Lintcode】1793. Balanced Sales Array
- Poj--3080Blue Jeans+KMP水題KMP
- HDU 6299-Balanced Sequence(貪心)
- poj 2031
- poj 3461
- RMQ的SSL配置最佳實踐MQ
- RMQ_第一彈_Sparse TableMQ
- HDU 2888 Check Corners(二維RMQ)MQ
- 第六章 數學問題 -------- 6.11【同餘方程組】POJ1006 生理週期
- HDU 3486 Interviewe(RMQ+二分)ViewMQ
- poj 3278 BFS
- POJ 2975 Nim