1 #include<cstdio> 2 #include<string.h> 3 using namespace std; 4 int ans; 5 int max1=0; 6 int a[200010]; 7 struct Node{ 8 int left; 9 int right; 10 int max; 11 }node[800010]; 12 void TreeMake(int l,int r,int i) 13 { 14 node[i].left=l; 15 node[i].right=r; 16 node[i].max=0; 17 if(l==r) 18 { 19 node[i].max=a[l];//是a[l]!!!!! 20 return; 21 } 22 else 23 { 24 int mid=(l+r)/2; 25 TreeMake(l,mid,2*i); 26 TreeMake(mid+1,r,2*i+1); 27 node[i].max=node[2*i].max>node[2*i+1].max?node[2*i].max:node[2*i+1].max;//注意更新最大值,回溯 28 } 29 } 30 void Updata_tree(int l,int i) 31 { 32 if(node[i].left==l&&node[i].right==l) 33 { 34 node[i].max=ans; 35 return; 36 } 37 38 else{ 39 int mid=(node[i].left+node[i].right)/2; 40 if(l<=mid) Updata_tree(l,2*i); 41 if(l>mid) Updata_tree(l,2*i+1); 42 node[i].max=node[2*i].max>node[2*i+1].max?node[2*i].max:node[2*i+1].max;//注意回溯!! 43 } 44 45 } 46 47 void Query_tree(int l,int r,int i) 48 { 49 //if(node[i].left==l&&node[i].right==r) 50 // return node[i].max; 51 // 52 if(node[i].left>=l&&node[i].right<=r) 53 { 54 if(max1<node[i].max) 55 max1=node[i].max; 56 return; 57 } 58 int mid=(node[i].left+node[i].right)/2; 59 if(r<=mid) Query_tree(l,r,2*i); 60 else if(l>mid) Query_tree(l,r,2*i+1); 61 else 62 { 63 Query_tree(l,r,2*i); 64 Query_tree(l,r,2*i+1); 65 } 66 } 67 68 69 70 int main() 71 { 72 int n,m; 73 char ch[10]; 74 while(scanf("%d %d",&n,&m)!=EOF) 75 { 76 for(int i=1;i<=n;i++) 77 scanf("%d",&a[i]); 78 TreeMake(1,n,1); 79 while(m--) 80 { 81 scanf("%s",ch); 82 int x,y; 83 scanf("%d %d",&x,&y); 84 max1=0; 85 if(ch[0]==‘Q‘) 86 { 87 Query_tree(x,y,1);//查询(x,y)区间 88 printf("%d\n",max1); 89 } 90 if(ch[0]==‘U‘) 91 { 92 ans=y; 93 Updata_tree(x,1); 94 } 95 } 96 } 97 return 0; 98 }
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
Hint
Huge input,the C function scanf() will work better than cin
原文:http://www.cnblogs.com/angledamon/p/3901642.html