http://www.cnblogs.com/ambition/archive/2011/04/06/bit_rmq.html
代码如下:
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> #include<string> #define LL long long #define MAXN 100005 using namespace std; int N, seq[MAXN], idx[MAXN]; inline int lowbit(int x) { return x & (-x); } void modify(int p) { for (int i = p; i <= N; ++i) { idx[i] = seq[i]; for (int j = 1; j < lowbit(i); j <<= 1) { idx[i] = max(idx[i], idx[i-j]); } } } int query(int L, int R) { int ret = seq[R]; while (1) { ret = max(ret, seq[R]); if (L == R) break; for (R -= 1; R-L >= lowbit(R); R -= lowbit(R)) { ret = max(ret, idx[R]); } } return ret; } int main( ) { int Q, L, R, op, x, y; while (scanf("%d", &N) == 1) { for (int i = 1; i <= N; ++i) { scanf("%d", &seq[i]); } modify(1); scanf("%d", &Q); while (Q--) { scanf("%d", &op); if (op == 1) { scanf("%d %d", &L, &R); printf("%d\n", query(L, R)); } else { scanf("%d %d", &x, &y); seq[x] = y; modify(x); } } } return 0; }