結果

問題 No.1209 XOR Into You
ユーザー gosou513gosou513
提出日時 2024-03-13 16:44:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 12,570 bytes
コンパイル時間 2,532 ms
コンパイル使用メモリ 211,364 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-09-29 22:54:04
合計ジャッジ時間 5,131 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 25 ms
6,820 KB
testcase_05 AC 25 ms
6,820 KB
testcase_06 AC 45 ms
6,820 KB
testcase_07 AC 41 ms
6,816 KB
testcase_08 AC 36 ms
6,816 KB
testcase_09 AC 37 ms
6,816 KB
testcase_10 AC 34 ms
6,816 KB
testcase_11 AC 36 ms
6,816 KB
testcase_12 AC 35 ms
6,820 KB
testcase_13 AC 39 ms
6,816 KB
testcase_14 AC 32 ms
6,816 KB
testcase_15 AC 34 ms
6,820 KB
testcase_16 AC 33 ms
6,820 KB
testcase_17 AC 32 ms
6,820 KB
testcase_18 AC 47 ms
6,816 KB
testcase_19 AC 34 ms
6,820 KB
testcase_20 AC 38 ms
6,820 KB
testcase_21 AC 29 ms
6,816 KB
testcase_22 AC 30 ms
6,816 KB
testcase_23 AC 30 ms
6,820 KB
testcase_24 AC 31 ms
6,816 KB
testcase_25 AC 50 ms
6,820 KB
testcase_26 AC 50 ms
6,820 KB
testcase_27 AC 48 ms
6,816 KB
testcase_28 AC 48 ms
6,816 KB
testcase_29 AC 48 ms
6,816 KB
testcase_30 AC 50 ms
6,816 KB
testcase_31 AC 55 ms
6,820 KB
testcase_32 AC 53 ms
6,816 KB
testcase_33 AC 53 ms
6,820 KB
testcase_34 AC 55 ms
6,820 KB
testcase_35 AC 53 ms
6,816 KB
testcase_36 AC 53 ms
6,816 KB
testcase_37 AC 36 ms
6,816 KB
testcase_38 AC 39 ms
6,820 KB
testcase_39 AC 36 ms
6,820 KB
testcase_40 AC 31 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#ifdef LOCAL
#include <debug_print.hpp>
#define debug( ... ) debug_print::multi_print( #__VA_ARGS__, __VA_ARGS__ )
#else
#define debug( ... ) ( static_cast< void >( 0 ) )
#endif

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair< int, int >;
using pll = pair< ll, ll >;
template< class T > using vc = vector< T >;
template< class T > using vvc = vector< vector< T > >;
template< class T > using vvvc = vector< vector< vector< T > > >;
template< class T > using vvvvc = vector< vector< vector< vector< T > > > >;
template< class T > using pq = priority_queue< T >;
template< class T > using pqg = priority_queue< T, vector< T >, greater< T > >;
template< class T > using mset = multiset< T >;
template< class T > using uset = unordered_set< T >;
template< class T, class U > using umap = unordered_map< T, U >;
template< class T > struct Edge {
    int from, to;
    T cost;
    int id;
    Edge( int from_, int to_, T cost_ = 1, int id_ = -1 ) : from( from_ ), to( to_ ), cost( cost_ ), id( id_ ) {}
};
template< class T > struct Graph {
    vector< vector< Edge< T > > > G;
    int e;
    bool is_directed;

    Graph() {}
    Graph( int n, bool is_directed_ = false ) : G( n ), e( 0 ), is_directed( is_directed_ ) {}
    int size() const {
        return G.size();
    }
    void add_edge( int from, int to, T cost = 1 ) {
        G[ from ].emplace_back( from, to, cost, e );
        if ( !is_directed ) G[ to ].emplace_back( to, from, cost, e );
        e++;
    }
    void add_edge( int from, int to, T cost, int id ) {
        G[ from ].emplace_back( from, to, cost, id );
        if ( !is_directed ) G[ to ].emplace_back( to, from, cost, id );
        e++;
    }
    inline vector< Edge< T > > &operator[]( int i ) {
        return G[ i ];
    }
};
template< class T > using Edges = vector< Edge< T > >;

