线段树 区间查询最大值,单体修改 hdu 1754
1 #include<cstdio> 2 #include<algorithm> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<iostream> 7 using namespace std; 8 const int maxn=2e5+10; 9 int ary[maxn]; 10 struct node 11 { 12 int l,r,val; 13 }tree[maxn<<2]; 14 void push_up(int cur) 15 { 16 tree[cur].val=max(tree[cur*2].val,tree[cur*2+1].val); 17 } 18 void build(int l,int r,int cur) 19 { 20 tree[cur].l=l,tree[cur].r=r; 21 tree[cur].val=0; 22 if(l==r){ 23 tree[cur].val=ary[l]; 24 return; 25 } 26 int mid=(l+r)/2; 27 build(l,mid,cur*2); 28 build(mid+1,r,cur*2+1); 29 push_up(cur); 30 } 31 int query(int l,int r,int cur) 32 { 33 34 if(l<=tree[cur].l&&r>=tree[cur].r) 35 return tree[cur].val; 36 int mid=(tree[cur].l+tree[cur].r)/2; 37 int ans=0; 38 if(l<=mid) ans=max(ans,query(l,r,cur*2)); 39 if(r>mid) ans=max(ans,query(l,r,cur*2+1)); 40 return ans; 41 } 42 void Update(int base,int price,int cur) 43 { 44 if(tree[cur].l==tree[cur].r){ 45 tree[cur].val=price; 46 return; 47 } 48 int mid=(tree[cur].l+tree[cur].r)/2; 49 if(base<=mid) Update(base,price,cur*2); 50 else Update(base,price,cur*2+1); 51 push_up(cur); 52 } 53 int main() 54 { 55 int n,m; 56 while(scanf("%d%d",&n,&m)!=EOF){ 57 for(int i=1;i<=n;i++) scanf("%d",&ary[i]); 58 build(1,n,1); 59 while(m--){ 60 char tmp; 61 cin>>tmp; 62 if(tmp=='Q'){ 63 int l,r; 64 scanf("%d%d",&l,&r); 65 int ans=query(l,r,1); 66 printf("%d\n",ans); 67 } 68 else{ 69 int l,r; 70 scanf("%d%d",&l,&r); 71 Update(l,r,1); 72 } 73 } 74 } 75 return 0; 76 }