uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36
预处理,RMQ求区间最大值。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; typedef long long LL;
const int N = ;
const int M = ;
int pre[N], RMQ[M][N >> ];
int dfs(LL n) {
if (n <= ) cout << n << endl;
if (n < N && pre[n]) return pre[n];
int tmp;
if (n & ) tmp = dfs(n * + ) + ;
else tmp = dfs(n >> ) + ;
if (n < N) pre[n] = tmp;
return tmp;
} void PRE() {
pre[] = ;
for (int i = , end = N >> ; i < end; i++) if (pre[i] == ) dfs(i);
// for (int i = 1; i < 20; i++) cout << i << ' ' << pre[i] << endl;
// prepare RMQ
for (int i = , end = N >> ; i < end; i++) RMQ[][i] = pre[i];
for (int i = ; i < M; i++) {
for (int j = , end = (N >> ) - ( << i); j <= end; j++) {
RMQ[i][j] = max(RMQ[i - ][j], RMQ[i - ][j + ( << i - )]);
}
}
} int query(int l, int r) {
if (l > r) swap(l, r);
int ep = (int) log2((double) r - l + );
return max(RMQ[ep][l], RMQ[ep][r - ( << ep) + ]);
} int main() {
PRE();
int l, r;
while (cin >> l >> r) cout << l << ' ' << r << ' ' << query(l, r) << endl;
return ;
}
——written by Lyon