結果

問題 No.1209 XOR Into You
ユーザー mugen_1337mugen_1337
提出日時 2020-08-30 22:21:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 428 ms / 2,000 ms
コード長 3,135 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 223,092 KB
実行使用メモリ 92,288 KB
最終ジャッジ日時 2024-04-27 13:32:15
合計ジャッジ時間 12,923 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 25 ms
6,944 KB
testcase_05 AC 25 ms
6,944 KB
testcase_06 AC 121 ms
18,816 KB
testcase_07 AC 329 ms
77,568 KB
testcase_08 AC 310 ms
70,144 KB
testcase_09 AC 318 ms
70,272 KB
testcase_10 AC 224 ms
57,984 KB
testcase_11 AC 293 ms
69,248 KB
testcase_12 AC 258 ms
64,768 KB
testcase_13 AC 318 ms
73,344 KB
testcase_14 AC 246 ms
62,848 KB
testcase_15 AC 265 ms
66,304 KB
testcase_16 AC 271 ms
65,280 KB
testcase_17 AC 254 ms
62,464 KB
testcase_18 AC 381 ms
88,320 KB
testcase_19 AC 254 ms
65,024 KB
testcase_20 AC 295 ms
73,472 KB
testcase_21 AC 212 ms
56,704 KB
testcase_22 AC 220 ms
92,288 KB
testcase_23 AC 221 ms
92,288 KB
testcase_24 AC 221 ms
92,288 KB
testcase_25 AC 382 ms
92,288 KB
testcase_26 AC 384 ms
92,288 KB
testcase_27 AC 413 ms
92,160 KB
testcase_28 AC 428 ms
92,288 KB
testcase_29 AC 424 ms
92,288 KB
testcase_30 AC 408 ms
92,288 KB
testcase_31 AC 74 ms
8,832 KB
testcase_32 AC 72 ms
8,832 KB
testcase_33 AC 72 ms
8,832 KB
testcase_34 AC 73 ms
8,832 KB
testcase_35 AC 72 ms
8,704 KB
testcase_36 AC 72 ms
8,832 KB
testcase_37 AC 35 ms
8,320 KB
testcase_38 AC 306 ms
76,928 KB
testcase_39 AC 294 ms
68,608 KB
testcase_40 AC 219 ms
92,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

template<typename T>
struct BinaryIndexedTree{
   vector<T> data;
   BinaryIndexedTree(int sz){
       data.assign(sz+1, 0);
   }
   // 区間[0,k]の合計 sum(v[0]~v[k])
   T sum(int k){
       T ret=0;
       for(++k;k>0;k-=(k&-k)) ret+=data[k];
       return ret;
   }
   // v[a]+=x
   void add(int k,T x){
       for(++k;k<data.size();k+=(k&-k)) data[k]+=x;
   }
   // wを超える最小のv[i]のiを返す(0indexedのを返す)(data上のindexを返したいならreturn x+1)
   int lower_bound(T w){//T?
       if(w<=0) return 0;
       //左右どっちに行くか考える
       int x=0,r=1;
       while(r<data.size()) r<<=1;
       for(int k=r;k>0;k>>=1){
           if(x+k<=data.size()&&data[x+k]<w){
               //左部分を引いて右へ,再起っぽい
               w-=data[x+k];
               x+=k;
           }
       }
       // return x+1;
       return x;
   }
};
 
 
//転倒数を求める
ll inversion_number(vector<int> v){
    int n=v.size();
    if(n<=1){
        return 0;
    }
    int vmax=*max_element(ALL(v));
    BinaryIndexedTree<ll> bit(vmax+1);
    ll ret=0;
    rep(i,n){
        ret+=bit.sum(vmax)-bit.sum(v[i]);
        bit.add(v[i],1);
    }
    return ret;
}


signed main(){
    int n;cin>>n;
    vector<ll> a(n),b(n);
    cin>>a>>b;

    if(a[0]!=b[0] or a.back()!=b.back()){
        cout<<-1<<endl;
        return 0;
    }

    vector<ll> u(n-1),v(n-1);
    rep(i,n-1){
        u[i]=(a[i]^a[i+1]);
        v[i]=(b[i]^b[i+1]);
    }

    n--;
    map<ll,int> uc,vc;
    rep(i,n)uc[u[i]]++;
    rep(i,n)vc[v[i]]++;

    for(auto p:uc){
        if(p.second!=vc[p.first]){
            cout<<-1<<endl;
            return 0;
        }
    }

    map<ll,queue<int>> id;
    rep(i,n)id[u[i]].push(i);

    vector<int> is(n);
    rep(i,n){
        is[i]=id[v[i]].front();
        id[v[i]].pop();
    }
    cout<<inversion_number(is)<<endl;
    return 0;
}
0