QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77316 | #2559. Endless Road | Sorting | WA | 2ms | 46436kb | C++ | 8.0kb | 2023-02-14 04:07:28 | 2023-02-14 04:07:32 |
Judging History
answer
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector<int> vi;
typedef unsigned int uint ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
const int MAXN = 5e5 + 7 ;
const ll inf = 1e18 + 7 ;
int n ;
struct range {
int l , r ;
int id , rem ;
};
range a[ MAXN ] ;
map < int , int > nwvals ;
int rev_vals[ MAXN ] ;
int mxval ;
int lens[ MAXN ] ;
class sum_tree {
public :
ll tr[ 4 * MAXN ] ;
void init ( int w , int l , int r ) {
if ( l == r ) {
tr[ w ] = lens[ l ] ;
return ;
}
int mid = ( l + r ) / 2 ;
init ( 2 * w , l , mid ) ;
init ( 2 * w + 1 , mid + 1 , r ) ;
tr[ w ] = tr[ 2 * w ] + tr[ 2 * w + 1 ] ;
}
void update ( int w , int l , int r , int pos , ll add ) {
if ( l == r ) {
tr[ w ] += add ;
return ;
}
int mid = ( l + r ) / 2 ;
if ( pos <= mid ) { update ( 2 * w , l , mid , pos , add ) ; }
else { update ( 2 * w + 1 , mid + 1 , r , pos , add ) ; }
tr[ w ] = tr[ 2 * w ] + tr[ 2 * w + 1 ] ;
}
ll query ( int w , int l , int r , int st , int en ) {
if ( l > r || st > en ) { return 0 ; }
if ( r < st || en < l ) { return 0 ; }
if ( st <= l && r <= en ) { return tr[ w ] ; }
int mid = ( l + r ) / 2 ;
return query ( 2 * w , l , mid , st , en ) + query ( 2 * w + 1 , mid + 1 , r , st , en ) ;
}
};
sum_tree w_sum ;
class min_tree {
public :
pii tr[ 4 * MAXN ] ;
pii unite ( pii p1 , pii p2 ) {
if ( p2.fi <= p1.fi ) { return p2 ; }
return p1 ;
}
void init ( int w , int l , int r ) {
if ( l == r ) {
tr[ w ] = { a[ l ].r , l } ;
return ;
}
int mid = ( l + r ) / 2 ;
init ( 2 * w , l , mid ) ;
init ( 2 * w + 1 , mid + 1 , r ) ;
tr[ w ] = unite ( tr[ 2 * w ] , tr[ 2 * w + 1 ] ) ;
}
void update ( int w , int l , int r , int pos ) {
if ( l == r ) {
tr[ w ] = { MAXN , l } ;
return ;
}
int mid = ( l + r ) / 2 ;
if ( pos <= mid ) { update ( 2 * w , l , mid , pos ) ; }
else { update ( 2 * w + 1 , mid + 1 , r , pos ) ; }
tr[ w ] = unite ( tr[ 2 * w ] , tr[ 2 * w + 1 ] ) ;
}
pii query ( int w , int l , int r , int st , int en ) {
if ( l > r || st > en ) { return { MAXN , MAXN } ; }
if ( r < st || en < l ) { return { MAXN , MAXN } ; }
if ( st <= l && r <= en ) { return tr[ w ] ; }
int mid = ( l + r ) / 2 ;
return unite ( query ( 2 * w , l , mid , st , en ) , query ( 2 * w + 1 , mid + 1 , r , st , en ) ) ;
}
};
min_tree current_min ;
class rem_sum_tree {
public :
pair < ll , int > tr[ 4 * MAXN ] ;
ll lazy[ 4 * MAXN ] ;
void push_lazy ( int w , int l , int r ) {
tr[ w ].fi += lazy[ w ] ;
if ( l != r ) {
lazy[ 2 * w ] += lazy[ w ] ;
lazy[ 2 * w + 1 ] += lazy[ w ] ;
}
lazy[ w ] = 0 ;
}
pair < ll , int > unite ( pair < ll , int > p1 , pair < ll , int > p2 ) {
if ( p1.fi < p2.fi ) { return p1 ; }
else if ( p2.fi < p1.fi ) { return p2 ; }
else {
if ( a[ p1.se ].id < a[ p2.se ].id ) { return p1 ; }
return p2 ;
}
}
void init ( int w , int l , int r ) {
lazy[ w ] = 0 ;
if ( l == r ) {
tr[ w ] = { inf , l } ;
return ;
}
int mid = ( l + r ) / 2 ;
init ( 2 * w , l , mid ) ;
init ( 2 * w + 1 , mid + 1 , r ) ;
tr[ w ] = unite ( tr[ 2 * w ] , tr[ 2 * w + 1 ] ) ;
}
void update ( int w , int l , int r , int st , int en , ll add ) {
push_lazy ( w , l , r ) ;
if ( l > r || st > en ) { return ; }
if ( r < st || en < l ) { return ; }
if ( st <= l && r <= en ) {
lazy[ w ] += add ;
push_lazy ( w , l , r ) ;
return ;
}
int mid = ( l + r ) / 2 ;
update ( 2 * w , l , mid , st , en , add ) ;
update ( 2 * w + 1 , mid + 1 , r , st , en , add ) ;
tr[ w ] = unite ( tr[ 2 * w ] , tr[ 2 * w + 1 ] ) ;
}
int query ( ) { return tr[ 1 ].se ; }
};
rem_sum_tree rem_lens ;
set < pii > cands_left , cands_right ;
set < int > alive ;
bool used[ MAXN ] ;
void solve ( ) {
cin >> n ;
vector < int > all_vals ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ].l >> a[ i ].r ;
a[ i ].rem = a[ i ].r - a[ i ].l ;
a[ i ].id = i ;
all_vals.push_back ( a[ i ].l ) ;
all_vals.push_back ( a[ i ].r ) ;
}
sort ( all_vals.begin ( ) , all_vals.end ( ) ) ;
nwvals[ all_vals[ 0 ] ] = 1 ;
rev_vals[ 1 ] = all_vals[ 0 ] ;
mxval = 1 ;
for ( int i = 1 ; i < 2 * n ; ++ i ) {
if ( all_vals[ i ] == all_vals[ i - 1 ] ) { continue ; }
nwvals[ all_vals[ i ] ] = ++ mxval ;
rev_vals[ mxval ] = all_vals[ i ] ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
a[ i ].l = nwvals[ a[ i ].l ] ;
a[ i ].r = nwvals[ a[ i ].r ] ;
}
auto cmp = [ & ] ( range p1 , range p2 ) {
if ( p1.l != p2.l ) { return ( p1.l < p2.l ) ; }
return ( p1.r > p2.r ) ;
};
sort ( a + 1 , a + n + 1 , cmp ) ;
for ( int i = 1 ; i < mxval ; ++ i ) {
lens[ i ] = rev_vals[ i + 1 ] - rev_vals[ i ] ;
alive.insert ( i ) ;
}
w_sum.init ( 1 , 1 , mxval - 1 ) ;
current_min.init ( 1 , 1 , n ) ;
rem_lens.init ( 1 , 1 , n ) ;
int lst = 0 ;
while ( 1 ) {
auto [ val , id ] = current_min.query ( 1 , 1 , n , lst + 1 , n ) ;
if ( val == MAXN ) { break ; }
used[ id ] = true ;
cands_left.insert ( { a[ id ].l , id } ) ;
cands_right.insert ( { a[ id ].r , id } ) ;
rem_lens.update ( 1 , 1 , n , id , id , -inf + w_sum.query ( 1 , 1 , mxval - 1 , a[ id ].l , a[ id ].r - 1 ) ) ;
// current_min.update ( 1 , 1 , n , id ) ;
lst = id ;
}
for ( int ctr = 0 ; ctr < n ; ++ ctr ) {
int wh = rem_lens.query ( ) ;
cout << a[ wh ].id << " " ;
cands_left.erase ( { a[ wh ].l , wh } ) ;
cands_right.erase ( { a[ wh ].r , wh } ) ;
rem_lens.update ( 1 , 1 , n , wh , wh , inf ) ;
while ( 1 ) {
auto it = alive.lower_bound ( a[ wh ].l ) ;
if ( it == alive.end ( ) || *it >= a[ wh ].r ) { break ; }
int pos = *it ;
alive.erase ( pos ) ;
w_sum.update ( 1 , 1 , mxval - 1 , pos , -lens[ pos ] ) ;
auto r_end = cands_left.upper_bound ( make_pair ( pos , MAXN ) ) ;
auto l_end = cands_right.lower_bound ( make_pair ( pos + 1 , -MAXN ) ) ;
if ( l_end == cands_right.end ( ) || r_end == cands_left.begin ( ) ) { continue ; }
-- r_end ;
if ( l_end->se > r_end->se ) { continue ; }
rem_lens.update ( 1 , 1 , n , l_end->se , r_end->se , -lens[ pos ] ) ;
}
int lst = 0 ;
auto it = cands_left.lower_bound ( make_pair ( a[ wh ].l , wh ) ) ;
if ( it != cands_left.begin ( ) ) {
-- it ;
lst = it->se ;
}
while ( 1 ) {
auto [ val , id ] = current_min.query ( 1 , 1 , n , lst + 1 , n ) ;
if ( val == MAXN || used[ id ] == true ) { break ; }
used[ id ] = true ;
cands_left.insert ( { a[ id ].l , id } ) ;
cands_right.insert ( { a[ id ].r , id } ) ;
rem_lens.update ( 1 , 1 , n , id , id , -inf + w_sum.query ( 1 , 1 , mxval - 1 , a[ id ].l , a[ id ].r - 1 ) ) ;
lst = id ;
}
current_min.update ( 1 , 1 , n , wh ) ;
}
cout << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 46436kb
input:
6 1 2 2 3 3 4 4 5 1 3 3 5
output:
1 2 3 5 4 5
result:
wrong answer 3rd words differ - expected: '5', found: '3'