QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#158790 | #7108. Couleur | ucup-team1325# | AC ✓ | 2529ms | 36972kb | C++17 | 3.4kb | 2023-09-02 17:01:10 | 2023-09-02 17:01:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef unsigned long long ull;
const int inf = 1e9; const ll llnf = 4e18;
template< class Tp > void chkmax( Tp &x , Tp y ) { x = max( x , y ); }
template< class Tp > void chkmin( Tp &x , Tp y ) { x = min( x , y ); }
template< class Int >
struct pqueue {
private:
priority_queue< Int > p , q;
public:
pqueue( ) { while( !p.empty( ) ) p.pop( ); while( !q.empty( ) ) q.pop( ); }
void ins( Int x ) { p.emplace( x ); }
void era( Int x ) { q.emplace( x ); }
Int top( ) { while( !p.empty( ) && !q.empty( ) && p.top( ) == q.top( ) ) p.pop( ) , q.pop( ); return p.top( ); }
} ;
void solve( ) {
int n; cin >> n;
vector< int > a( n + 1 ); for( int i = 1; i <= n; i ++ ) cin >> a[i];
int N = n * ( __lg( n ) + 8 ); vector< int > ls( N ) , rs( N ) , cnt( N ); vector< int > root( n + 1 ); int pnode = 0;
auto add = [&] ( auto add , int &o , int p , int L , int R , int x ) -> void {
o = ++ pnode; ls[o] = ls[p] , rs[o] = rs[p] , cnt[o] = cnt[p] + 1;
if( L == x && R == x ) return void( );
else { int M = ( L + R ) / 2; ( x <= M ) ? ( add( add , ls[o] , ls[p] , L , M , x ) ) : ( add( add , rs[o] , rs[p] , M + 1 , R , x ) ); }
} ;
auto qry = [&] ( auto qry , int o , int L , int R , int l , int r ) -> int {
if( !o || l > R || r < L ) return 0;
else if( l <= L && R <= r ) return cnt[o];
else { int M = ( L + R ) / 2; return qry( qry , ls[o] , L , M , l , r ) + qry( qry , rs[o] , M + 1 , R , l , r ); }
} ;
for( int i = 1; i <= n; i ++ ) add( add , root[i] , root[i - 1] , 1 , n , a[i] );
ll ninv = 0; for( int i = 1; i <= n; i ++ ) ninv += qry( qry , root[i - 1] , 1 , n , a[i] + 1 , n );
pqueue< ll > pque; set< tuple< int , int , ll > > S; pque.ins( ninv ) , S.emplace( 1 , n , ninv );
vector< ll > ans( n + 1 ); ans[0] = ninv;
for( int t = 1; t <= n; t ++ ) {
ll _k; cin >> _k; int k = _k ^ ans[t - 1];
auto [ l , r , s ] = *prev( S.lower_bound( make_tuple( k + 1 , 0 , 0 ) ) ); S.erase( make_tuple( l , r , s ) ) , pque.era( s );
if( k - l <= r - k ) {
ll sl = 0 , sr = s - ( qry( qry , root[r] , 1 , n , 1 , a[k] - 1 ) - qry( qry , root[k] , 1 , n , 1 , a[k] - 1 ) );
for( int i = l; i <= k - 1; i ++ ) sl += qry( qry , root[i - 1] , 1 , n , a[i] + 1 , n ) - qry( qry , root[l - 1] , 1 , n , a[i] + 1 , n );
for( int i = l; i <= k - 1; i ++ ) sr -= qry( qry , root[r] , 1 , n , 1 , a[i] - 1 ) - qry( qry , root[i] , 1 , n , 1 , a[i] - 1 );
if( l <= k - 1 ) S.emplace( l , k - 1 , sl ) , pque.ins( sl ); if( k + 1 <= r ) S.emplace( k + 1 , r , sr ) , pque.ins( sr );
} else {
ll sl = s - ( qry( qry , root[k - 1] , 1 , n , a[k] + 1 , n ) - qry( qry , root[l - 1] , 1 , n , a[k] + 1 , n ) ) , sr = 0;
for( int i = k + 1; i <= r; i ++ ) sr += qry( qry , root[r] , 1 , n , 1 , a[i] - 1 ) - qry( qry , root[i] , 1 , n , 1 , a[i] - 1 );
for( int i = k + 1; i <= r; i ++ ) sl -= qry( qry , root[i - 1] , 1 , n , a[i] + 1 , n ) - qry( qry , root[l - 1] , 1 , n , a[i] + 1 , n );
if( l <= k - 1 ) S.emplace( l , k - 1 , sl ) , pque.ins( sl ); if( k + 1 <= r ) S.emplace( k + 1 , r , sr ) , pque.ins( sr );
}
if( t < n ) ans[t] = pque.top( );
}
for( int t = 0; t <= n - 1; t ++ ) cout << ans[t] << " \n"[t == n - 1]; cout.flush( );
}
int main( ) {
ios::sync_with_stdio( 0 ), cin.tie( 0 ), cout.tie( 0 );
int T; cin >> T; while( T -- ) solve( ); return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3700kb
input:
3 5 4 3 1 1 1 5 4 5 3 1 10 9 7 1 4 7 8 5 7 4 8 21 8 15 5 9 2 4 5 10 6 15 4 8 8 1 12 1 10 14 7 14 2 9 13 10 3 37 19 23 15 7 2 10 15 2 13 4 5 8 7 10
output:
7 0 0 0 0 20 11 7 2 0 0 0 0 0 0 42 31 21 14 14 4 1 1 1 0 0 0 0 0 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2529ms
memory: 36972kb
input:
11116 10 10 5 10 3 6 4 8 5 9 8 31 27 24 11 12 3 0 2 3 1 10 8 2 7 2 8 10 1 10 9 10 6 5 2 13 2 1 0 1 3 1 10 7 10 7 6 1 3 10 6 7 9 21 18 10 1 6 5 4 8 9 10 10 2 10 4 8 8 5 7 2 6 7 20 10 9 1 15 0 4 2 9 7 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 6 3 5 2 7 10 9 1 4 8 10 1 10 1 3...
output:
21 18 16 12 10 6 4 1 1 0 12 12 10 10 4 4 4 2 1 0 20 16 9 5 3 3 3 0 0 0 22 14 8 8 5 5 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 12 7 4 4 2 2 1 0 0 20 18 8 3 1 1 0 0 0 0 45 21 21 10 3 3 3 0 0 0 17 11 8 2 1 1 1 0 0 0 13 4 1 0 0 0 0 0 0 0 29 27 22 15 9 7 4 3 1 0 26 16 9 2 1 1 1 1 1 0 0 0 0 0 0 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed