結果

問題 No.1209 XOR Into You
ユーザー pockynypockyny
提出日時 2020-08-30 14:46:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 92,076 KB
実行使用メモリ 31,616 KB
最終ジャッジ日時 2024-04-27 07:41:21
合計ジャッジ時間 13,000 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 82 ms
6,944 KB
testcase_05 AC 83 ms
6,944 KB
testcase_06 AC 201 ms
14,464 KB
testcase_07 AC 351 ms
27,216 KB
testcase_08 AC 312 ms
24,576 KB
testcase_09 AC 305 ms
24,704 KB
testcase_10 AC 239 ms
20,736 KB
testcase_11 AC 300 ms
24,448 KB
testcase_12 AC 282 ms
23,372 KB
testcase_13 AC 309 ms
25,728 KB
testcase_14 AC 267 ms
22,732 KB
testcase_15 AC 295 ms
23,424 KB
testcase_16 AC 289 ms
23,040 KB
testcase_17 AC 258 ms
22,604 KB
testcase_18 AC 433 ms
30,412 KB
testcase_19 AC 275 ms
23,040 KB
testcase_20 AC 337 ms
25,728 KB
testcase_21 AC 232 ms
20,352 KB
testcase_22 AC 257 ms
31,564 KB
testcase_23 AC 250 ms
31,564 KB
testcase_24 AC 256 ms
31,568 KB
testcase_25 AC 471 ms
31,568 KB
testcase_26 AC 436 ms
31,616 KB
testcase_27 AC 455 ms
31,560 KB
testcase_28 AC 443 ms
31,564 KB
testcase_29 AC 450 ms
31,616 KB
testcase_30 AC 451 ms
31,568 KB
testcase_31 AC 151 ms
7,424 KB
testcase_32 AC 152 ms
7,368 KB
testcase_33 AC 154 ms
7,424 KB
testcase_34 AC 158 ms
7,424 KB
testcase_35 AC 150 ms
7,368 KB
testcase_36 AC 153 ms
7,372 KB
testcase_37 AC 98 ms
6,944 KB
testcase_38 AC 338 ms
26,752 KB
testcase_39 AC 288 ms
24,320 KB
testcase_40 AC 253 ms
31,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
ll n,seg[200010],INF = 1000000000000;
void init(){
    for(int i=n - 1;i>0;i--){
        seg[i] = seg[i<<1] + seg[i<<1|1];
    }
}

void update(int p,ll val){
    for(seg[p += n] = val;p>1;p>>=1){
        seg[p>>1] = seg[p] + seg[p^1];
    }
}

ll query(int l,int r){
    ll res = 0;
    for(l += n,r += n; l<r;l>>=1,r>>=1){
        if(l&1) res += seg[l++];
        if(r&1) res += seg[--r];
    }
    return res;
}

int a[100010],b[100010],c[100010],d[100010];
map<int,int> mp1,mp2,mp4;
map<int,vector<int>> mp3;
int main(){
    int i; cin >> n;
    for(i=0;i<n;i++) cin >> a[i];
    for(i=0;i<n;i++) cin >> b[i];
    if(a[0]!=b[0] || a[n - 1]!=b[n - 1]){
        cout << -1 << endl;
        return 0;
    }
    n--;
    for(i=0;i<n;i++){
        c[i] = a[i]^a[i + 1]; d[i] = b[i]^b[i + 1];
        mp1[c[i]]++; mp2[d[i]]++;
    }
    for(auto x:mp1){
        int y = x.first;
        if(mp2[y]!=x.second){
            cout << -1 << endl;
            return 0;
        }
    }
    for(i=0;i<n;i++) mp3[c[i]].push_back(i + 1);
    /*for(i=0;i<n;i++) cout << c[i] << " ";
    cout << endl;
    for(i=0;i<n;i++) cout << d[i] << " ";
    cout << endl;*/
    for(i=0;i<n;i++){
        int k = 0;
        if(mp4.find(d[i])!=mp4.end()) k = mp4[d[i]] + 1;
        //cout << k << " " << mp3[d[i]][k] << endl;
        mp4[d[i]] = k; d[i] = mp3[d[i]][k];
    }
    /*for(i=0;i<n;i++) cout << d[i] << " ";
    cout << endl;*/
    ll ans = 0;
    for(i=0;i<n;i++){
        update(d[i] - 1,1);
        ans += query(d[i],n);
    }
    cout << ans << endl;
}
0