#define vec( type, name, ... ) vector< type > name( __VA_ARGS__ )
#define vv( type, name, h, ... ) vector name( h, vector< type >( __VA_ARGS__ ) )
#define vvv( type, name, h, w, ... ) vector name( h, vector( w, vector< type >( __VA_ARGS__ ) ) )
#define vvvv( type, name, a, b, c, ... ) vector< vector< vector< vector< type > > > > name( a, vector< vector< vector< type > > >( b, vector< vector< type > >( c, vector< type >( __VA_ARGS__ ) ) ) )

const int INF = 0x3fffffff;
const ll LINF = 0x1fffffffffffffff;
const ld DINF = numeric_limits< ld >::infinity();
const ld EPS = 1e-10;
const ld PI = 3.1415926535897932;
const ll dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const ll dy[] = { 1, 0, -1, 0, 1, 1, -1, -1 };

#define overload5( a, b, c, d, e, name, ... ) name
#define overload4( a, b, c, d, name, ... ) name
#define overload3( a, b, c, name, ... ) name
#define rep0( n ) for ( ll qwerty = 0; qwerty < n; qwerty++ )
#define rep1( i, n ) for ( ll i = 0; i < ( n ); i++ )
#define rep2( i, a, b ) for ( ll i = ( a ); i < ( b ); i++ )
#define rep3( i, a, b, c ) for ( ll i = ( a ); i < ( b ); i += ( c ) )
#define rep( ... ) overload4( __VA_ARGS__, rep3, rep2, rep1, rep0 )( __VA_ARGS__ )
#define rrep0( n ) for ( ll qwerty = (n)-1; qwerty >= 0; qwerty-- )
#define rrep1( i, n ) for ( ll i = (n)-1; i >= 0; i-- )
#define rrep2( i, a, b ) for ( ll i = (a)-1; i >= ( b ); i-- )
#define rrep3( i, a, b, c ) for ( ll i = (a)-1; i >= ( b ); i -= ( c ) )
#define rrep( ... ) overload4( __VA_ARGS__, rrep3, rrep2, rrep1, rrep0 )( __VA_ARGS__ )
#define each1( i, a ) for ( auto &&i : a )
#define each2( x, y, a ) for ( auto &&[ x, y ] : a )
#define each3( x, y, z, a ) for ( auto &&[ x, y, z ] : a )
#define each4( w, x, y, z, a ) for ( auto &&[ w, x, y, z ] : a )
#define each( ... ) overload5( __VA_ARGS__, each4, each3, each2, each1 )( __VA_ARGS__ )
#define all1( i ) begin( i ), end( i )
#define all2( i, a ) begin( i ), begin( i ) + a
#define all3( i, a, b ) begin( i ) + a, begin( i ) + b
#define all( ... ) overload3( __VA_ARGS__, all3, all2, all1 )( __VA_ARGS__ )
#define rall1( i ) rbegin( i ), rend( i )
#define rall2( i, a ) rbegin( i ), rbegin( i ) + a
#define rall3( i, a, b ) rbegin( i ) + a, rbegin( i ) + b
#define rall( ... ) overload3( __VA_ARGS__, rall3, rall2, rall1 )( __VA_ARGS__ )

#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define Endl '\n'
#define ifnot( x ) if ( !( x ) )
#define YesNo( x ) cout << ( x ? "Yes" : "No" ) << endl;

#define bit( i ) ( 1LL << ( i ) )
#define getbit( x, i ) ( ( ( x ) >> ( i ) ) & 1 )
#define topbit( x ) ( ( x ) == 0 ? -1 : 63 - __builtin_clzll( x ) )
#define lowbit( x ) ( ( x ) == 0 ? 64 : __builtin_ctzll( x ) )
#define popcount( x ) __builtin_popcountll( x )
#define mask( i ) ( ( 1LL << ( i ) ) - 1 )

template< class T > vector< T > &operator++( vector< T > &v ) {
    for ( auto &e : v ) e++;
    return v;
}
template< class T > vector< T > operator++( vector< T > &v, signed ) {
    auto res = v;
    for ( auto &e : v ) e++;
    return res;
}
template< class T > vector< T > &operator--( vector< T > &v ) {
    for ( auto &e : v ) e--;
    return v;
}
template< class T > vector< T > operator--( vector< T > &v, signed ) {
    auto res = v;
    for ( auto &e : v ) e--;
    return res;
}

