POJ 2823 castrated line segment tree

Source: Internet
Author: User

The better method for this question is monotonous queue. Implement it later. Use castrated line tree to write 9600 ms + + records ~

The simple line segment tree will not be mentioned. For details, you can read previous articles or the following code .....

Although it's a bit rough to count ....


#include<iostream>#define MAXN 1000001using namespace std;int _max[MAXN<<2];int _min[MAXN<<2];int mx( int a,int b ){    return a>b?a:b;}int mn( int a,int b ){    return a<b?a:b;}void PushUp( int rt ){     _max[rt]=mx( _max[rt<<1],_max[rt<<1|1] );     _min[rt]=mn( _min[rt<<1],_min[rt<<1|1] );}void build(int l,int r,int rt){     if( l==r ){         scanf( "%d",&_max[rt] );         _min[rt]=_max[rt];         return ;     }     int m=(l+r)>>1;     build( l,m,rt<<1 );     build( m+1,r,rt<<1|1 );     PushUp(rt);}int query( int l,int r,int c,int L,int R,int rt ){    if( l<=L&&R<=r )    {        if( c )        return _max[rt];        else        return _min[rt];    }    int m=(L+R)>>1;    int res;    if( c )    {         res=-214748363;        if( l<=m ) res=mx(res,query( l,r,c,L,m,rt<<1) );        if( m<r ) res=mx( res,query( l,r,c,m+1,R,rt<<1|1) );        return res;    }    else    {        res=214748363;        //res=100;        if( l<=m ) res=mn(res,query( l,r,c,L,m,rt<<1) );        if( m<r ) res=mn( res,query( l,r,c,m+1,R,rt<<1|1) );        return res;    }}int main(){    int n,c,i;    while( scanf( "%d %d",&n,&c)!=EOF )    {           build(1,n,1);           printf( "%d",query( 1,c,0,1,n,1 ) );           for( i=2;;i++ )           {                if( i+c-1<=n ){                    printf( " %d",query( i,i+c-1,0,1,n,1 ) );                }                else                    break;           }           printf( "\n%d",query( 1,c,1,1,n,1 ) );           for( i=2;;i++ )           {                if( i+c-1<=n ){                    printf( " %d",query( i,i+c-1,1,1,n,1 ) );                }                else                    break;           }           printf( "\n" );    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.