标签:
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 int tree[222222<<2]; 7 void push_up(int rt) 8 { 9 tree[rt]=max(tree[rt<<1],tree[rt<<1 | 1]); 10 } 11 void creat(int rt,int start,int end) 12 { 13 if(start==end) 14 { 15 scanf("%d",&tree[rt]); 16 return ; 17 } 18 else 19 { 20 //递归构造左右子树 21 creat(rt<<1,start,(start+end)>>1); 22 creat(rt<<1 | 1,((start+end)>>1)+1,end); 23 //回溯得到当前节点的信息 24 push_up(rt); 25 } 26 } 27 int recreach(int rt,int start,int end,int left,int right) 28 { 29 if(left>end||right<start) //查询区间和要求区间没交集 30 return -1; 31 if(left<=start&&right>=end) 32 return tree[rt]; 33 int mid=(start+end)>>1; 34 int ans=0; 35 if(left<=mid) 36 ans=max(ans,recreach(rt<<1,start,mid,left,right)); 37 if(right>mid) 38 ans=max(ans,recreach(rt<<1 | 1,mid+1,end,left,right)); 39 return ans; 40 } 41 void update(int rt,int start,int end,int a,int b) 42 { 43 if(start==end) 44 { 45 tree[rt]=b; 46 return ; 47 } 48 int mid=(start+end)>>1; 49 if(a<=mid) 50 update(rt<<1,start,mid,a,b); 51 else 52 update(rt<<1 | 1,mid+1,end,a,b); 53 push_up(rt); 54 } 55 int main() 56 { 57 int n,m; 58 char c[2]; 59 int a,b; 60 while(~scanf("%d%d",&n,&m)) 61 { 62 memset(tree,0,sizeof(tree)); 63 creat(1,1,n); 64 while(m--) 65 { 66 scanf("%s%d%d",c,&a,&b); 67 if(c[0]==‘Q‘) 68 printf("%d\n",recreach(1,1,n,a,b)); 69 if(c[0]==‘U‘) 70 update(1,1,n,a,b); 71 } 72 } 73 return 0; 74 }
标签:
原文地址:http://www.cnblogs.com/cxbky/p/4872804.html