#define INT( ... )   \
    int __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define INTd( ... )  \
    int __VA_ARGS__; \
    IN2( __VA_ARGS__ )
#define LL( ... )   \
    ll __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define LLd( ... )  \
    ll __VA_ARGS__; \
    IN2( __VA_ARGS__ )
#define STR( ... )      \
    string __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define CHR( ... )    \
    char __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define DBL( ... )      \
    double __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define LD( ... )            \
    long double __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define VEC( type, name, size )  \
    vector< type > name( size ); \
    IN( name )
#define VECd( type, name, size ) \
    vector< type > name( size ); \
    IN2( name )
#define VEC2( type, name1, name2, size )         \
    vector< type > name1( size ), name2( size ); \
    for ( int i = 0; i < size; i++ ) IN( name1[ i ], name2[ i ] )
#define VEC2d( type, name1, name2, size )        \
    vector< type > name1( size ), name2( size ); \
    for ( int i = 0; i < size; i++ ) IN2( name1[ i ], name2[ i ] )
#define VEC3( type, name1, name2, name3, size )                 \
    vector< type > name1( size ), name2( size ), name3( size ); \
    for ( int i = 0; i < size; i++ ) IN( name1[ i ], name2[ i ], name3[ i ] )
#define VEC3d( type, name1, name2, name3, size )                \
    vector< type > name1( size ), name2( size ), name3( size ); \
    for ( int i = 0; i < size; i++ ) IN2( name1[ i ], name2[ i ], name3[ i ] )
#define VEC4( type, name1, name2, name3, name4, size )                         \
    vector< type > name1( size ), name2( size ), name3( size ), name4( size ); \
    for ( int i = 0; i < size; i++ ) IN( name1[ i ], name2[ i ], name3[ i ], name4[ i ] );
#define VEC4d( type, name1, name2, name3, name4, size )                        \
    vector< type > name1( size ), name2( size ), name3( size ), name4( size ); \
    for ( int i = 0; i < size; i++ ) IN2( name1[ i ], name2[ i ], name3[ i ], name4[ i ] );
#define VV( type, name, h, w )                               \
    vector< vector< type > > name( h, vector< type >( w ) ); \
    IN( name )
#define VVd( type, name, h, w )                              \
    vector< vector< type > > name( h, vector< type >( w ) ); \
    IN2( name )
int scan() {
    return getchar();
}
void scan( int &a ) {
    cin >> a;
}
void scan( long long &a ) {
    cin >> a;
}
void scan( char &a ) {
    cin >> a;
}
void scan( double &a ) {
    cin >> a;
}
void scan( string &a ) {
    cin >> a;
}
template< class T, class S > void scan( pair< T, S > &p ) {
    scan( p.first ), scan( p.second );
}
template< class T > void scan( vector< T > & );
template< class T > void scan( vector< T > &a ) {
    for ( auto &i : a ) scan( i );
}
template< class T > void scan( T &a ) {
    cin >> a;
}
void IN() {}
void IN2() {}
template< class Head, class... Tail > void IN( Head &head, Tail &...tail ) {
    scan( head );
    IN( tail... );
}
template< class Head, class... Tail > void IN2( Head &head, Tail &...tail ) {
    scan( head );
    head--;
    IN2( tail... );
}

template< class T, class S > bool inc( const T &x, const S &l, const S &r ) {
    return l <= x && x < r;
}
template< typename T > int si( const T &x ) {
    return x.size();
}
template< class T, class S > inline bool chmax( T &a, const S &b ) {
    return ( a < b ? a = b, 1 : 0 );
}
template< class T, class S > inline bool chmin( T &a, const S &b ) {
    return ( a > b ? a = b, 1 : 0 );
}
vector< int > iota( int n, int s = 0 ) {
    vector< int > ret( n );
    iota( ret.begin(), ret.end(), s );
    return ret;
}
template< class T > void sort( T &v ) {
    sort( v.begin(), v.end() );
}
template< class T > void rsort( T &v ) {
    sort( v.rbegin(), v.rend() );
}
template< class T > void reverse( T &v ) {
    reverse( v.begin(), v.end() );
}
template< class T > auto max( const T &a ) {
    return *max_element( a.begin(), a.end() );
}
template< class T > auto min( const T &a ) {
    return *min_element( a.begin(), a.end() );
}
template< class T > int max_id( const T &a ) {
    return max_element( a.begin(), a.end() ) - a.begin();
}
template< class T > int min_id( const T &a ) {
    return min_element( a.begin(), a.end() ) - a.begin();
}
template< class T, class S > int lb( vector< T > &v, S a ) {
    return lower_bound( v.begin(), v.end(), (T)a ) - v.begin();
}
template< class T, class S > int ub( vector< T > &v, S a ) {
    return upper_bound( v.begin(), v.end(), (T)a ) - v.begin();
}
template< typename T = ll, typename S > T sum( const S &v ) {
    return accumulate( v.begin(), v.end(), T( 0 ) );
}
template< class T > vector< T > uniq( vector< T > v ) {
    sort( v.begin(), v.end() );
    v.erase( unique( v.begin(), v.end() ), v.end() );
    return v;
}

