萌新刚学线段树,求助/dk/dk
查看原帖
萌新刚学线段树,求助/dk/dk
54047
兮水XiShui丶楼主2021/3/8 16:47

RT找了好久都不知道自己哪错了.求大佬查错/kel

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

template < class T > 
inline void read ( T &x ) {
    int s = 0 , w = 1;
    char ch = getchar ();
    while ( ch > '9' || ch < '0' ) { if ( ch == '-' ) w = -1; ch = getchar ();}
    while ( ch >= '0' && ch <= '9' ) { s = s * 10 + ch - '0'; ch = getchar ();}
    x = s * w;
    return;
}
template < typename T , typename ...Argc > 
inline void read ( T &x , Argc &...argc ) {
    read ( x );
    read ( argc... );
    return;
} 

const int INF = 998244353;
const int N = 5e5 + 10;

int n , m;              
int val[N];                 
struct Tree {
    int size;
    int lmax , rmax;
    int val , sum;
}tree[N << 2];

namespace Support {
    inline void swap ( int x , int y ) {
        int t = x;
        x = y;
        y = t;
    }
    inline int max ( int x , int y ) {
        return x > y ? x : y;
    }
    inline int min ( int x , int y ) {
        return x < y ? x : y;
    }
}

using namespace Support;

#define LC ( root << 1 )
#define RC ( root << 1 | 1 )  

void Build ( int root , int start , int end ) {
    tree[root].size = end - start + 1;
    if ( start == end ) {
        tree[root].lmax = val[start];
        tree[root].rmax = val[start];
        tree[root].val = val[start];
        tree[root].sum = val[start];
        return;
    }
    int mid = ( start + end ) >> 1;
    Build ( LC , start , mid );
    Build ( RC , mid + 1 , end );
    tree[root].sum = tree[LC].sum + tree[RC].sum;
    tree[root].lmax = max ( tree[LC].lmax , tree[RC].lmax + tree[LC].sum );
    tree[root].rmax = max ( tree[RC].rmax , tree[RC].sum + tree[LC].rmax );
    tree[root].val = max ( max ( tree[RC].val , tree[LC].val ) , tree[LC].rmax + tree[RC].lmax ); 
    return;
}   
Tree null;          
Tree check ( int root , int nstart , int nend , int qstart , int qend ) {
    if ( qstart > qend ) 
    	return null;
	if ( qstart <= nstart && qend >= nend ) return tree[root]; 
    int mid = ( nstart + nend ) >> 1;
    if ( qend <= mid ) 
        return check ( LC , nstart , mid , qstart , qend );
    else if ( qstart > mid )
        return check ( RC , mid + 1 , nend , qstart , qend );
    else {     
        Tree ne;
        Tree x = check ( LC , nstart , mid , qstart , qend )  , y = check ( RC , mid + 1 , nend , qstart , qend );
        ne.sum = x.sum + y.sum;
        ne.lmax = max ( x.lmax , y.lmax + x.sum );
        ne.rmax = max ( y.rmax , x.rmax + y.sum );
        ne.val = max ( max ( x.val , y.val ) , x.rmax + y.lmax );
        return ne;
    }
}

int main ( void ) {
    int T;
	read ( T );
    while ( T-- ) {
    	read ( n );
    	memset ( tree , 0 , sizeof ( tree ) );
		for ( int i = 1 ; i <= n ; i++ ) 
    	    read ( val[i] );
    	Build ( 1 , 1 , n );
    	read ( m );
    	for ( int i = 1 ; i <= m ; i++ ) {
			int ll , lr , rl , rr;
			read ( ll , lr , rl , rr );
			if ( lr < rl ) 
				printf ( "%d\n" , check ( 1 , 1 , n , lr + 1 , rl - 1 ).sum + check ( 1 , 1 , n , ll , lr ).rmax + check ( 1 , 1 , n , rl , rr ).lmax );
			else {
				int maxs = -1;
				maxs = max ( maxs , check ( 1 , 1 , n , rl , lr ).val );
				maxs = max ( maxs , check ( 1 , 1 , n , ll , rl - 1 ).rmax + check ( 1 , 1 , n , rl , rr ).lmax );
				maxs = max ( maxs , check ( 1 , 1 , n , lr + 1 , rr ).lmax + check ( 1 , 1 , n , ll , lr ).rmax ); 
				printf ( "%d\n" , maxs );
			}
    	}
    }
    return 0;
}
2021/3/8 16:47
加载中...