1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include<stdio.h> #define inf 2147483647 int tree[100005*4]={0},a[100005]={0};
int min(int a,int b){ if(a<b)return a; return b; }
void build(int rt,int st,int ed){ if(st==ed) tree[rt]=a[st]; else{ int mid=(st+ed)/2; build(rt*2,st,mid); build(rt*2+1,mid+1,ed); tree[rt]=min(tree[rt*2],tree[rt*2+1]); } }
int query(int rt,int nst,int ned,int qst,int qed){ if(qst>ned||qed<nst) return inf; if(qst<=nst&&qed>=ned) return tree[rt]; int mid=(nst+ned)/2; return min(query(rt*2,nst,mid,qst,qed),query(rt*2+1,mid+1,ned,qst,qed)); }
int main(){ int n,m; int i,x,y; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); for(i=1;i<=m;i++){ scanf("%d%d",&x,&y); printf("%d ",query(1,1,n,x,y)); } return 0; }
|