http://acm.hdu.edu.cn/showproblem.php?pid=1754
代码:
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; #define maxn 200005 int tree[4*maxn]; void build(int left,int right,int root) { if(left==right) { scanf("%d",&tree[root]); } else { int mid=(left+right)/2; build(left,mid,root*2); build(mid+1,right,root*2+1); tree[root]=max(tree[root*2],tree[root*2+1]); } } void Pointupdate(int pos,int newn,int left,int right,int root) { if(left==right) { tree[root]=newn; } else { int mid=(left+right)/2; if(pos<=mid) Pointupdate(pos,newn,left,mid,root*2); else Pointupdate(pos,newn,mid+1,right,root*2+1); tree[root]=max(tree[root*2],tree[root*2+1]); } } int query(int x,int y,int left,int right,int root) { if(x<=left&&right<=y) { return tree[root]; } else { int cnt=0; int mid=(left+right)/2; if(x<=mid) cnt=max(cnt,query(x,y,left,mid,root*2)); if(y>mid) cnt=max(cnt,query(x,y,mid+1,right,root*2+1)); return cnt; } } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); while(m--) { char ch[3]; int x,y; scanf("%s%d%d",ch,&x,&y); if(ch[0]=='Q') printf("%d ",query(x,y,1,n,1)); else Pointupdate(x,y,1,n,1); } } return 0; }