This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include "prize.h"
#define pb push_back
#define s second
#define f first
#define pf push_front
#define inf 100000000000000000
#define bitebi __builtin_popcountll
#define FOR( i , n ) for( int i = 0 ; i < n ; i ++ )
#define YES cout <<"YES\n"
#define NO cout << "NO\n"
#define fbo find_by_order // returns iterator
#define ook order_of_key // returns strictly less numbers than key
using namespace std ;
const int N = 2e5 + 10 ;
int fen[ N ] ;
void add( int i ){
i ++ ;
for( int j = i ; j <= N ; j = j + ( j & ( - j ) ) ) fen[ j ] += 1 ;
}
int get_sum( int l , int r ){
r ++ ;
int sum = 0 ;
for( int j = r ; j > 0 ; j = j - ( j & ( - j ) ) ) sum += fen[ j ] ;
for( int j = l ; j > 0 ; j = j - ( j & ( - j ) ) ) sum -= fen[ j ] ;
return sum ;
}
int find_best( int n ) {
int mx = 0 ;
for( int i = 0 ; i < min( n , 500 ) ; i ++ ){
vector < int > v ;
v = ask( i ) ;
mx = max( mx , v[ 0 ] + v[ 1 ] ) ;
}
vector < pair < int , int > > v ;
v.pb( { 0 , n - 1 } ) ;
while( true ){
if( v.size() == 0 ) break ;
int sz = v.size() - 1 ;
int l = v[ sz ].f ;
int r = v[ sz ].s ;
v.pop_back() ;
int indx = ( l + r ) / 2 ;
int tt = 0 ;
while( true ){
vector < int > ve = ask( indx ) ;
if( ve[ 0 ] + ve[ 1 ] != mx ){
if( ve[ 0 ] + ve[ 1 ] == 0 ){
return indx ;
}
add( indx ) ;
if( tt == 0 && indx != l ) indx -- ;
else{
if( tt == 1 ) indx ++ ;
else{
indx = ( l + r ) / 2 + 1 ;
tt = 1 ;
}
if( indx > r ) break ;
}
}
else{
if( tt == 0 ){
// left part
int L = l ;
int R = indx - 1 ;
int lft = ve[ 0 ] ;
lft = lft - get_sum( -1 , l - 1 ) ;
if( lft != 0 && L <= R ){
v.pb( { L , R } ) ;
}
// right part
while( true ){
if( indx > r ) break ;
ve = ask( indx ) ;
if( ve[ 0 ] + ve[ 1 ] == mx ) break ;
add( indx ) ;
indx ++ ;
}
L = indx + 1 ;
R = r ;
lft = ve[ 1 ] ;
lft = lft - get_sum( r + 1 , N - 1 ) ;
if( lft != 0 && L <= R ){
v.pb( { L , R } ) ;
}
}
else{
// right part
int L = indx + 1 ;
int R = r ;
int lft = ve[ 1 ] ;
lft = lft - get_sum( r + 1 , n - 1 ) ;
if( lft != 0 && L <= R ){
v.pb( { L , R } ) ;
}
}
break ;
}
}
}
}
Compilation message (stderr)
prize.cpp: In function 'int find_best(int)':
prize.cpp:35:35: warning: control reaches end of non-void function [-Wreturn-type]
35 | vector < pair < int , int > > v ;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |