标签:
B - I Hate It
Description
Input
Output
Sample Input
Sample Output
Hint
Huge input,the C function scanf() will work better than cin
一开始没看到多组数据,WA了两发。
代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<algorithm> 4 # include<string> 5 6 using namespace std; 7 8 # define MAX 200004 9 # define lid (id<<1) 10 # define rid (id<<1|1) 11 12 13 int a[MAX]; 14 struct Segtree 15 { 16 int l,r; 17 int mx; 18 }tree[MAX*4]; 19 20 21 void push_up( int id ) 22 { 23 tree[id].mx = max(tree[lid].mx,tree[rid].mx); 24 } 25 26 27 void build ( int id,int l,int r ) 28 { 29 tree[id].l = l; tree[id].r = r; 30 if ( l==r ) 31 { 32 tree[id].mx = a[l]; 33 return; 34 } 35 int mid = ( tree[id].l+tree[id].r )>>1; 36 build(lid,l,mid); 37 build(rid,mid+1,r); 38 push_up(id); 39 } 40 41 void update( int id,int x,int val ) 42 { 43 if ( tree[id].l==x&&tree[id].r==x ) 44 { 45 tree[id].mx = val; 46 return; 47 } 48 int mid = ( tree[id].l+tree[id].r )>>1; 49 if ( x <= mid ) 50 update(lid,x,val); 51 else 52 update(rid,x,val); 53 push_up(id); 54 } 55 56 int query( int id,int l,int r ) 57 { 58 if ( tree[id].l==l&&tree[id].r==r ) 59 return tree[id].mx; 60 int mid = ( tree[id].l+tree[id].r )>>1; 61 if ( r <= mid ) 62 return query(lid,l,r); 63 if ( l > mid ) 64 return query(rid,l,r); 65 return max(query(lid,l,mid),query(rid,mid+1,r)); 66 } 67 68 69 70 int main(void) 71 { 72 int n,m; 73 while ( scanf("%d%d",&n,&m)!=EOF ) 74 { 75 for ( int i = 1; i<= n;i++ ) 76 scanf("%d",&a[i]); 77 build(1,1,n); 78 string str; 79 while ( m-- ) 80 { 81 cin>>str; 82 if ( str[0]==‘Q‘ ) 83 { 84 int t1,t2;scanf("%d%d",&t1,&t2); 85 int ans = query(1,t1,t2); 86 printf("%d\n",ans); 87 } 88 else if ( str[0]==‘U‘ ) 89 { 90 int t1,t2;scanf("%d%d",&t1,&t2); 91 update(1,t1,t2); 92 } 93 else 94 break; 95 } 96 97 } 98 return 0; 99 }
标签:
原文地址:http://www.cnblogs.com/wikioibai/p/4529503.html