Calculate MAX node
MAX [ v ] = max ( MAX [ v * 2 ] , max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == tm - tl + 1 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + suf [ v * 2 ] * ( suf [ 2 * v + 1 ] == tr - tm ) ) ; } }