【Codeforces】280D k-Maximum Subsequence Sum【区间内k段最大和——线段树模拟费用流】

传送门:【Codeforces】280D k-Maximum Subsequence Sum

key word:取反

my  code:

#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define clr( a , x ) memset ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define mid ( ( l + r ) >> 1 )

const int MAXN = 100005 ;

struct Node {
    int s , mv , lv , rv , ml , mr , l , r ;
    Node () {}
    Node ( int x , int v ) {
        s = mv = lv = rv = v ;
        ml = mr = l = r = x ;
    }
    Node operator + ( const Node& a ) const {
        Node c ;
        c.s = s + a.s ;
        if ( mv > a.mv ) {
            c.mv = mv ;
            c.ml = ml ;
            c.mr = mr ;
        } else {
            c.mv = a.mv ;
            c.ml = a.ml ;
            c.mr = a.mr ;
        }
        if ( rv + a.lv > c.mv ) {
            c.mv = rv + a.lv ;
            c.ml = r ;
            c.mr = a.l ;
        }
        c.lv = lv ;
        c.rv = a.rv ;
        c.l = l ;
        c.r = a.r ;
        if ( s + a.lv > c.lv ) {
            c.lv = s + a.lv ;
            c.l = a.l ;
        }
        if ( rv + a.s > c.rv ) {
            c.rv = rv + a.s ;
            c.r = r ;
        }
        return c ;
    }
    Node operator - ( const Node& a ) const {
        Node c ;
        c.s = s + a.s ;
        if ( mv < a.mv ) {
            c.mv = mv ;
            c.ml = ml ;
            c.mr = mr ;
        } else {
            c.mv = a.mv ;
            c.ml = a.ml ;
            c.mr = a.mr ;
        }
        if ( rv + a.lv < c.mv ) {
            c.mv = rv + a.lv ;
            c.ml = r ;
            c.mr = a.l ;
        }
        c.lv = lv ;
        c.rv = a.rv ;
        c.l = l ;
        c.r = a.r ;
        if ( s + a.lv < c.lv ) {
            c.lv = s + a.lv ;
            c.l = a.l ;
        }
        if ( rv + a.s < c.rv ) {
            c.rv = rv + a.s ;
            c.r = r ;
        }
        return c ;
    }
    void rev () {
        mv = -mv ;
        lv = -lv ;
        rv = -rv ;
        s = -s ;
    }
} ;

struct Seg {
    Node mi , ma ;
    bool flip ;
    void down () {
        swap ( mi , ma ) ;
        mi.rev () ;
        ma.rev () ;
        flip ^= 1 ;
    }
} ;

Seg T[MAXN << 2] ;
Node ans ;
int S[30][2] ;
int n , m ;

void up ( int o ) {
    T[o].mi = T[ls].mi - T[rs].mi ;
    T[o].ma = T[ls].ma + T[rs].ma ;
}

void down ( int o ) {
    if ( T[o].flip ) {
        T[ls].down () ;
        T[rs].down () ;
        T[o].flip = 0 ;
    }
}

void modify ( int x , int v , int o , int l , int r ) {
    if ( l == r ) T[o].mi = Node ( x , v ) , T[o].ma = Node ( x , v ) ;
    else {
        down ( o ) ;
        int m = mid ;
        if ( x <= m ) modify ( x , v , lson ) ;
        else modify ( x , v , rson ) ;
        up ( o ) ;
    }
}

void update ( int L , int R , int o , int l , int r ) {
    if ( L <= l && r <= R ) T[o].down () ;
    else {
        down ( o ) ;
        int m = mid ;
        if ( L <= m ) update ( L , R , lson ) ;
        if ( m <  R ) update ( L , R , rson ) ;
        up ( o ) ;
    }
}

void query ( int L , int R , int o , int l , int r ) {
    if ( L <= l && r <= R ) ans = ans + T[o].ma ;
    else {
        down ( o ) ;
        int m = mid ;
        if ( L <= m ) query ( L , R , lson ) ;
        if ( m <  R ) query ( L , R , rson ) ;
    }
}

void build ( int o , int l , int r ) {
    T[o].flip = 0 ;
    if ( l == r ) {
        int v ;
        scanf ( "%d" , &v ) ;
        T[o].mi = Node ( l , v ) ;
        T[o].ma = Node ( l , v ) ;
        return ;
    }
    int m = mid ;
    build ( lson ) ;
    build ( rson ) ;
    up ( o ) ;
}

void solve () {
    int op , x , y , v , k ;
    build ( root ) ;
    scanf ( "%d" , &m ) ;
    for ( int i = 0 ; i < m ; ++ i ) {
        scanf ( "%d" , &op ) ;
        if ( op == 0 ) {
            scanf ( "%d%d" , &x , &v ) ;
            modify ( x , v , root ) ;
        } else {
            scanf ( "%d%d%d" , &x , &y , &k ) ;
            int res = 0 ;
            for ( int j = 0 ; j < k ; ++ j ) {
                ans = Node ( -1000 , -1000 ) ;
                query ( x , y , root ) ;
                S[j][0] = ans.ml ;
                S[j][1] = ans.mr ;
                if ( ans.mv < 0 ) {
                    k = j ;
                    break ;
                }
                res += ans.mv ;
                update ( S[j][0] , S[j][1] , root ) ;
            }
            printf ( "%d\n" , max ( res , 0 ) ) ;
            for ( int j = 0 ; j < k ; ++ j ) {
                update ( S[j][0] , S[j][1] , root ) ;
            }
        }
    }
}

int main () {
    while ( ~scanf ( "%d" , &n ) ) solve () ;
    return 0 ;
}

你可能感兴趣的:(【Codeforces】280D k-Maximum Subsequence Sum【区间内k段最大和——线段树模拟费用流】)