제출 #607127

#제출 시각아이디문제언어결과실행 시간메모리
607127chonkaElection (BOI18_election)C++98
100 / 100
689 ms42916 KiB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int MAXN = 500007 ;

int n ;
string a ;

struct node {
    int sm , pref , suff ;
    int ans = 0 ;
    node ( ) { sm = pref = suff = 0 ; }
    node ( int _sm , int _pref , int _suff ) {
        sm = _sm , pref = _pref , suff =_suff ;
        ans = min ( pref , suff ) ;
    }
};
node tr[ 4 * MAXN ] ;



node unite ( node l , node r ) {
    node ret = node ( ) ;
    ret.sm = l.sm + r.sm ;

    ret.ans = l.pref + r.suff ;
    ret.ans = min ( ret.ans , l.ans + r.sm ) ;
    ret.ans = min ( ret.ans , r.ans + l.sm ) ;
    
    ret.pref = min ( l.pref , l.sm + r.pref ) ;
    ret.suff = min ( r.suff , r.sm + l.suff ) ;
    return ret ;
}

void init ( int where , int IL , int IR ) {
    if ( IL == IR ) {
        if ( a[ IL - 1 ] == 'C' ) { tr[ where ] = node ( 1 , 0 , 0 ) ; }
        else { tr[ where ] = node ( -1 , -1 , -1 ) ; }
        return ;
    }
    int mid = ( IL + IR ) / 2 ;
    init ( 2 * where , IL , mid ) ;
    init ( 2 * where + 1 , mid + 1 , IR ) ;
    tr[ where ] = unite ( tr[ 2 * where ] , tr[ 2 * where + 1 ] ) ;
}

node query ( int where , int IL , int IR , int CURL , int CURR ) {
    if ( IL > IR || CURL > CURR ) { return node ( ) ; }
    if ( IR < CURL || CURR < IL ) { return node ( ) ; }
    if ( CURL <= IL && IR <= CURR ) { return tr[ where ] ; }
    int mid = ( IL + IR ) / 2 ;
    return unite ( query ( 2 * where , IL , mid , CURL , CURR ) , query ( 2 * where + 1 , mid + 1 , IR , CURL , CURR ) ) ;
}

void solve ( ) {
    cin >> n >> a ;
    init ( 1 , 1 , n ) ;
    int q ; cin >> q ;
    while ( q -- ) {
        int l , r ; cin >> l >> r ;
        node aux = query ( 1 , 1 , n , l , r ) ;
        cout << -aux.ans << "\n" ;
    }
}

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