标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45514 Accepted Submission(s): 17865
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <string> 5 #include <iomanip> 6 #include <stdio.h> 7 #include <algorithm> 8 using namespace std; 9 #define maxn 200020 10 #define lson l,m,rt<<1 11 #define rson m+1,r,rt<<1|1 12 int cnt[maxn<<2]; 13 int N, M; 14 int max(int a, int b){ 15 return a>b?a:b; 16 } 17 void PushUP(int rt){ 18 cnt[rt] = max( cnt[rt<<1],cnt[rt<<1|1]); 19 } 20 void build(int l, int r, int rt){ 21 if(l == r){ 22 scanf("%d", &cnt[rt]); 23 return; 24 } 25 int m = (l+r)>>1; 26 build(lson); 27 build(rson); 28 PushUP(rt); 29 } 30 void update(int p, int add, int l, int r, int rt){ 31 if(l == r){ 32 cnt[rt] = add; 33 return; 34 } 35 int m = (l+r)>>1; 36 if(p <= m) update(p, add, lson); 37 else update(p, add, rson); 38 PushUP(rt); 39 } 40 int query(int L, int R, int l, int r, int rt){ 41 if(L <= l && r <= R){ 42 return cnt[rt]; 43 } 44 int m = (l+r)>>1; 45 int ret = 0; 46 if(L <= m) ret = max(ret,query(L, R, lson)); 47 if(R > m) ret = max(ret, query(L, R, rson)); 48 return ret; 49 } 50 int main(){ 51 while(~scanf("%d%d", &N, &M)){ 52 build(1, N, 1); 53 while(M--){ 54 char c[2]; 55 scanf("%s", &c); 56 int a, b; 57 scanf("%d%d", &a, &b); 58 if(c[0] == ‘Q‘) printf("%d\n", query(a, b, 1, N, 1)); 59 else if(c[0] == ‘U‘) update(a, b, 1, N, 1); 60 } 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/titicia/p/4453741.html