long long max( int x, long long y ) {
    return max( (long long)x, y );
}
long long max( long long x, int y ) {
    return max( x, (long long)y );
}
long long min( int x, long long y ) {
    return min( (long long)x, y );
}
long long min( long long x, int y ) {
    return min( x, (long long)y );
}
template< class T, class S > T floor( T x, S y ) {
    if ( y < 0 ) x = -x, y = -y;
    return ( x >= 0 ? x / y : ( x - y + 1 ) / y );
}
template< class T, class S > T ceil( T x, S y ) {
    if ( y < 0 ) x = -x, y = -y;
    return ( x >= 0 ? ( x + y - 1 ) / y : x / y );
}

template< class T > class FenwickTree {
    int N;
    T unity = 0;
    vector< T > dat;

   public:
    FenwickTree( int N_, T unity_ = 0 ) : N( N_ ), unity( unity_ ), dat( N + 1, unity ) {}

    void add( int idx, T val ) {
        idx += 1;
        for ( int i = idx; i <= N; i += ( i & -i ) ) {
            dat[ i ] += val;
        }
    }
    // [0,idx)
    T sum( int idx ) {
        T ret = unity;
        for ( int i = idx; i > 0; i -= ( i & -i ) ) {
            ret += dat[ i ];
        }
        return ret;
    }
    // [idx1,idx2)
    T sum( int idx1, int idx2 ) {
        return sum( idx2 ) - sum( idx1 );
    }
};

template< class T > long long swap_distance( const vector< T > &v1, const vector< T > &v2 ) {
    int N = v1.size();
    assert( N == (int)v2.size() );

    vector< int > ord1( N ), ord2( N );
    iota( ord1.begin(), ord1.end(), 0 );
    sort( ord1.begin(), ord1.end(), [ & ]( int i, int j ) {
        if ( v1[ i ] == v1[ j ] ) return i < j;
        return v1[ i ] < v1[ j ];
    } );

    iota( ord2.begin(), ord2.end(), 0 );
    sort( ord2.begin(), ord2.end(), [ & ]( int i, int j ) {
        if ( v2[ i ] == v2[ j ] ) return i < j;
        return v2[ i ] < v2[ j ];
    } );

    vector< int > w( N );
    for ( int i = 0; i < N; i++ ) {
        if ( v1[ ord1[ i ] ] != v2[ ord2[ i ] ] ) return -1;
        w[ ord2[ i ] ] = ord1[ i ];
    }

    long long ret = 0;
    FenwickTree< int > ft( N );
    for ( int i = 0; i < N; i++ ) {
        ret += ft.sum( w[ i ] + 1, N );
        ft.add( w[ i ], 1 );
    }
    return ret;
}

int main() {
    cin.tie( 0 );
    ios::sync_with_stdio( false );
    cout << fixed << setprecision( 15 );

    INT( N );
    VEC( int, A, N );
    VEC( int, B, N );

    vec( int, diff_A, N - 1 );
    rep( i, N - 1 ) diff_A[ i ] = A[ i ] ^ A[ i + 1 ];
    vec( int, diff_B, N - 1 );
    rep( i, N - 1 ) diff_B[ i ] = B[ i ] ^ B[ i + 1 ];

    if ( A[ 0 ] != B[ 0 ] || A[ N - 1 ] != B[ N - 1 ] ) cout << -1 << endl;
    else cout << swap_distance( diff_A, diff_B ) << endl;
}
0