原题链接:http://www.tyvj.cn/p/1038
第一次写线段树,挑简单的写。。。/(ㄒoㄒ)/~~
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Max_N 100010
#define _min(a,b) ((a)>(b)?(b):(a))
typedef struct _node{
int l, r, min;
}SegTree;
int ret[Max_N];
SegTree rec[400000];
void built(int x, int y, int cur){
rec[cur].l = x, rec[cur].r = y;
if (x == y){
rec[cur].min = ret[x];
return;
}
int mid = (x + y) >> 1;
built(x, mid, cur << 1);
built(mid + 1, y, (cur << 1) | 1);
rec[cur].min = _min(rec[cur << 1].min, rec[(cur << 1) | 1].min);
}
int query(int x, int y, int cur){
int l = rec[cur].l, r = rec[cur].r;
if (l == x && r == y) return rec[cur].min;
int mid = (l + r) >> 1;
if (y <= mid) return query(x, y, cur << 1);
if (x > mid) return query(x, y, (cur << 1) | 1);
int v1 = query(x, mid, cur << 1);
int v2 = query(mid + 1, y, (cur << 1) | 1);
return _min(v1, v2);
}
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int i, m, n, x, y;
while (~scanf("%d %d", &m, &n)){
for (i = 1; i <= m; i++) scanf("%d", &ret[i]);
built(1, m, 1);
for (i = 0; i < n; i++){
scanf("%d %d", &x, &y);
printf("%d", query(x, y, 1));
if (i < n - 1) printf(" ");
}
}
return 0;
}