题目链接:AOJ 351
RMQ:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define debug 0
const int maxn = 100000 + 5;
int a[maxn],n,m,Max[maxn][20],Min[maxn][20];
void initRMQ() {
for (int i = 1; i <= n; i++) {
Max[i][0] = Min[i][0] = a[i];
}
for(int j = 1; j < 20; j++)//初始化
for(int i = 1; i <= n; i++)
if (i + (1 << j) - 1 <= n)
{
Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]);
Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
}
}
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif//debug
int l, r;
while (~scanf("%d%d", &n, &m))
{
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
initRMQ();
while (m--)
{
scanf("%d%d", &l, &r);
int x = (int)log2(r - l + 1);//query查询
int maxAns = max(Max[l][x], Max[r - (1 << x) + 1][x]);
int minAns = min(Min[l][x], Min[r - (1 << x) + 1][x]);
printf("%d\n", maxAns - minAns);
}
}
return 0;
}
线段树:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
#define debug 0
const int maxn = 100000 + 5;
int Max[maxn << 2], Min[maxn << 2];
int n, m, ans1, ans2;
#define Mid ((l + r) >> 1)
#define ls rt << 1, l , Mid
#define rs rt << 1 | 1, Mid + 1 , r
void build(int rt, int l, int r) {
if (l == r){
scanf("%d", &Max[rt]);//建树时读入
Min[rt] = Max[rt];
}else {
build(rt << 1, l, (l + r) >> 1);
build(rt << 1 | 1, ((l + r) >> 1) + 1, r);
Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);
}
}
void query(int rt, int l, int r, int L, int R) {//查询
if (L <= l && r <= R) {
ans1 = max(ans1, Max[rt]);
ans2 = min(ans2, Min[rt]);
}else {
if (L <= ((l + r) >> 1)){
query(ls, L, R);
}
if (R > ((l + r) >> 1)) {
query(rs, L, R);
}
}
}
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
int a, b;
while (~scanf("%d%d", &n, &m)) {
build(1, 1, n);
while (m--) {
ans1 = -0x3f3f3f3f;
ans2 = 0x3f3f3f3f;
//printf("%d\n", m);
scanf("%d%d", &a, &b);
query(1, 1, n, a, b);
printf("%d\n", ans1 - ans2);
}
}
return 0;
}