首页 > 代码库 > *HDU1754 线段树

*HDU1754 线段树

10868584 2014-06-11 18:26:52 Accepted 1754 1078MS 3156K 1430 B G++ little_w

【题解】:

【代码】:

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #define Mod 1000000007
 5 #define LL long long
 6 #define toL 2*o,l,m
 7 #define toR 2*o+1,m+1,r
 8 using namespace std;
 9 
10 int maxv[200005<<2];
11 int A[200005];
12 
13 void builtTree(int o,int l,int r){
14     if (l==r){
15         maxv[o]=A[l];
16         return ;
17     }
18     int m=(l+r)/2;
19     builtTree(toL);
20     builtTree(toR);
21     maxv[o]=max(maxv[2*o],maxv[2*o+1]);
22     return ;
23 }
24 void Updata(int o,int l,int r,int p,int x){
25     if (l==r && p==l){
26         maxv[o]=x;
27         return ;
28     }else {
29         int m=(r+l)/2;
30         if (p<=m) Updata(toL,p,x);
31         if (m+1<=p) Updata(toR,p,x);
32         maxv[o]=max(maxv[2*o],maxv[2*o+1]);
33         return ;
34     }
35 }
36 int Query(int o,int l,int r,int ql,int qr){
37     if (ql<=l && r<=qr) return maxv[o];
38     int m=(r+l)/2;
39     int ans=-1;
40     if (ql<=m) ans=max(ans,Query(toL,ql,qr));
41     if (m+1<=qr) ans=max(ans,Query(toR,ql,qr));
42     return ans;
43 }
44 int n,q;
45 
46 int main(){
47     while(~scanf("%d%d",&n,&q)){
48         for(int i=1;i<=n;i++) scanf("%d",&A[i]);
49         builtTree(1,1,n);
50         for(int i=1;i<=q;i++){
51             int a,b;
52             char s[3];
53             scanf("%s%d%d",s,&a,&b);
54             if (s[0]==Q){
55                 printf("%d\n",Query(1,1,n,a,b));
56             }
57             if (s[0]==U){
58                 Updata(1,1,n,a,b);
59             }
60         }
61     }
62     return 0;
63 }
View Code