結果

問題 No.1209 XOR Into You
ユーザー ゆきのんゆきのん
提出日時 2020-08-30 22:53:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 2,121 bytes
コンパイル時間 1,886 ms
コンパイル使用メモリ 185,220 KB
実行使用メモリ 32,128 KB
最終ジャッジ日時 2024-04-27 13:35:16
合計ジャッジ時間 11,595 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 81 ms
6,944 KB
testcase_05 AC 81 ms
6,940 KB
testcase_06 AC 220 ms
26,624 KB
testcase_07 AC 277 ms
27,392 KB
testcase_08 AC 242 ms
24,960 KB
testcase_09 AC 254 ms
24,960 KB
testcase_10 AC 193 ms
20,992 KB
testcase_11 AC 235 ms
24,704 KB
testcase_12 AC 222 ms
23,168 KB
testcase_13 AC 253 ms
25,984 KB
testcase_14 AC 199 ms
22,656 KB
testcase_15 AC 228 ms
23,808 KB
testcase_16 AC 210 ms
23,296 KB
testcase_17 AC 210 ms
22,400 KB
testcase_18 AC 318 ms
30,976 KB
testcase_19 AC 225 ms
23,296 KB
testcase_20 AC 258 ms
26,112 KB
testcase_21 AC 184 ms
20,608 KB
testcase_22 AC 218 ms
32,128 KB
testcase_23 AC 223 ms
32,128 KB
testcase_24 AC 218 ms
32,128 KB
testcase_25 AC 348 ms
32,128 KB
testcase_26 AC 345 ms
32,128 KB
testcase_27 AC 349 ms
32,128 KB
testcase_28 AC 344 ms
32,128 KB
testcase_29 AC 360 ms
32,128 KB
testcase_30 AC 346 ms
32,128 KB
testcase_31 AC 120 ms
6,940 KB
testcase_32 AC 120 ms
6,940 KB
testcase_33 AC 121 ms
6,940 KB
testcase_34 AC 132 ms
6,944 KB
testcase_35 AC 126 ms
6,944 KB
testcase_36 AC 123 ms
6,940 KB
testcase_37 AC 89 ms
6,940 KB
testcase_38 AC 273 ms
27,136 KB
testcase_39 AC 226 ms
24,448 KB
testcase_40 AC 216 ms
32,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

template< typename T >
struct BinaryIndexedTree{

    //0-indexed
    vector< T > bit;

    BinaryIndexedTree(int sz){
        bit.assign(sz, 0);
    }

    //[0,k)までの総和を返す
    T sum(int k){
        T ret = 0;
        for (--k; k >= 0; k = (k&(k+1))-1) ret += bit[k];
        return ret;
    }
    
    //[l,r)の総和を返す
    T sum(int l, int r){
        return sum(r) - sum(l);
    }

    //kにx加算する
    void add(int k, T x){
        for (; k < bit.size(); k |= k+1) bit[k] += x;
    }
};
 
int main(){
    int n;cin >> n;
    vector<int> a(n),b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    if(a[0] != b[0] || a[n-1] != b[n-1]){
        puts("-1");
        return 0;
    }
    vector<int> av(n-1), bv(n-1);
    map<int,vector<int> > ma,mb;
    for (int i = 0; i < n-1; i++) {
        av[i] = a[i] ^ a[i+1];
        ma[av[i]].pb(i);
        bv[i] = b[i] ^ b[i+1];
        mb[bv[i]].pb(i);
    }
    for(auto A:ma){
        if(A.sc.size() != mb[A.fs].size()){
            puts("-1");
            return 0;
        }
    }
    vector<int> map_av(n - 1);
    map<int,int> m_id;
    for (int i = 0; i < n-1; i++) {
        map_av[i] = mb[av[i]][m_id[av[i]]++];
    }
    BinaryIndexedTree<int> bit(n);
    LL ans = 0;
    for (int i = 0; i < n-1; i++) {
        ans += bit.sum(map_av[i], n);
        bit.add(map_av[i], 1);
    }
    cout << ans << endl;
    return 0;
}
0