http://acm.hdu.edu.cn/showproblem.php?pid=1754
解题思路:线段树啦
#include <stdio.h> #include <string.h> #define GetMax(a,b) a>b?a:b #define size 200001 struct seg_tree { int left,right; int max; }tree[size*4]; int val[size],N,M; void build(int num,int left,int right) { tree[num].left = left; tree[num].right = right; if(right-left>1) { int mid = (left+right)>>1; build(2*num,left,mid); build(2*num+1,mid,right); tree[num].max = GetMax(tree[2*num].max,tree[2*num+1].max); } else tree[num].max = GetMax(val[left],val[right]); } void InputData() { int i; for(i=1;i<=N;i++) scanf("%d",&val[i]); scanf("%d",&M); } void update(int num,int locate) { if (tree[num].right-tree[num].left==1) { tree[num].max = GetMax(val[tree[num].right],val[tree[num].left]); return; } int mid = (tree[num].left+tree[num].right)>>1; if (locate<=mid) update(2*num,locate); if(locate>=mid) update(2*num+1,locate); tree[num].max = GetMax(tree[2*num].max,tree[2*num+1].max); } int query(int num,int left,int right) { if(tree[num].left==left&&tree[num].right==right) return tree[num].max; int mid = (tree[num].left+tree[num].right)>>1; if(right<=mid) return query(2*num,left,right); else if(left>=mid) return query(2*num+1,left,right); else { int max1,max2; max1 = query(2*num,left,mid); max2 = query(2*num+1,mid,right); return GetMax(max1,max2); } } int main() { char s[2]; int a,b; while (scanf("%d%d",&N,&M)!=EOF) { InputData(); build(1,1,N); while (M--) { scanf("%s%d%d",s,&a,&b); if (s[0]=='Q') printf("%d/n",query(1,a,b)); else val[a] = b,update(1,a); } } return 0; }