結果

問題 No.1209 XOR Into You
ユーザー beetbeet
提出日時 2020-08-30 13:27:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 2,000 ms
コード長 2,072 bytes
コンパイル時間 2,956 ms
コンパイル使用メモリ 220,868 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2023-08-09 11:49:00
合計ジャッジ時間 12,867 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 26 ms
5,212 KB
testcase_05 AC 27 ms
4,948 KB
testcase_06 AC 42 ms
8,124 KB
testcase_07 AC 252 ms
17,980 KB
testcase_08 AC 222 ms
16,616 KB
testcase_09 AC 230 ms
16,560 KB
testcase_10 AC 152 ms
14,036 KB
testcase_11 AC 218 ms
16,424 KB
testcase_12 AC 201 ms
15,416 KB
testcase_13 AC 215 ms
17,192 KB
testcase_14 AC 178 ms
15,064 KB
testcase_15 AC 204 ms
15,740 KB
testcase_16 AC 213 ms
15,684 KB
testcase_17 AC 192 ms
15,076 KB
testcase_18 AC 315 ms
20,112 KB
testcase_19 AC 211 ms
15,632 KB
testcase_20 AC 248 ms
17,208 KB
testcase_21 AC 169 ms
13,864 KB
testcase_22 AC 173 ms
21,048 KB
testcase_23 AC 174 ms
21,024 KB
testcase_24 AC 174 ms
21,192 KB
testcase_25 AC 343 ms
21,248 KB
testcase_26 AC 337 ms
21,196 KB
testcase_27 AC 342 ms
21,136 KB
testcase_28 AC 340 ms
21,152 KB
testcase_29 AC 339 ms
21,196 KB
testcase_30 AC 339 ms
21,016 KB
testcase_31 AC 149 ms
14,864 KB
testcase_32 AC 147 ms
14,760 KB
testcase_33 AC 147 ms
14,748 KB
testcase_34 AC 148 ms
14,708 KB
testcase_35 AC 149 ms
14,924 KB
testcase_36 AC 150 ms
14,844 KB
testcase_37 AC 82 ms
14,688 KB
testcase_38 AC 245 ms
17,972 KB
testcase_39 AC 216 ms
16,408 KB
testcase_40 AC 174 ms
21,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T>
struct BIT{
  Int n;
  vector<T> bit;
  // 1-indexed
  BIT(Int n_):n(n_+1),bit(n+1,0){}

  T sum(Int i){
    T s(0);
    for(Int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }

  void add(Int i,T a){
    if(i==0) return;
    for(Int x=i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }

  // [l, r)
  T query(Int l,Int r){
    return sum(r-1)-sum(l-1);
  }

  Int lower_bound(Int w){
    if(w<=0) return 0;
    Int x=0,r=1;
    while(r<n) r<<=1;
    for(Int k=r;k>0;k>>=1){
      if(x+k<=n&&bit[x+k]<w){
        w-=bit[x+k];
        x+=k;
      }
    }
    return x+1;
  }

  // 0-indexed
  T sum0(Int i){return sum(i+1);}
  void add0(Int i,T a){add(i+1,a);}
  T query0(Int l,Int r){return sum(r)-sum(l);}
};

//INSERT ABOVE HERE
Int calc(vector<Int> vs){
  Int n=vs.size();
  BIT<Int> bit(n+10);
  Int res=0;
  for(Int i=0;i<n;i++){
    res+=i-bit.sum0(vs[i]);
    bit.add0(vs[i],1);
  }
  return res;
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;
  auto as=read(n);
  auto bs=read(n);

  if(as[0]!=bs[0]) drop(-1);

  vector<Int> xs(n-1),ys(n-1);
  for(Int i=0;i+1<n;i++) xs[i]=as[i]^as[i+1];
  for(Int i=0;i+1<n;i++) ys[i]=bs[i]^bs[i+1];

  {
    auto us=xs;
    auto vs=ys;
    sort(us.begin(),us.end());
    sort(vs.begin(),vs.end());
    if(us!=vs) drop(-1);
  }

  using P = pair<Int, Int>;
  map<Int, Int> app;
  map<P, Int> idx;
  for(Int i=0;i+1<n;i++){
    idx[P(xs[i],app[xs[i]])]=i;
    app[xs[i]]++;
  }

  app.clear();
  vector<Int> ps(n-1);
  for(Int i=0;i+1<n;i++){
    ps[i]=idx[P(ys[i],app[ys[i]])];
    app[ys[i]]++;
  }

  cout<<calc(ps)<<newl;
  return 0;
}
0