HDU 1754 点更新段查询最大值线段树

来源:互联网 发布:win7内存优化 编辑:程序博客网 时间:2024/05/17 05:09

由于自己做代码优化时,条件不清晰,两个变量写错导致WA了两次,不应该!

#include<iostream>#include<cstdio>#define MAXN 222222using namespace std;int tree[MAXN<<2];int N,M;void pushUp( int rt ){  tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);}void build( int l,int r,int rt ){  if( l==r )  {   scanf( "%d",&tree[rt] );   return ; } int m=(l+r)/2; build( l,m,rt<<1 ); build( m+1,r,rt<<1|1 ); pushUp(rt);}int query( int L,int R,int l,int r,int rt ){ if( L<=l&&r<=R ) return tree[rt];int m=(l+r)/2,ret=0;if( m>=R )return query( L,R,l,m,rt<<1 );if( m+1<=L )return query( L,R,m+1,r,rt<<1|1 );ret=max(ret,query(L,R,l,m,rt<<1));ret=max(ret,query(L,R,m+1,r,rt<<1|1));return ret;}void update( int pt,int v,int l,int r,int rt ){  if( l==r )  {   tree[rt]=v;   return ; } int m=(l+r)/2; if( pt<=m )  update( pt,v,l,m,rt<<1 ); else  update( pt,v,m+1,r,rt<<1|1 ); pushUp(rt);}int main(){ while( scanf("%d %d",&N,&M)!=EOF ) {    build( 1,N,1 );    char str[111];   gets(str);    while( M-- )    {     gets(str);     int n1,n2;     if( sscanf(str,"Q %d %d",&n1,&n2) )       printf( "%d\n",query(n1,n2,1,N,1) );     else if( sscanf(str,"U %d %d",&n1,&n2) )       update( n1,n2,1,N,1 );     }  } return 0;}


原创粉丝点击