結果

問題 No.1209 XOR Into You
ユーザー pockynypockyny
提出日時 2020-08-30 14:46:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 408 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 980 ms
コンパイル使用メモリ 91,520 KB
実行使用メモリ 31,584 KB
最終ジャッジ日時 2023-08-09 12:38:22
合計ジャッジ時間 13,249 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 74 ms
4,380 KB
testcase_05 AC 73 ms
4,376 KB
testcase_06 AC 174 ms
14,288 KB
testcase_07 AC 315 ms
26,800 KB
testcase_08 AC 273 ms
24,536 KB
testcase_09 AC 275 ms
24,688 KB
testcase_10 AC 207 ms
20,668 KB
testcase_11 AC 262 ms
24,268 KB
testcase_12 AC 236 ms
22,768 KB
testcase_13 AC 274 ms
25,452 KB
testcase_14 AC 226 ms
22,240 KB
testcase_15 AC 241 ms
23,308 KB
testcase_16 AC 234 ms
23,000 KB
testcase_17 AC 219 ms
22,028 KB
testcase_18 AC 369 ms
30,476 KB
testcase_19 AC 244 ms
22,952 KB
testcase_20 AC 291 ms
25,624 KB
testcase_21 AC 203 ms
20,268 KB
testcase_22 AC 246 ms
31,548 KB
testcase_23 AC 241 ms
31,512 KB
testcase_24 AC 241 ms
31,420 KB
testcase_25 AC 380 ms
31,456 KB
testcase_26 AC 400 ms
31,440 KB
testcase_27 AC 386 ms
31,472 KB
testcase_28 AC 398 ms
31,428 KB
testcase_29 AC 378 ms
31,436 KB
testcase_30 AC 408 ms
31,528 KB
testcase_31 AC 139 ms
7,288 KB
testcase_32 AC 141 ms
7,312 KB
testcase_33 AC 140 ms
7,284 KB
testcase_34 AC 146 ms
7,248 KB
testcase_35 AC 142 ms
7,264 KB
testcase_36 AC 140 ms
7,452 KB
testcase_37 AC 90 ms
6,680 KB
testcase_38 AC 295 ms
26,600 KB
testcase_39 AC 250 ms
24,044 KB
testcase_40 AC 238 ms
31,584 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