标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 62840 Accepted Submission(s): 24384
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 struct Node{ 6 int l, r, val; 7 }; 8 Node node[200005<<2]; 9 10 void build(int v, int l, int r) 11 { 12 node[v].l = l; 13 node[v].r = r; 14 if(node[v].l == node[v].r){ 15 scanf("%d", &node[v].val); 16 return; 17 } 18 int mid = (l+r)>>1; 19 build(v<<1, l, mid); 20 build(v<<1|1, mid+1, r); 21 node[v].val = max(node[v<<1].val, node[v<<1|1].val); 22 } 23 24 int query(int v, int l, int r) 25 { 26 if(node[v].l == l && node[v].r == r) 27 return node[v].val; 28 int mid = (node[v].l+node[v].r)>>1; 29 if(r <= mid) 30 return query(v<<1, l, r); 31 else if(l > mid) 32 return query(v<<1|1, l, r); 33 else 34 return max(query(v<<1, l, mid), query(v<<1|1, mid+1, r)); 35 } 36 37 void update(int v, int l, int r, int m) 38 { 39 if(node[v].l == l && node[v].r == r){ 40 node[v].val = m; 41 return; 42 } 43 int mid = (node[v].l+node[v].r)>>1; 44 if(r <= mid) 45 update(v<<1, l, r, m); 46 else if(l > mid) 47 update(v<<1|1, l, r, m); 48 else{ 49 update(v<<1, l, mid, m); 50 update(v<<1|1, mid+1, r, m); 51 } 52 node[v].val = max(node[v<<1].val, node[v<<1|1].val); 53 } 54 55 int main() 56 { 57 int N, M; 58 while(~scanf("%d%d", &N, &M)){ 59 build(1, 1, N); 60 char order[5]; 61 int A, B; 62 while(M--){ 63 scanf("%s%d%d", order, &A, &B); 64 if(order[0] == ‘Q‘) 65 printf("%d\n", query(1, A, B)); 66 else 67 update(1, A, A, B); 68 } 69 } 70 return 0; 71 }
标签:
原文地址:http://www.cnblogs.com/inmoonlight/p/5674019.html