[POJ3264]Balanced Lineup(RMQ, ST算法)

题目链接:http://poj.org/problem?id=3264

典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法。

st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的最值一定是这两个区间合并后的最值,这条性质决定了这个dp子问题的重叠。可以利用这个性质预处理出这张表,只不过步长是2的幂次。

查询的时候也是如此,但是未必会精准地选中两个区间,不要紧,因为两个区间重叠的部分也会被自动算在求最值的内部。这个时候如果算的是区间和的话,要减去这一部分。(区间和的话直接用前缀和不就好了嘛)

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &(a))
 43 #define Rll(a) scanf("%lld", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f, sizeof(a))
 53 #define lrt rt << 1
 54 #define rrt rt << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long Uint;
 62 typedef pair<int, int> pii;
 63 typedef pair<LL, LL> pLL;
 64 typedef pair<string, LL> psi;
 65 typedef map<string, LL> msi;
 66 typedef vector<LL> vi;
 67 typedef vector<LL> vl;
 68 typedef vector<vl> vvl;
 69 typedef vector<bool> vb;
 70 
 71 const int maxn =50050;
 72 int n, q;
 73 int h[maxn];
 74 int dp[maxn][30][2];
 75 
 76 void st() {
 77     for(int i = 1; i <= n; i++) dp[i][0][0] = dp[i][0][1] = h[i];
 78     for(int j = 1; (1 << j) <= n; j++) {
 79         for(int i = 1; i + (1 << j) - 1 <= n; i++) {
 80             dp[i][j][0] = min(dp[i][j-1][0], dp[i+(1<<(j-1))][j-1][0]);
 81             dp[i][j][1] = max(dp[i][j-1][1], dp[i+(1<<(j-1))][j-1][1]);
 82         }
 83     }
 84 }
 85 
 86 int query(int l, int r) {
 87     int j = 0;
 88     while((1 << (j + 1)) <= r - l + 1) j++;
 89     return max(dp[l][j][1], dp[r-(1<<j)+1][j][1]) - min(dp[l][j][0], dp[r-(1<<j)+1][j][0]);
 90 }
 91 
 92 int main() {
 93 //    FRead();
 94     while(~scanf("%d%d",&n,&q)) {
 95         For(i, 1, n+1) Rint(h[i]);
 96         st();
 97         int l, r;
 98         W(q) {
 99             scanf("%d%d",&l,&r);
100             printf("%d
",query(l,r));
101         }
102     }
103     RT 0;
104 }
原文地址:https://www.cnblogs.com/kirai/p/5758306.html