标签:str 题解 const 个学生 script tchar cst code 程序
此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 6 using namespace std; 7 const int MAXN = 500005; 8 9 inline void read(int &x) 10 { 11 char ch = getchar(),c = ch;x = 0; 12 while(ch < ‘0‘ || ch > ‘9‘) c = ch,ch = getchar(); 13 while(ch >= ‘0‘ && ch <= ‘9‘) x = (x<<1)+(x<<3)+ch-‘0‘,ch = getchar(); 14 if(c == ‘-‘) x = -x; 15 } 16 17 int sdata[MAXN<<1],num[MAXN],n,m,a,b; 18 char op[5]; 19 20 inline int max(int a,int b) 21 { 22 return a>b?a:b; 23 } 24 25 inline void update(int o) 26 { 27 sdata[o] = max(sdata[o<<1],sdata[o<<1|1]); 28 } 29 30 void build(int l,int r,int o) 31 { 32 if(l == r) 33 { 34 read(sdata[o]); 35 return; 36 } 37 int mid = (l+r)>>1; 38 build(l,mid,o<<1); 39 build(mid+1,r,o<<1|1); 40 update(o); 41 } 42 43 void modify(int l,int r,int o,int p,int k) 44 { 45 if(l == r && r == p) 46 { 47 sdata[o] = k; 48 return; 49 } 50 int mid = (l+r)>>1; 51 if(p <= mid) modify(l,mid,o<<1,p,k); 52 else modify(mid+1,r,o<<1|1,p,k); 53 update(o); 54 } 55 56 int ask(int l,int r,int o,int ll,int rr) 57 { 58 if(ll <= l && rr >= r) 59 { 60 return sdata[o]; 61 } 62 int mid = (l+r)>>1; 63 int ans = 0; 64 if(ll <= mid) ans = max(ans,ask(l,mid,o<<1,ll,rr)); 65 if(rr > mid) ans = max(ans,ask(mid+1,r,o<<1|1,ll,rr)); 66 return ans; 67 } 68 69 int main() 70 { 71 while(scanf("%d%d",&n,&m) != EOF) 72 { 73 memset(sdata,0,sizeof(sdata)); 74 build(1,n,1); 75 for(int i = 1;i <= m;++ i) 76 { 77 scanf("%s",op); 78 read(a),read(b); 79 if(op[0] == ‘Q‘) 80 { 81 printf("%d\n",ask(1,n,1,a,b)); 82 } 83 else 84 { 85 modify(1,n,1,a,b); 86 } 87 } 88 } 89 return 0; 90 }
标签:str 题解 const 个学生 script tchar cst code 程序
原文地址:http://www.cnblogs.com/shingen/p/7509131.html