Description
Input
Output
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
代码:
#include <cstdio> #include <cstring> #define maxn 200000 using namespace std; struct node { int left,right,max; int mid(){ return (left+right)>>1; } }tree[maxn*4]; int Max(int a,int b) { return (a>b)? a:b; } void CreatBtree(int l,int r,int rt){ tree[rt].left=l; tree[rt].right=r; //cout<<l<<" "<<r<<endl; if(l==r){ scanf("%d",&tree[rt].max); return; } int mid=tree[rt].mid(); CreatBtree(l,mid,rt<<1); CreatBtree(mid+1,r,rt<<1|1); tree[rt].max=Max(tree[rt<<1].max,tree[rt<<1|1].max); } void change(int rt,int k,int step){ if(tree[rt].left==tree[rt].right){ tree[rt].max=step; return; } else if(k<=tree[rt].mid()) change(rt<<1,k,step); else change(rt<<1|1,k,step); tree[rt].max=Max(tree[rt<<1].max,tree[rt<<1|1].max); } int Query(int rt,int l,int r){ if(tree[rt].left==l && tree[rt].right==r)return tree[rt].max; int ans; if(r<=tree[rt].mid()) ans=Query(rt<<1,l,r); else if(l>tree[rt].mid())ans=Query(rt<<1|1,l,r); else ans=Max(Query(rt<<1,l,tree[rt].mid()),Query(rt<<1|1,tree[rt].mid()+1,r)); return ans; } int main() { int i,j,n,m,step,k,l,r; char ch; while(scanf("%d%d",&n,&m)!=EOF){ CreatBtree(1,n,1); //printf("%d %d %d\n",tree[1].max,tree[1].left,tree[1].right); for(i=0;i<m;i++){ scanf("%s%d%d",&ch,&k,&step); if(ch=='Q')printf("%d\n",Query(1,k,step)); else change(1,k,step); } } return 0; }