HDU 1698 區間更新線段樹

來源:互聯網
上載者:User

一點點想當然的想法使得我WA了6次...

對線段樹的理解又深了一點。

熟悉了加標記的操作。

#include<iostream>#include<cstdio>#define MAXN 111111using namespace std;int tree[MAXN<<2];int flag[MAXN<<2];void PushUp( int rt ){  tree[rt]=tree[rt<<1]+tree[rt<<1|1];}void PushDown( int l,int r,int rt ){  if( flag[rt] )  {   int m=(r+l)>>1;    flag[rt<<1]=flag[rt<<1|1]=flag[rt];   //tree[rt<<1]=tree[rt]*(m-l+1)/(r-l+1);   tree[rt<<1]=flag[rt]*(m-l+1);   //tree[rt<<1]=flag[rt]*(m-(m>>1));   //tree[rt<<1|1]=tree[rt]*(r-m)/(r-l+1); tree[rt<<1|1]=flag[rt]*(r-m);   //tree[rt<<1|1]=flag[rt]*(m>>1);    flag[rt]=0; }}void build( int l,int r,int rt ){ flag[rt]=0; tree[rt]=1;  if( l==r )   return ; int m=(l+r)>>1; build( l,m,rt<<1 ); build( m+1,r,rt<<1|1 ); PushUp(rt);}void update( int L,int R,int v,int l,int r,int rt ){  if( L<=l&&r<=R )  {   tree[rt]=(r-l+1)*v;   flag[rt]=v;   return ; } PushDown(l,r,rt); int m=(l+r)>>1; if( L<=m )  update( L,R,v,l,m,rt<<1 ); if( m+1<=R )  update( L,R,v,m+1,r,rt<<1|1 ); /**else {  update( L,R,v,l,m,rt<<1 );  update( L,R,v,m+1,r,rt<<1|1 ); }*/ PushUp(rt);}int main(){ int T,cases=1; scanf( "%d",&T ); while( T-- ) {    memset( flag,0,sizeof(flag) );    int N;    scanf( "%d",&N );    build( 1,N,1 );    int Q;    scanf( "%d",&Q );    int x,y,z;    while( Q-- )    {     scanf( "%d %d %d",&x,&y,&z );     update( x,y,z,1,N,1 );     //printf( "%d\n",tree[1] );     }     printf( "Case %d: The total value of the hook is %d.\n",cases++,tree[1] );  }  return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.