tvyj 1039 忠诚2 发表于 2016-12-12 | 分类于 ~❤程序❤~ , 数据结构 , 线段树 单点修改 区间查询 题目链接:tyvj 忠诚2 模板题目,感觉单点修改还不是太难,没啥好注意的地方,自己Y Y就挺好的。直接粘代码:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>using namespace std;int map[100100];int now[100100*4];int min(int a,int b){ if(a<=b) return a; else return b;}void build(int l,int r,int pos){ if(l==r) now[pos]=map[l]; else { int mid=(l+r)/2,lson=pos*2,rson=pos*2+1; build(l,mid,lson),build(mid+1,r,rson); now[pos]=min(now[lson],now[rson]); }}void add(int l,int r,int pos,int where,int w){ if(l==r) now[pos]=w; else { int mid=(l+r)/2,lson=pos*2,rson=pos*2+1; if(mid<where) { add(mid+1,r,rson,where,w); } else { add(l,mid,lson,where,w); } now[pos]=min(now[lson],now[rson]); }}int ask(int l,int r,int pos,int fr,int to){ if(l==fr&&r==to) return now[pos]; else { int mid=(l+r)/2,lson=pos*2,rson=pos*2+1; int ans=247483647; if(mid>=to) { ans=ask(l,mid,lson,fr,to); } else if(mid<fr) { ans=ask(mid+1,r,rson,fr,to); } else { ans=min(ask(l,mid,lson,fr,mid),ask(mid+1,r,rson,mid+1,to)); } return ans; }}int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&map[i]); } build(1,n,1); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&z,&x,&y); if(z==1) { printf("%d ",ask(1,n,1,x,y)); } else { add(1,n,1,x,y); } } return 0;}