ST
int dp[maxn][20]; int a[maxn]; void RMQ_init(int n) { int i,j,k; for(i = 1; i <= n; i++) dp[i][0] = a[i]; k = (int) (log((double)n + 0.2) / log(2.0)); for(j = 1; j <= k; j++) for(i = 1; i + (1 << j) - 1 <= n; i++) dp[i][j] = min(dp[i][j-1], dp[i + (1 << (j-1))][j-1]); }
void RMQ_init(int n) { for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { dp[i][j][0][0] = a[i][j]; } int k = (int) (log((double)n + 0.2) / log(2.0)); for(int x = 0; x <= k; x++) for(int y = 0; y <= k; y++) { if(!x && !y) continue; for(int i = 0; i + (1<<x) - 1 <= n; i++) for(int j = 0; j + (1<<y) - 1 <= n; j++) { if(x == 0) dp[i][j][x][y] = max(dp[i][j][x][y-1], dp[i][j+(1<<(y-1))][x][y-1]); else dp[i][j][x][y] = max(dp[i][j][x-1][y], dp[i+(1<<(x-1))][j][x-1][y]); } } } int RMQ(int x1, int y1, int x2, int y2) { int kx = log(double(x2 - x1 + 1)) / log(2.0); int ky = log(double(y2 - y1 + 1)) / log(2.0); int ans = 0; ans = max(ans, dp[x1][y1][kx][ky]); ans = max(ans, dp[x2-(1<<kx)+1][y1][kx][ky]); ans = max(ans, dp[x1][y2-(1<<ky)+1][kx][ky]); ans = max(ans, dp[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky]); return ans; }
原文地址:http://blog.csdn.net/u011686226/article/details/26860375