結果

問題 No.1209 XOR Into You
ユーザー milanis48663220milanis48663220
提出日時 2020-09-01 00:29:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 1,224 ms
コンパイル使用メモリ 98,548 KB
実行使用メモリ 76,928 KB
最終ジャッジ日時 2024-04-28 11:43:22
合計ジャッジ時間 10,318 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 200 ms
76,032 KB
testcase_05 AC 203 ms
75,904 KB
testcase_06 AC 202 ms
76,928 KB
testcase_07 AC 244 ms
64,896 KB
testcase_08 AC 239 ms
58,880 KB
testcase_09 AC 220 ms
58,880 KB
testcase_10 AC 169 ms
48,512 KB
testcase_11 AC 219 ms
58,240 KB
testcase_12 AC 189 ms
54,528 KB
testcase_13 AC 221 ms
61,440 KB
testcase_14 AC 205 ms
52,992 KB
testcase_15 AC 206 ms
55,680 KB
testcase_16 AC 203 ms
54,656 KB
testcase_17 AC 205 ms
52,608 KB
testcase_18 AC 336 ms
73,600 KB
testcase_19 AC 210 ms
54,656 KB
testcase_20 AC 230 ms
61,440 KB
testcase_21 AC 185 ms
47,360 KB
testcase_22 AC 166 ms
76,928 KB
testcase_23 AC 166 ms
76,928 KB
testcase_24 AC 168 ms
76,800 KB
testcase_25 AC 294 ms
76,928 KB
testcase_26 AC 295 ms
76,928 KB
testcase_27 AC 298 ms
76,800 KB
testcase_28 AC 291 ms
76,800 KB
testcase_29 AC 293 ms
76,928 KB
testcase_30 AC 303 ms
76,800 KB
testcase_31 AC 65 ms
5,760 KB
testcase_32 AC 63 ms
5,760 KB
testcase_33 AC 64 ms
5,760 KB
testcase_34 AC 65 ms
5,760 KB
testcase_35 AC 64 ms
5,760 KB
testcase_36 AC 65 ms
5,760 KB
testcase_37 AC 33 ms
5,504 KB
testcase_38 AC 238 ms
64,384 KB
testcase_39 AC 218 ms
57,728 KB
testcase_40 AC 168 ms
76,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

int N;
int A[100001], B[100001];

template <typename T>
struct bit{
    int n;
    vector<T> data;

    bit(int n_){
        n = 1;
        while(n < n_) n *= 2;
        data = vector<T>(n+1);
        for(int i = 0; i <= n; i++) data[i] = 0;
    }
    
    T sum(int i){
        T ret = 0;
        while(i > 0){
            ret += data[i];
            i -= i&-i;
        }
        return ret;
    }

    void add(int i, T x){
        while(i <= n){
            data[i] += x;
            i += i&-i;
        }
    }
};

map<int, queue<int>> mp;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        if(i >= 1){
            mp[A[i]^A[i-1]].push(i);
        }
    }
    for(int i = 0; i < N; i++) cin >> B[i];
    if(A[0] != B[0]){
        cout << -1 << endl;
        return 0;
    }
    ll ans = 0;
    bit<ll> bt(N);
    for(int i = 1; i < N; i++){
        int b = B[i]^B[i-1];
        if(mp[b].empty()){
            cout << -1 << endl;
            return 0;
        }
        int idx = mp[b].front(); mp[b].pop();
        ans += (i-1-bt.sum(idx));
        bt.add(idx, 1);
    }
    cout << ans << endl;
}
0