제출 #575555

#제출 시각아이디문제언어결과실행 시간메모리
575555chonkaTwo Antennas (JOI19_antennas)C++17
22 / 100
360 ms27944 KiB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int MAXN = 200007 ;
const int inf = 2000000007 ;

int n , m ;
int h[ MAXN ] , a[ MAXN ] , b[ MAXN ] ;

pair < int , int > qlist[ MAXN ] ;
int ans[ MAXN ] ;

vector < pair < int , int > > v[ MAXN ] ;
vector < int > to_solve[ MAXN ] ;

class Tree {
public :
    int tr[ 4 * MAXN ] , cand[ 4 * MAXN ] ;
    int lazy[ 4 * MAXN ] ;
    void push_lazy ( int where , int IL , int IR ) {
        cand[ where ] = max ( cand[ where ] , tr[ where ] + lazy[ where ] ) ;
        if ( IL != IR ) {
            lazy[ 2 * where ] = max ( lazy[ 2 * where ] , lazy[ where ] ) ;
            lazy[ 2 * where + 1 ] = max ( lazy[ 2 * where + 1 ] , lazy[ where ] ) ;
        }
        lazy[ where ] = 0 ;
    }
    void init ( int where , int IL , int IR ) {
        cand[ where ] = tr[ where ] = -inf ;
        lazy[ where ] = 0 ;
        if ( IL == IR ) { return ; }
        int mid = ( IL + IR ) / 2 ;
        init ( 2 * where , IL , mid ) ;
        init ( 2 * where + 1 , mid + 1 , IR ) ;
    }
    void upd_pos ( int where , int IL , int IR , int pos , int nw ) {
        push_lazy ( where , IL , IR ) ;
        if ( IR < pos || pos < IL ) { return ; }
        if ( IL == IR ) {
            tr[ where ] = nw ;
            return ;
        }
        int mid = ( IL + IR ) / 2 ;
        upd_pos ( 2 * where , IL , mid , pos , nw ) ;
        upd_pos ( 2 * where + 1 , mid + 1 , IR , pos , nw ) ;
        tr[ where ] = max ( tr[ 2 * where ] , tr[ 2 * where + 1 ] ) ;
        cand[ where ] = max ( cand[ 2 * where ] , cand[ 2 * where + 1 ] ) ;
    }
    void upd_range ( int where , int IL , int IR , int CURL , int CURR , int val ) {
        push_lazy ( where , IL , IR ) ;
        if ( IL > IR || CURL > CURR ) { return ; }
        if ( IR < CURL || CURR < IL ) { return ; }
        if ( CURL <= IL && IR <= CURR ) {
            lazy[ where ] = val ;
            push_lazy ( where , IL , IR ) ;
            return ;
        }
        int mid = ( IL + IR ) / 2 ;
        upd_range ( 2 * where , IL , mid , CURL , CURR , val ) ;
        upd_range ( 2 * where + 1 , mid + 1 , IR , CURL , CURR , val ) ;
        cand[ where ] = max ( cand[ 2 * where ] , cand[ 2 * where + 1 ] ) ;
    }
    int query ( int where , int IL , int IR , int CURL , int CURR ) {
        push_lazy ( where , IL , IR ) ;
        if ( IL > IR || CURL > IR ) { return -inf ; }
        if ( IR < CURL || CURR < IL ) { return -inf ; }
        if ( CURL <= IL && IR <= CURR ) { return cand[ where ] ; }
        int mid = ( IL + IR ) / 2 ;
        return max ( query ( 2 * where , IL , mid , CURL , CURR ) , query ( 2 * where + 1 , mid + 1 , IR , CURL , CURR ) ) ;
    }
};
Tree w ;

void input ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> h[ i ] >> a[ i ] >> b[ i ] ;
    }
    cin >> m ;
    for ( int i = 1 ; i <= m ; ++ i ) {
        cin >> qlist[ i ].first >> qlist[ i ].second ;
        ans[ i ] = -inf ;
    }
}

void solve ( ) {
    for ( int iter = 0 ; iter < 2 ; ++ iter ) {
        w.init ( 1 , 1 , n ) ;
        for ( int i = 1 ; i <= n ; ++ i ) {
            int st = i + a[ i ] , en = i + b[ i ] + 1 ;
            if ( st <= n ) {
                v[ st ].push_back ( { i , 1 } ) ;
            }
            if ( en <= n ) {
                v[ en ].push_back ( { i , -1 } ) ;
            }
        }
        for ( int i = 1 ; i <= m ; ++ i ) {
            to_solve[ qlist[ i ].second ].push_back ( i ) ;
        }
        for ( int i = 1 ; i <= n ; ++ i ) {
            for ( auto e : v[ i ] ) {
                int id = e.first , type = e.second ;
                if ( type == 1 ) {
                    w.upd_pos ( 1 , 1 , n , id , -h[ id ] ) ;
                }
                else {
                    w.upd_pos ( 1 , 1 , n , id , -inf ) ;
                }
            }
            int st = i - b[ i ] ;
            int en = i - a[ i ] ;
            if ( st < 1 ) { st = 1 ; }
            if ( en < st ) { en = st ; }
            w.upd_range ( 1 , 1 , n , st , en , h[ i ] ) ;
            for ( auto id : to_solve[ i ] ) {
                ans[ id ] = max ( ans[ id ] , w.query ( 1 , 1 , n , qlist[ id ].first , i ) ) ;
            }
        }
        for ( int i = 1 ; i < n - i + 1 ; ++ i ) {
            swap ( h[ i ] , h[ n - i + 1 ] ) ;
            swap ( a[ i ] , a[ n - i + 1 ] ) ;
            swap ( b[ i ] , b[ n - i + 1 ] ) ;
        }
        for ( int i = 1 ; i <= m ; ++ i ) {
            int x = qlist[ i ].first , y = qlist[ i ].second ;
            qlist[ i ] = { n - y + 1 , n - x + 1 } ;
        }
        for ( int i = 1 ; i <= n ; ++ i ) {
            v[ i ].clear ( ) ;
            to_solve[ i ].clear ( ) ;
        }
    }
    for ( int i = 1 ; i <= m ; ++ i ) {
        if ( ans[ i ] == -inf ) { ans[ i ] = -1 ; }
        cout << ans[ i ] << "\n" ;
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ;
    /// cin >> t ;
    while ( t -- ) {
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...