题目链接:
点击打开链接
代码:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1000005;
int n;
int a[maxn],tree[maxn*4];
void build(int root,int l,int r){
if(l == r){
tree[root] = a[l];
return;
}
int mid = (l+r) >> 1;
build(root*2, l, mid);
build(root*2+1, mid+1, r);
tree[root] = min(tree[root*2], tree[root*2+1]);
}
int cal(int root,int l,int r,int x,int y){
if(x<=l && r<=y){
return tree[root];
}
int mid = (l+r) >> 1;
int ans1=-1,ans2=-1;
if(x<=mid) ans1 = cal(root*2,l,mid,x,y);
if(mid<y) ans2 = cal(root*2+1,mid+1,r,x,y);
if(ans1==-1) return ans2;
else if(ans2 == -1) return ans1;
else return min(ans1, ans2);
}
void change(int root,int l,int r,int pos,int num){
if(l == r) {tree[root] = num;return;}
int mid = (l+r) >> 1;
if(pos<=mid) change(root*2,l,mid,pos,num);
else change(root*2+1,mid+1,r,pos,num);
tree[root] = min(tree[root*2], tree[root*2+1]);
}
int main(){
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
build(1,1,n);
int q,type;
scanf("%d",&q);
while(q--){
scanf("%d",&type);
if(type == 0){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",cal(1,1,n,l,r));
}
else {
int pos,num;
scanf("%d%d",&pos,&num);
change(1,1,n,pos,num);
}
}
return 0;
}
自己写了一遍线段树..复习了一下。
记得return..以及,记得更新...