Title Link: http://poj.org/problem?id=3264
The main problem: N number, the difference between the maximum and minimum value of the interval [l,r];
Topic Analysis:
rqm:dp[I [j], I start length is the interval maximum of the length of the 2^j;
O (nlog N) preprocessing interval value, O (1) of the query;
Code:
author:acsorry//result:accept#include<iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <queue># include<deque> #include <stack> #include <map> #include <set> #define INF 1<<29#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int N=1000007;int dp[n][ 20]; 2^20 already 10^6 array//dp[i][j] 2^j length from I to the interval of the most value int a[n]; Subscript starting from 1 void rmqinit (int n)//n array size {for (int i=1;i<=n;i++) dp[i][0]=a[i]; for (int j=1, (1<<j) <=n;j++) {for (int i=1;i+ (1<<J) -1<=n;i++) {dp[i][j]=min (DP[I][J-1],DP [I+ (1<< (j-1))] [J-1]); }}}int query (int l,int R)//queries [L,r] interval Maximum {int M=floor (log (r-l+1.0)/log (2.0)); return min (dp[l][m],dp[r-(1<<m) +1][m]);} int dp1[n][20];void rmqInit1 (int N)//n An array size {for (int i=1;i<=n;i++) dp1[i][0]=a[i]; for (int j=1; (1<<j) <=n;j++) {for (int i=1;i+ (1<<J) -1<=n;i++) {Dp1[i][j]=max (dp1[i][j-1],dp1[i+ (1<<)) j-1] ); }}}int query1 (int l,int R)//query [L,r] interval value {int M=floor (log (r-l+1.0)/log (2.0)); Return Max (dp1[l][m],dp1[r-(1<<m) +1][m]);} int main () {int n,m; while (scanf ("%d%d", &n,&m) ==2) {for (int i=1;i<=n;i++) scanf ("%d", a+i); Rmqinit (n); rmqInit1 (n); int l,r; Cout<<query (1,6) <<endl; while (m--) {scanf ("%d%d", &l,&r); printf ("%d\n", Query1 (l,r)-query (l,r)); }} return 0;}
I would rather have a wonderful life than a mediocre life.
POJ 3264 Balanced lineup[rmq Introductory question]